Skip to content

Commit dc87b3c

Browse files
committed
Create util for display jupyter version warnings and update imports
1 parent 7f241f3 commit dc87b3c

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

packages/python/plotly/plotly/basewidget.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pathlib
2-
import ipywidgets as widgets
3-
from traitlets import List, Unicode, Dict, observe, Integer
2+
from traitlets import List, Dict, observe, Integer
3+
from plotly.io._utils import display_jupyter_version_warnings
44

55
from .basedatatypes import BaseFigure, BasePlotlyType
66
from .callbacks import BoxSelector, LassoSelector, InputDeviceState, Points
@@ -725,6 +725,7 @@ def _repr_mimebundle_(self, include=None, exclude=None, validate=True, **kwargs)
725725
"""
726726
Return mimebundle corresponding to default renderer.
727727
"""
728+
display_jupyter_version_warnings()
728729
return {
729730
"application/vnd.jupyter.widget-view+json": {
730731
"version_major": 2,

packages/python/plotly/plotly/io/_renderers.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from copy import copy
33

44
import os
5-
import warnings
6-
import psutil
75
from packaging.version import Version
86

97
from plotly import optional_imports
@@ -28,13 +26,14 @@
2826
CoCalcRenderer,
2927
DatabricksRenderer,
3028
)
31-
from plotly.io._utils import validate_coerce_fig_to_dict
29+
from plotly.io._utils import (
30+
validate_coerce_fig_to_dict,
31+
display_jupyter_version_warnings,
32+
)
3233

3334
ipython = optional_imports.get_module("IPython")
3435
ipython_display = optional_imports.get_module("IPython.display")
3536
nbformat = optional_imports.get_module("nbformat")
36-
jupyter_notebook = optional_imports.get_module("notebook")
37-
jupyter_lab = optional_imports.get_module("jupyterlab")
3837

3938
# Renderer configuration class
4039
# -----------------------------
@@ -398,18 +397,7 @@ def show(fig, renderer=None, validate=True, **kwargs):
398397
"Mime type rendering requires nbformat>=4.2.0 but it is not installed"
399398
)
400399

401-
parent_process = psutil.Process().parent().cmdline()[-1]
402-
403-
if "jupyter-notebook" in parent_process and jupyter_notebook.__version__ < "7":
404-
# Add warning about upgrading notebook
405-
warnings.warn(
406-
f"Plotly version >= 6 requires Jupyter Notebook >= 7 but you have {jupyter_notebook.__version__} installed.\n To upgrade Jupyter Notebook, please run `pip install notebook --upgrade`."
407-
)
408-
elif "jupyter-lab" in parent_process and jupyter_lab.__version__ < "3":
409-
# Add warning about upgrading jupyterlab
410-
warnings.warn(
411-
f"Plotly version >= 6 requires JupyterLab >= 3 but you have {jupyter_lab.__version__} installed. To upgrade JupyterLab, please run `pip install jupyterlab --upgrade`."
412-
)
400+
display_jupyter_version_warnings()
413401

414402
ipython_display.display(bundle, raw=True)
415403

packages/python/plotly/plotly/io/_utils.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import plotly
22
import plotly.graph_objs as go
33
from plotly.offline import get_plotlyjs_version
4+
from plotly import optional_imports
5+
6+
import warnings
7+
import psutil
8+
9+
jupyter_notebook = optional_imports.get_module("notebook")
10+
jupyter_lab = optional_imports.get_module("jupyterlab")
11+
12+
13+
def display_jupyter_version_warnings():
14+
15+
parent_process = psutil.Process().parent().cmdline()[-1]
16+
17+
if "jupyter-notebook" in parent_process and jupyter_notebook.__version__ < "7":
18+
# Add warning about upgrading notebook
19+
warnings.warn(
20+
f"Plotly version >= 6 requires Jupyter Notebook >= 7 but you have {jupyter_notebook.__version__} installed.\n To upgrade Jupyter Notebook, please run `pip install notebook --upgrade`."
21+
)
22+
elif "jupyter-lab" in parent_process and jupyter_lab.__version__ < "3":
23+
# Add warning about upgrading jupyterlab
24+
warnings.warn(
25+
f"Plotly version >= 6 requires JupyterLab >= 3 but you have {jupyter_lab.__version__} installed. To upgrade JupyterLab, please run `pip install jupyterlab --upgrade`."
26+
)
427

528

629
def validate_coerce_fig_to_dict(fig, validate):

packages/python/plotly/test_requirements/requirements_312_np2_optional.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ matplotlib==3.9.2
1818
scikit-image==0.24.0
1919
psutil==5.9.7
2020
kaleido
21-
orjson==3.9.10
21+
orjson==3.9.10
22+
anywidget==0.9.13

0 commit comments

Comments
 (0)