10000 API: Add entrypoint for plotting by TomAugspurger · Pull Request #27488 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

API: Add entrypoint for plotting #27488

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

Merged
merged 11 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension < 8000 label class="SelectMenu-item" role="menuitem"> .rst  (2)


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
API: Add entrypoint for plotting
Libraries, including pandas, register backends via entrypoints.

xref #26747
  • Loading branch information
TomAugspurger committed Jul 20, 2019
commit 853bd6649fd4080ff900eb071f52bfa3706b5b2a
17 changes: 17 additions & 0 deletions doc/source/development/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,22 @@ This would be more or less equivalent to:
The backend module can then use other visualization tools (Bokeh, Altair,...)
to generate the plots.

Libraries implementing the plotting backend should use `entry points <https://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins>`__
to make their backend discoverable to pandas. The key is ``"pandas_plotting_backends"``. For example, pandas
registers the default "matplotlib" backend as follows.

.. code-block:: python

# in setup.py
setup(
...
entry_points={
"pandas_plotting_backends": [
"matplotlib = pandas:plotting._matplotlib",
],
},
)


More information on how to implement a third-party plotting backend can be found at
https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py#L1.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ I/O
Plotting
^^^^^^^^

-
- Added a pandas_plotting_backends entrypoint group for registering plot backends. See :ref:`extending.plotting-backends` for more (:issue:`26747`).
-
-

Expand Down
29 changes: 24 additions & 5 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,10 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
return self(kind="hexbin", x=x, y=y, C=C, **kwargs)


def _get_plot_backend(backend=None):
_backends = {}


def _get_plot_backend(backend="matplotlib"):
"""
Return the plotting backend to use (e.g. `pandas.plotting._matplotlib`).

Expand All @@ -1546,7 +1549,23 @@ def _get_plot_backend(backend=None):
The backend is imported lazily, as matplotlib is a soft dependency, and
pandas can be used without it being installed.
"""
backend_str = backend or pandas.get_option("plotting.backend")
if backend_str == "matplotlib":
backend_str = "pandas.plotting._matplotlib"
return importlib.import_module(backend_str)
import pkg_resources # Delay import for performance.

if backend in _backends:
return _backends[backend]

for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
_backends[entry_point.name] = entry_point.load()

try:
return _backends[backend]
except KeyError:
try:
module = importlib.import_module(backend)
except ImportError:
pass
else:
_backends[backend] = module
return module

raise ValueError("No backend {}".format(backend))
10000 31 changes: 31 additions & 0 deletions pandas/tests/plotting/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import sys
import types

import pkg_resources
import pytest

import pandas
Expand Down Expand Up @@ -36,3 +40,30 @@ def test_backend_is_correct(monkeypatch):
pandas.set_option("plotting.backend", "matplotlib")
except ImportError:
pass


def test_register_entrypoint():
Copy link
Member

Choose a reason for hiding this comment

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

this is failing for me locally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What are you seeing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I'm guessing it's a KeyError: 'pandas_plotting_backends'?

You'll need to re-run python -m pip install -e . in your pandas directory. This adds the entrypoint.

Copy link
Member

Choose a reason for hiding this comment

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

Yep that does fix it, thanks

mod = types.ModuleType("my_backend")
mod.plot = lambda *args, **kwargs: 1

backends = pkg_resources.get_entry_map("pandas")
my_entrypoint = pkg_resources.EntryPoint(
"pandas_plotting_backend",
mod.__name__,
dist=pkg_resources.get_distribution("pandas"),
)
backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint
# TODO: the docs recommend importlib.util.module_from_spec. But this works for now.
sys.modules["my_backend"] = mod

result = pandas.plotting._core._get_plot_backend("my_backend")
assert result is mod


def test_register_import():
mod = types.ModuleType("my_backend2")
mod.plot = lambda *args, **kwargs: 1
sys.modules["my_backend2"] = mod

result = pandas.plotting._core._get_plot_backend("my_backend2")
assert result is mod
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,5 +830,10 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
"hypothesis>=3.58",
]
},
entry_points={
"pandas_plotting_backends": [
"matplotlib = pandas:plotting._matplotlib",
],
},
**setuptools_kwargs
)
0