Skip to content

Commit 4e4f59d

Browse files
lucasgcbOrtnerMichael
authored andcommitted
Documentation Generator (Sphinx Docs) (magpylib#49)
* Add base Sphinx configuration files * Add __all__ module recognition to public packages * Fix underline lacking an hyphen in a few files * Add placeholders for index and custom pages * Add configuration to show inherited methods in docstrings * Standardize docstrings - Numpy format ( Issue magpylib#20 ) - Put the correct format for the strings in Sphinx * Put only relevant pages in the index * Add Readme to exclude list, remove module pages * Add placeholder titles for __init__.py in packages * Adjust levels and add static plages * Add picture for static pages * Fix numpy docstrings for moment package (issue magpylib#20) * Add inherited method notes * Remove redundant encoding declarations from init * Add more descriptive docstring to collection * Add autosummary list to source class init docstring * Move Readme picture to docs/_static folder * Add warning and restructure docstrings in collection * Add sphinx project build and requirement file * Adjust setup file for Sphinx installation * Remove relative path from requirements * Retain private members in docs * Move autodoc variables up top * Lock Sphinx Version in conf * Add Sphinx Version to project's setup.py * Fix typo in setup variable * Add Sphinx version to requirements in docs * Update examples in Collection.py ( magpylib#20 ) * Fix example import * Update suppression example * Update RCS method examples (Issue magpylib#20) * Update current examples ( Issue magpylib#20 ) * Update magnet examples ( Issue magpylib#20 ) * Add epsilon result to magnet example * Add markdown support to documentation - Also add placeholder to index.html * Add placeholder install instructions in markdown * Update installation instructions * Add docs build status badge to readme * Adjust docs badge
1 parent 7610ab4 commit 4e4f59d

25 files changed

+816
-357
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ instance/
7272

7373
# Sphinx documentation
7474
docs/_build/
75-
75+
docs/_autogen/
7676
# PyBuilder
7777
target/
7878

@@ -122,4 +122,6 @@ dmypy.json
122122
### Python Patch ###
123123
.venv/
124124

125+
126+
125127
# End of https://www.gitignore.io/api/python

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[![](https://readthedocs.com/projects/magpylib-magpylib/badge/?version=sphinx-docs)](https://magpylib-magpylib.readthedocs-hosted.com/)
2+
3+
---
4+
15
# About
26
*A simple and user friendly magnetic toolbox for Python 3.2+*
37

@@ -47,7 +51,7 @@ The library is now in the packCondaTest environment.
4751
- The total magnetic field that is generated by the collection is calculated on a grid in the xz-plane and is displayed using matplotlib.
4852

4953
**Program output:**
50-
![](./img/examplePlot.jpg)
54+
![](./docs/_static/examplePlot.jpg)
5155

5256
**Code:**
5357
```python

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line.
5+
SPHINXOPTS =
6+
SPHINXBUILD = sphinx-build
7+
SOURCEDIR = .
8+
BUILDDIR = _build
9+
10+
# Put it first so that "make" without argument is like "make help".
11+
help:
12+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
13+
14+
.PHONY: help Makefile
15+
16+
# Catch-all target: route all unknown targets to Sphinx using the new
17+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
18+
%: Makefile
19+
@pip install -r ./requirements.txt
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/_pages/0_summary.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MagPyLib Summary
2+
----------------
3+
4+
The core module of magpylib is :mod:`~magpylib.source`, whose subpackages
5+
offer the primitive building blocks for creating our simulation data.
6+
7+
The top level serves the :class:`magpylib.Collection`
8+
class, which offers easy grouping of :mod:`~magpylib.source` objects,
9+
allowing for the display and combination of fields.
10+
11+
The :mod:`~magpylib.math` module provides on-hand methods for handling axis
12+
and angle information as well as rotation of position vectors.
13+
14+
.. currentmodule:: magpylib
15+
16+
.. autosummary::
17+
18+
Collection
19+
source
20+
math
21+
22+

docs/_pages/how2install.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Installation Instructions
2+
3+
4+
1. Clone Repo
5+
```
6+
git clone https://github.com/ortnermichael/magpylib
7+
```
8+
2. Enter the Repo
9+
```
10+
cd magpylib
11+
```
12+
3. Install the library into the current environment
13+
```
14+
pip install .
15+
```
16+
17+
Generating the documentation:
18+
1. Clone Repo
19+
```
20+
git clone https://github.com/ortnermichael/magpylib
21+
```
22+
2. Enter the docs Repo
23+
```
24+
cd magpylib/docs
25+
```
26+
3. Run generation with `make`
27+
```
28+
make html
29+
```
30+
31+
Documentation is now in `magpylib/docs/_build/html`

docs/_pages/x_examples.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Examples
2+
========
3+
4+
The following code executes a mixed simulation with most features.
5+
6+
.. code-block:: python
7+
8+
# imports
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
import magpylib as magpy
12+
13+
# create magnets
14+
magnet1 = magpy.source.magnet.Box(mag=[0,0,600],dim=[3,3,3],pos=[-4,0,3])
15+
magnet2 = magpy.source.magnet.Cylinder(mag=[0,0,500], dim=[3,5], pos=[0,0,0])
16+
17+
# manipulate magnets
18+
magnet1.rotate(45,[0,1,0],anchor=[0,0,0])
19+
magnet2.move([5,0,-4])
20+
21+
# collect magnets
22+
pmc = magpy.Collection(magnet1,magnet2)
23+
24+
# display system geometry
25+
pmc.displaySystem()
26+
27+
# calculate B-fields on a grid
28+
xs = np.linspace(-10,10,20)
29+
zs = np.linspace(-10,10,20)
30+
Bs = np.array([[pmc.getB([x,0,z]) for x in xs] for z in zs])
31+
32+
# display fields using matplotlib
33+
fig, ax = plt.subplots()
34+
X,Y = np.meshgrid(xs,zs)
35+
U,V = Bs[:,:,0], Bs[:,:,2]
36+
ax.streamplot(X, Y, U, V, color=np.log(U**2+V**2), density=1.5)
37+
plt.show()
38+
39+
40+
.. image:: ../_static/examplePlot.jpg

docs/_static/copybutton.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2014 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
2+
// File originates from the cpython source found in https://docs.python.org/3/_static/copybutton.js
3+
4+
$(document).ready(function() {
5+
/* Add a [>>>] button on the top-right corner of code samples to hide
6+
* the >>> and ... prompts and the output and thus make the code
7+
* copyable. */
8+
var div = $('.highlight-python .highlight,' +
9+
'.highlight-default .highlight,' +
10+
'.highlight-python3 .highlight')
11+
var pre = div.find('pre');
12+
13+
// get the styles from the current theme
14+
pre.parent().parent().css('position', 'relative');
15+
var hide_text = 'Hide the prompts and output';
16+
var show_text = 'Show the prompts and output';
17+
var border_width = pre.css('border-top-width');
18+
var border_style = pre.css('border-top-style');
19+
var border_color = pre.css('border-top-color');
20+
var button_styles = {
21+
'cursor':'pointer', 'position': 'absolute', 'top': '0', 'right': '0',
22+
'border-color': border_color, 'border-style': border_style,
23+
'border-width': border_width, 'color': border_color, 'text-size': '75%',
24+
'font-family': 'monospace', 'padding-left': '0.2em', 'padding-right': '0.2em',
25+
'border-radius': '0 3px 0 0'
26+
}
27+
28+
// create and add the button to all the code blocks that contain >>>
29+
div.each(function(index) {
30+
var jthis = $(this);
31+
if (jthis.find('.gp').length > 0) {
32+
var button = $('<span class="copybutton">Click to Hide &gt;&gt;&gt;</span>');
33+
button.css(button_styles)
34+
button.attr('title', hide_text);
35+
button.data('hidden', 'false');
36+
jthis.prepend(button);
37+
}
38+
// tracebacks (.gt) contain bare text elements that need to be
39+
// wrapped in a span to work with .nextUntil() (see later)
40+
jthis.find('pre:has(.gt)').contents().filter(function() {
41+
return ((this.nodeType == 3) && (this.data.trim().length > 0));
42+
}).wrap('<span>');
43+
});
44+
45+
// define the behavior of the button when it's clicked
46+
$('.copybutton').click(function(e){
47+
e.preventDefault();
48+
var button = $(this);
49+
if (button.data('hidden') === 'false') {
50+
// hide the code output
51+
button.parent().find('.go, .gp, .gt').hide();
52+
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
53+
button.css('text-decoration', 'line-through');
54+
button.attr('title', show_text);
55+
button.data('hidden', 'true');
56+
} else {
57+
// show the code output
58+
button.parent().find('.go, .gp, .gt').show();
59+
button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
60+
button.css('text-decoration', 'none');
61+
button.attr('title', hide_text);
62+
button.data('hidden', 'false');
63+
}
64+
});
65+
});
File renamed without changes.

0 commit comments

Comments
 (0)