Skip to content

Commit 2f0fb8a

Browse files
committed
Remove _utils.ipython.get_display_function
The previous change made this function an unneccessary.
1 parent 1a4a058 commit 2f0fb8a

4 files changed

Lines changed: 51 additions & 62 deletions

File tree

plotnine/_utils/ipython.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
from typing import TYPE_CHECKING
44

55
if TYPE_CHECKING:
6-
from typing import Callable, Literal, NotRequired, TypeAlias, TypedDict
7-
86
from IPython.core.interactiveshell import InteractiveShell
97

10-
FigureFormat: TypeAlias = Literal[
11-
"png", "retina", "jpeg", "jpg", "svg", "pdf"
12-
]
13-
14-
class DisplayMetadata(TypedDict):
15-
width: NotRequired[int]
16-
height: NotRequired[int]
8+
from ..typing import DisplayMetadata, FigureFormat, MimeBundle
179

1810

1911
def get_ipython() -> "None | InteractiveShell":
@@ -28,36 +20,21 @@ def get_ipython() -> "None | InteractiveShell":
2820
return _get_ipython()
2921

3022

31-
def is_inline_backend():
23+
def is_inline_backend() -> bool:
3224
"""
3325
Return True if the inline_backend is on
3426
3527
This can only be True if also running in an jupyter/ipython session.
3628
"""
3729
import matplotlib as mpl
3830

39-
return "matplotlib_inline.backend_inline" in mpl.get_backend()
40-
41-
42-
def get_display_function() -> Callable[
43-
[dict[str, bytes], dict[str, DisplayMetadata]], None
44-
]:
45-
"""
46-
Return a function that will display the plot image
47-
"""
48-
from IPython.display import display
49-
50-
def display_func(
51-
data: dict[str, bytes], metadata: dict[str, DisplayMetadata]
52-
) -> None:
53-
display(data, metadata=metadata, raw=True)
54-
55-
return display_func
31+
backend = mpl.get_backend()
32+
return backend in ("inline", "module://matplotlib_inline.backend_inline")
5633

5734

5835
def get_mimebundle(
5936
b: bytes, format: FigureFormat, figure_size_px: tuple[int, int]
60-
):
37+
) -> MimeBundle:
6138
"""
6239
Return a the display MIME bundle from image data
6340

plotnine/composition/_compose.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from io import BytesIO
77
from typing import TYPE_CHECKING, overload
88

9-
from .._utils.ipython import get_display_function, get_ipython, get_mimebundle
9+
from .._utils.ipython import (
10+
get_ipython,
11+
get_mimebundle,
12+
is_inline_backend,
13+
)
14+
from .._utils.quarto import is_quarto_environment
1015
from ..options import get_option
1116
from ._plotspec import plotspec
1217

@@ -18,7 +23,7 @@
1823

1924
from plotnine._mpl.gridspec import p9GridSpec
2025
from plotnine.ggplot import PlotAddable, ggplot
21-
from plotnine.typing import FigureFormat
26+
from plotnine.typing import FigureFormat, MimeBundle
2227

2328

2429
@dataclass
@@ -215,7 +220,7 @@ def __getitem__(
215220
def __setitem__(self, key, value):
216221
self.items[key] = value
217222

218-
def _repr_mimebundle_(self, **kwargs):
223+
def _repr_mimebundle_(self, include=None, exclude=None) -> MimeBundle:
219224
"""
220225
Return dynamic MIME bundle for composition display
221226
"""
@@ -359,18 +364,24 @@ def _make_plotspecs(
359364
self.figure = plt.figure()
360365
self.plotspecs = list(_make_plotspecs(self, None))
361366

362-
def _display(self):
367+
def show(self):
363368
"""
364369
Display plot in the cells output
365370
366371
This function is called for its side-effects.
367-
368-
It draws the plot to an io buffer then uses ipython display
369-
methods to show the result.
370372
"""
371-
data, metadata = self._repr_mimebundle_()
372-
display_func = get_display_function()
373-
display_func(data, metadata)
373+
# Prevent against any modifications to the users
374+
# ggplot object. Do the copy here as we may/may not
375+
# assign a default theme
376+
self = deepcopy(self)
377+
378+
if is_inline_backend() or is_quarto_environment():
379+
from IPython.display import display
380+
381+
data, metadata = self._repr_mimebundle_()
382+
display(data, metadata=metadata, raw=True)
383+
else:
384+
self.draw(show=True)
374385

375386
def draw(self, *, show: bool = False) -> Figure:
376387
"""

plotnine/ggplot.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
)
2626
from ._utils.context import plot_context
2727
from ._utils.ipython import (
28-
get_display_function,
2928
get_ipython,
3029
get_mimebundle,
3130
is_inline_backend,
@@ -56,7 +55,7 @@
5655
from plotnine.composition import Compose
5756
from plotnine.coords.coord import coord
5857
from plotnine.facets.facet import facet
59-
from plotnine.typing import DataLike, FigureFormat
58+
from plotnine.typing import DataLike, FigureFormat, MimeBundle
6059

6160
class PlotAddable(Protocol):
6261
"""
@@ -138,11 +137,15 @@ def __str__(self) -> str:
138137
w, h = self.theme._figure_size_px
139138
return f"<ggplot: ({w} x {h})>"
140139

141-
def _repr_mimebundle_(self, **kwargs):
140+
def _repr_mimebundle_(self, include=None, exclude=None) -> MimeBundle:
142141
"""
143142
Return dynamic MIME bundle for plot display
144143
145144
This method is called when a ggplot object is the last in the cell.
145+
146+
Notes
147+
-----
148+
- https://ipython.readthedocs.io/en/stable/config/integrating.html
146149
"""
147150
ip = get_ipython()
148151
format: FigureFormat = (
@@ -166,34 +169,21 @@ def show(self):
166169
"""
167170
Show plot using the matplotlib backend set by the user
168171
169-
Users should prefer this method instead of printing or repring
170-
the object.
172+
This function is called for its side-effects.
171173
"""
172174
# Prevent against any modifications to the users
173175
# ggplot object. Do the copy here as we may/may not
174176
# assign a default theme
175177
self = deepcopy(self)
176178

177179
if is_inline_backend() or is_quarto_environment():
178-
# Take charge of the display because we have to make
179-
# adjustments for retina output.
180-
self._display()
180+
from IPython.display import display
181+
182+
data, metadata = self._repr_mimebundle_()
183+
display(data, metadata=metadata, raw=True)
181184
else:
182185
self.draw(show=True)
183186

184-
def _display(self):
185-
"""
186-
Display plot in the cells output
187-
188-
This function is called for its side-effects.
189-
190-
It plots the plot to an io buffer, then uses ipython display
191-
methods to show the result
192-
"""
193-
data, metadata = self._repr_mimebundle_()
194-
display_func = get_display_function()
195-
display_func(data, metadata)
196-
197187
def __deepcopy__(self, memo: dict[Any, Any]) -> ggplot:
198188
"""
199189
Deep copy without copying the dataframe and environment

plotnine/typing.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __future__ import annotations
22

3-
import sys
4-
from datetime import datetime, timedelta
53
from typing import (
6-
TYPE_CHECKING,
74
Any,
85
Callable,
96
Literal,
7+
NotRequired,
108
Protocol,
119
Sequence,
10+
TypedDict,
1211
TypeVar,
1312
)
1413

@@ -142,3 +141,15 @@ class PTransform(Protocol):
142141
"""
143142

144143
def __call__(self, x: TFloatArrayLike) -> TFloatArrayLike: ...
144+
145+
146+
class DisplayMetadata(TypedDict):
147+
"""
148+
Metadata for the IPython output
149+
"""
150+
151+
width: NotRequired[int]
152+
height: NotRequired[int]
153+
154+
155+
MimeBundle: TypeAlias = tuple[dict[str, bytes], dict[str, DisplayMetadata]]

0 commit comments

Comments
 (0)