Skip to content

Fix decorators in ipython code blocks in docs #7006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/requirements/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
- dask-core>=2.30
- h5netcdf>=0.7.4
- ipykernel
- ipython
- ipython>=8.5.0
- iris>=2.3
- jupyter_client
- matplotlib-base
Expand Down
23 changes: 0 additions & 23 deletions doc/examples/_code/accessor_example.py

This file was deleted.

30 changes: 23 additions & 7 deletions doc/internals/extending-xarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,28 @@ To resolve this issue for more complex cases, xarray has the
write a custom "geo" accessor implementing a geography specific extension to
xarray:

.. literalinclude:: ../examples/_code/accessor_example.py
.. ipython:: python

@xr.register_dataset_accessor("geo")
class GeoAccessor:
def __init__(self, xarray_obj):
self._obj = xarray_obj
self._center = None

@property
def center(self):
"""Return the geographic center point of this dataset."""
if self._center is None:
# we can use a cache on our accessor objects, because accessors
# themselves are cached on instances that access them.
lon = self._obj.latitude
lat = self._obj.longitude
self._center = (float(lon.mean()), float(lat.mean()))
return self._center

def plot(self):
"""Plot data on a map."""
return "plotting!"

In general, the only restriction on the accessor class is that the ``__init__`` method
must have a single parameter: the ``Dataset`` or ``DataArray`` object it is supposed
Expand All @@ -42,7 +63,7 @@ to work on.
This achieves the same result as if the ``Dataset`` class had a cached property
defined that returns an instance of your class:

.. code-block:: python
.. ipython:: python
Comment on lines -45 to +66
Copy link
Collaborator

@keewis keewis Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should revert this: we don't actually want to execute that, it just serves as an illustration of what register_dataset_accessor does

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - though this isn't the line the build fails on though. I'm worried that my fix in ipython didn't quite work in all cases 😞

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interestingly, it's the second block (the one where we call ds.geo.center) that fails if we convert the first .. ipython:: python block to a .. ipython:: block with explicit prompt. However, converting the second block as well does not seem to help.


class Dataset:
...
Expand Down Expand Up @@ -73,11 +94,6 @@ reasons:

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

.. ipython:: python
:suppress:

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

.. ipython:: python

ds = xr.Dataset({"longitude": np.linspace(0, 10), "latitude": np.linspace(0, 20)})
Expand Down