Skip to content

Commit 568f3c1

Browse files
authored
Switch docs to jupyter-execute sphinx extension for HTML reprs (#10383)
1 parent fa01fad commit 568f3c1

29 files changed

+1587
-1287
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ __pycache__
1010
doc/*.nc
1111
doc/auto_gallery
1212
doc/rasm.zarr
13-
doc/savefig
1413

1514
# C extensions
1615
*.so

doc/conf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
"sphinx.ext.extlinks",
6262
"sphinx.ext.mathjax",
6363
"sphinx.ext.napoleon",
64-
"IPython.sphinxext.ipython_directive",
65-
"IPython.sphinxext.ipython_console_highlighting",
6664
"jupyter_sphinx",
6765
"nbsphinx",
6866
"sphinx_autosummary_accessors",
@@ -213,7 +211,7 @@
213211

214212
# List of patterns, relative to source directory, that match files and
215213
# directories to ignore when looking for source files.
216-
exclude_patterns = ["_build", "**.ipynb_checkpoints"]
214+
exclude_patterns = ["_build", "debug.ipynb", "**.ipynb_checkpoints"]
217215

218216

219217
# The name of the Pygments (syntax highlighting) style to use.

doc/contribute/contributing.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,24 +387,24 @@ Some other important things to know about the docs:
387387
for a detailed explanation, or look at some of the existing functions to
388388
extend it in a similar manner.
389389

390-
- The tutorials make heavy use of the `ipython directive
391-
<https://matplotlib.org/sampledoc/ipython_directive.html>`_ sphinx extension.
392-
This directive lets you put code in the documentation which will be run
390+
- The documentation makes heavy use of the `jupyter-sphinx extension
391+
<https://jupyter-sphinx.readthedocs.io>`_.
392+
The ``jupyter-execute`` directive lets you put code in the documentation which will be run
393393
during the doc build. For example:
394394

395395
.. code:: rst
396396
397-
.. ipython:: python
397+
.. jupyter-execute::
398398
399399
x = 2
400400
x**3
401401
402-
will be rendered as::
402+
will be rendered as:
403403

404-
In [1]: x = 2
404+
.. jupyter-execute::
405405

406-
In [2]: x**3
407-
Out[2]: 8
406+
x = 2
407+
x**3
408408

409409
Almost all code examples in the docs are run (and the output saved) during the
410410
doc build. This approach means that code examples will always be up to date,

doc/get-help/faq.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Frequently Asked Questions
44
==========================
55

6-
.. ipython:: python
7-
:suppress:
6+
.. jupyter-execute::
7+
:hide-code:
88

99
import numpy as np
1010
import pandas as pd
@@ -101,22 +101,22 @@ Unfortunately, this means we sometimes have to explicitly cast our results from
101101
xarray when using them in other libraries. As an illustration, the following
102102
code fragment
103103

104-
.. ipython:: python
104+
.. jupyter-execute::
105105

106106
arr = xr.DataArray([1, 2, 3])
107107
pd.Series({"x": arr[0], "mean": arr.mean(), "std": arr.std()})
108108

109109
does not yield the pandas DataFrame we expected. We need to specify the type
110110
conversion ourselves:
111111

112-
.. ipython:: python
112+
.. jupyter-execute::
113113

114114
pd.Series({"x": arr[0], "mean": arr.mean(), "std": arr.std()}, dtype=float)
115115

116116
Alternatively, we could use the ``item`` method or the ``float`` constructor to
117117
convert values one at a time
118118

119-
.. ipython:: python
119+
.. jupyter-execute::
120120

121121
pd.Series({"x": arr[0].item(), "mean": float(arr.mean())})
122122

doc/internals/duck-arrays-integration.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,25 @@ To avoid duplicated information, this method must omit information about the sha
7070
:term:`dtype`. For example, the string representation of a ``dask`` array or a
7171
``sparse`` matrix would be:
7272

73-
.. ipython:: python
73+
.. jupyter-execute::
7474

7575
import dask.array as da
7676
import xarray as xr
77+
import numpy as np
7778
import sparse
7879

80+
.. jupyter-execute::
81+
7982
a = da.linspace(0, 1, 20, chunks=2)
8083
a
8184

85+
.. jupyter-execute::
86+
8287
b = np.eye(10)
8388
b[[5, 7, 3, 0], [6, 8, 2, 9]] = 2
8489
b = sparse.COO.from_numpy(b)
8590
b
8691

92+
.. jupyter-execute::
93+
8794
xr.Dataset(dict(a=("x", a), b=(("y", "z"), b)))

doc/internals/extending-xarray.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
Extending xarray using accessors
55
================================
66

7-
.. ipython:: python
8-
:suppress:
7+
.. jupyter-execute::
8+
:hide-code:
99

1010
import xarray as xr
11+
import numpy as np
1112

1213

1314
Xarray is designed as a general purpose library and hence tries to avoid
@@ -89,15 +90,18 @@ reasons:
8990

9091
Back in an interactive IPython session, we can use these properties:
9192

92-
.. ipython:: python
93-
:suppress:
93+
.. jupyter-execute::
94+
:hide-code:
9495

9596
exec(open("examples/_code/accessor_example.py").read())
9697

97-
.. ipython:: python
98+
.. jupyter-execute::
9899

99100
ds = xr.Dataset({"longitude": np.linspace(0, 10), "latitude": np.linspace(0, 20)})
100101
ds.geo.center
102+
103+
.. jupyter-execute::
104+
101105
ds.geo.plot()
102106

103107
The intent here is that libraries that extend xarray could add such an accessor

doc/internals/how-to-add-new-backend.rst

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,27 @@ performs the inverse transformation.
221221

222222
In the following an example on how to use the coders ``decode`` method:
223223

224-
.. ipython:: python
225-
:suppress:
224+
.. jupyter-execute::
225+
:hide-code:
226226

227227
import xarray as xr
228+
import numpy as np
228229

229-
.. ipython:: python
230+
.. jupyter-execute::
230231

231232
var = xr.Variable(
232233
dims=("x",), data=np.arange(10.0), attrs={"scale_factor": 10, "add_offset": 2}
233234
)
234235
var
235236

237+
.. jupyter-execute::
238+
236239
coder = xr.coding.variables.CFScaleOffsetCoder()
237240
decoded_var = coder.decode(var)
238241
decoded_var
242+
243+
.. jupyter-execute::
244+
239245
decoded_var.encoding
240246

241247
Some of the transformations can be common to more backends, so before
@@ -432,36 +438,55 @@ In the ``BASIC`` indexing support, numbers and slices are supported.
432438

433439
Example:
434440

435-
.. ipython::
436-
:verbatim:
441+
.. jupyter-input::
442+
443+
# () shall return the full array
444+
backend_array._raw_indexing_method(())
445+
446+
.. jupyter-output::
447+
448+
array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
449+
450+
.. jupyter-input::
451+
452+
# shall support integers
453+
backend_array._raw_indexing_method(1, 1)
437454

438-
In [1]: # () shall return the full array
439-
...: backend_array._raw_indexing_method(())
440-
Out[1]: array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])
455+
.. jupyter-output::
441456

442-
In [2]: # shall support integers
443-
...: backend_array._raw_indexing_method(1, 1)
444-
Out[2]: 5
457+
5
445458

446-
In [3]: # shall support slices
447-
...: backend_array._raw_indexing_method(slice(0, 3), slice(2, 4))
448-
Out[3]: array([[2, 3], [6, 7], [10, 11]])
459+
.. jupyter-input::
460+
461+
# shall support slices
462+
backend_array._raw_indexing_method(slice(0, 3), slice(2, 4))
463+
464+
.. jupyter-output::
465+
466+
array([[2, 3], [6, 7], [10, 11]])
449467

450468
**OUTER**
451469

452470
The ``OUTER`` indexing shall support number, slices and in addition it shall
453471
support also lists of integers. The outer indexing is equivalent to
454472
combining multiple input list with ``itertools.product()``:
455473

456-
.. ipython::
457-
:verbatim:
474+
.. jupyter-input::
475+
476+
backend_array._raw_indexing_method([0, 1], [0, 1, 2])
458477

459-
In [1]: backend_array._raw_indexing_method([0, 1], [0, 1, 2])
460-
Out[1]: array([[0, 1, 2], [4, 5, 6]])
478+
.. jupyter-output::
479+
480+
array([[0, 1, 2], [4, 5, 6]])
481+
482+
.. jupyter-input::
461483

462484
# shall support integers
463-
In [2]: backend_array._raw_indexing_method(1, 1)
464-
Out[2]: 5
485+
backend_array._raw_indexing_method(1, 1)
486+
487+
.. jupyter-output::
488+
489+
5
465490

466491

467492
**OUTER_1VECTOR**

doc/internals/internal-design.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
.. ipython:: python
2-
:suppress:
1+
.. jupyter-execute::
2+
:hide-code:
33

44
import numpy as np
55
import pandas as pd
66
import xarray as xr
77

88
np.random.seed(123456)
9-
np.set_printoptions(threshold=20)
9+
np.set_printoptions(threshold=10, edgeitems=2)
1010

1111
.. _internal design:
1212

@@ -150,7 +150,7 @@ Lazy Loading
150150
If we open a ``Variable`` object from disk using :py:func:`~xarray.open_dataset` we can see that the actual values of
151151
the array wrapped by the data variable are not displayed.
152152

153-
.. ipython:: python
153+
.. jupyter-execute::
154154

155155
da = xr.tutorial.open_dataset("air_temperature")["air"]
156156
var = da.variable
@@ -162,7 +162,7 @@ This is because the values have not yet been loaded.
162162
If we look at the private attribute :py:meth:`~xarray.Variable._data` containing the underlying array object, we see
163163
something interesting:
164164

165-
.. ipython:: python
165+
.. jupyter-execute::
166166

167167
var._data
168168

@@ -171,13 +171,13 @@ but provide important functionality.
171171

172172
Calling the public :py:attr:`~xarray.Variable.data` property loads the underlying array into memory.
173173

174-
.. ipython:: python
174+
.. jupyter-execute::
175175

176176
var.data
177177

178178
This array is now cached, which we can see by accessing the private attribute again:
179179

180-
.. ipython:: python
180+
.. jupyter-execute::
181181

182182
var._data
183183

@@ -189,22 +189,22 @@ subsequent analysis, by deferring loading data until after indexing is performed
189189

190190
Let's open the data from disk again.
191191

192-
.. ipython:: python
192+
.. jupyter-execute::
193193

194194
da = xr.tutorial.open_dataset("air_temperature")["air"]
195195
var = da.variable
196196

197197
Now, notice how even after subsetting the data has does not get loaded:
198198

199-
.. ipython:: python
199+
.. jupyter-execute::
200200

201201
var.isel(time=0)
202202

203203
The shape has changed, but the values are still not shown.
204204

205205
Looking at the private attribute again shows how this indexing information was propagated via the hidden lazy indexing classes:
206206

207-
.. ipython:: python
207+
.. jupyter-execute::
208208

209209
var.isel(time=0)._data
210210

0 commit comments

Comments
 (0)