|
| 1 | +.. _l-sphinx-runpython: |
| 2 | + |
| 3 | +========= |
| 4 | +runpython |
| 5 | +========= |
| 6 | + |
| 7 | +Description |
| 8 | +=========== |
| 9 | + |
| 10 | +Location: :py:class:`RunPythonDirective <sphinx_runpython.runpython.sphinx_runpython_extension.RunPythonDirective>`. |
| 11 | + |
| 12 | +In *conf.py*: |
| 13 | + |
| 14 | +:: |
| 15 | + |
| 16 | + extensions = [ ... |
| 17 | + 'sphinx_runpython.runpython.sphinxext_runpython_extension'] |
| 18 | + |
| 19 | +Documentation means many examples which needs to be updated when a change |
| 20 | +happen unless the documentation runs the example itself and update its output. |
| 21 | +That's what this directive does. It adds as raw text whatever comes out |
| 22 | +throught the standard output. |
| 23 | + |
| 24 | +.. sidebar:: runpython |
| 25 | + |
| 26 | + :: |
| 27 | + |
| 28 | + .. runpython:: |
| 29 | + :showcode: |
| 30 | + |
| 31 | + import os |
| 32 | + for i, name in enumerate(os.listdir(".")): |
| 33 | + print(i, name) |
| 34 | + |
| 35 | +.. runpython:: |
| 36 | + :showcode: |
| 37 | + |
| 38 | + import os |
| 39 | + for i, name in enumerate(os.listdir(".")): |
| 40 | + print(i, name) |
| 41 | + |
| 42 | +The output can also be compiled as RST format and the code can be hidden. |
| 43 | +It is useful if the documentation is a copy/paste of some external process |
| 44 | +or function. This function can be directly called from the documentation. |
| 45 | +The output must be converted into RST format. It is then added to the |
| 46 | +documentation. It is quite useful to display the version of some installed |
| 47 | +modules. |
| 48 | + |
| 49 | +.. sidebar:: runpython and rst |
| 50 | + |
| 51 | + :: |
| 52 | + |
| 53 | + .. runpython:: |
| 54 | + :rst: |
| 55 | + |
| 56 | + import pandas, numpy, sphinx |
| 57 | + |
| 58 | + for i, mod in [sphinx, pandas, numpy]: |
| 59 | + print("* version of *{0}*: *{1}*".format( |
| 60 | + getattr(mod, "__name__"), getattr(mod, "__version__")) |
| 61 | + |
| 62 | +.. runpython:: |
| 63 | + :rst: |
| 64 | + |
| 65 | + import os |
| 66 | + for i, name in enumerate(os.listdir(".")): |
| 67 | + print("* file **{0}**: *{1}*".format(i, name)) |
| 68 | + |
| 69 | +If the code throws an exception (except a syntax error), |
| 70 | +it can be caught by adding the option ``:exception:``. |
| 71 | +The directive displays the traceback. |
| 72 | + |
| 73 | +.. runpython:: |
| 74 | + :showcode: |
| 75 | + :exception: |
| 76 | + |
| 77 | + import os |
| 78 | + for i, name in enumerate(os.listdir("not existing")): |
| 79 | + pass |
| 80 | + |
| 81 | +.. _l-image-rst-runpython: |
| 82 | + |
| 83 | +The directive can also be used to display images |
| 84 | +with a tweak however. It consists in writing *rst* |
| 85 | +code. The variable ``__WD__`` indicates the local |
| 86 | +directory. |
| 87 | + |
| 88 | +.. runpython:: |
| 89 | + :showcode: |
| 90 | + |
| 91 | + print('__WD__=%r' % __WD__) |
| 92 | + |
| 93 | +Applied to images... |
| 94 | + |
| 95 | +.. sidebar:: runpython and image |
| 96 | + |
| 97 | + :: |
| 98 | + |
| 99 | + .. runpython:: |
| 100 | + :rst: |
| 101 | + |
| 102 | + import matplotlib.pyplot as plt |
| 103 | + fig, ax = plt.subplots(1, 1, figsize=(4, 4)) |
| 104 | + ax.plot([0, 1], [0, 1], '--') |
| 105 | + fig.savefig(os.path.join(__WD__, "oo.png")) |
| 106 | + |
| 107 | + text = ".. image:: oo.png\\\\n :width: 202px" |
| 108 | + print(text) |
| 109 | + |
| 110 | +The image needs to be save in the same folder than |
| 111 | +the *rst* file. |
| 112 | + |
| 113 | +.. runpython:: |
| 114 | + :rst: |
| 115 | + |
| 116 | + import matplotlib.pyplot as plt |
| 117 | + fig, ax = plt.subplots(1, 1, figsize=(4, 4)) |
| 118 | + ax.plot([0, 1], [0, 1], '--') |
| 119 | + fig.savefig(os.path.join(__WD__, "oo.png")) |
| 120 | + |
| 121 | + text = ".. image:: oo.png\\\\n :width: 201px" |
| 122 | + print(text) |
| 123 | + |
| 124 | +Option ``:toggle:`` can hide the code or the output or both |
| 125 | +but let the user unhide it by clicking on a button. |
| 126 | + |
| 127 | +.. sidebar:: runpython and image |
| 128 | + |
| 129 | + :: |
| 130 | + |
| 131 | + .. runpython:: |
| 132 | + :showcode: |
| 133 | + :toggle: out |
| 134 | + |
| 135 | + for i in range(0, 10): |
| 136 | + print("i=", i) |
| 137 | + |
| 138 | +.. runpython:: |
| 139 | + :showcode: |
| 140 | + :toggle: out |
| 141 | + |
| 142 | + for i in range(0, 10): |
| 143 | + print("i=", i) |
| 144 | + |
| 145 | +The last option of *runpython* allows the user to keep |
| 146 | +some context from one execution to the next one. |
| 147 | + |
| 148 | +.. sidebar:: runpython and context |
| 149 | + |
| 150 | + :: |
| 151 | + |
| 152 | + .. runpython:: |
| 153 | + :showcode: |
| 154 | + :store: |
| 155 | + |
| 156 | + a_to_keep = 5 |
| 157 | + print("a_to_keep", "=", a_to_keep) |
| 158 | + |
| 159 | + .. runpython:: |
| 160 | + :showcode: |
| 161 | + :restore: |
| 162 | + |
| 163 | + a_to_keep += 5 |
| 164 | + print("a_to_keep", "=", a_to_keep) |
| 165 | + |
| 166 | +.. runpython:: |
| 167 | + :showcode: |
| 168 | + :store: |
| 169 | + |
| 170 | + a_to_keep = 5 |
| 171 | + print("a_to_keep", "=", a_to_keep) |
| 172 | + |
| 173 | +.. runpython:: |
| 174 | + :showcode: |
| 175 | + :restore: |
| 176 | + |
| 177 | + a_to_keep += 5 |
| 178 | + print("a_to_keep", "=", a_to_keep) |
| 179 | + |
| 180 | +.. index:: sphinx-autorun |
| 181 | + |
| 182 | +`sphinx-autorun <https://pypi.org/project/sphinx-autorun/>`_ offers a similar |
| 183 | +service except it cannot produce compile :epkg:`RST` content, |
| 184 | +hide the source and a couple of other options. |
| 185 | + |
| 186 | +Interesting functions |
| 187 | +===================== |
| 188 | + |
| 189 | +.. autofunction:: sphinx_runpython.runpython.run_cmd |
0 commit comments