Skip to content
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

Add DeepnoteRenderer to pio.renderers #4389

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Repair crash on Matplotlib 3.8 related to get_offset_position [[#4372](https://github.com/plotly/plotly.py/pull/4372)],
- Handle deprecation of `pandas.Series.dt.to_pydatetime()` calls and suppress the `FutureWarning` they currently emit. [[#4379](https://github.com/plotly/plotly.py/pull/4379)]
- Add DeepnoteRenderer to pio.renderers [[#4389](https://github.com/plotly/plotly.py/pull/4389)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd call this ### Added, rather than ### Fixed 😉


## [5.17.0] - 2023-09-15

Expand Down
5 changes: 4 additions & 1 deletion doc/python/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fig

> To be precise, figures will display themselves using the current default renderer when the two following conditions are true. First, the last expression in a cell must evaluate to a figure. Second, `plotly.py` must be running from within an `IPython` kernel.

**In many contexts, an appropriate renderer will be chosen automatically and you will not need to perform any additional configuration.** These contexts include the classic [Jupyter Notebook](https://jupyter.org/), [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/), [Visual Studio Code notebooks](https://code.visualstudio.com/docs/python/jupyter-support), [Google Colaboratory](https://colab.research.google.com/notebooks/intro.ipynb), [Kaggle](https://www.kaggle.com/kernels) notebooks, [Azure](https://notebooks.azure.com/) notebooks, and the [Python interactive shell](https://www.python.org/shell/).
**In many contexts, an appropriate renderer will be chosen automatically and you will not need to perform any additional configuration.** These contexts include the classic [Jupyter Notebook](https://jupyter.org/), [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/), [Visual Studio Code notebooks](https://code.visualstudio.com/docs/python/jupyter-support), [Google Colaboratory](https://colab.research.google.com/notebooks/intro.ipynb), [Kaggle](https://www.kaggle.com/kernels) notebooks, [Azure](https://notebooks.azure.com/) notebooks, [Deepnote](https://deepnote.com/) notebooks, and the [Python interactive shell](https://www.python.org/shell/).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you planning to add a new auto-detection block then in _renderers.py?

Copy link
Author

Choose a reason for hiding this comment

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

Good point, will do. Thanks.


Additional contexts are supported by choosing a compatible renderer including the [IPython console](https://docs.spyder-ide.org/ipythonconsole.html), [QtConsole](https://qtconsole.readthedocs.io/en/stable/), [Spyder](https://www.spyder-ide.org/), and more.

Expand Down Expand Up @@ -138,6 +138,9 @@ These are aliases for `notebook_connected` because this renderer is a good choic
###### `colab`
This is a custom renderer for use with [Google Colab](https://colab.research.google.com).

###### `deepnote`
This is a custom renderer for use with [Deepnote](https://deepnote.com/) notebooks.

###### `browser`
This renderer will open a figure in a browser tab using the default web browser. This renderer can only be used when the Python kernel is running locally on the same machine as the web browser, so it is not compatible with Jupyter Hub or online notebook services.

Expand Down
25 changes: 25 additions & 0 deletions packages/python/plotly/plotly/io/_base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,31 @@ def __init__(
)


class DeepnoteRenderer(HtmlRenderer):
"""
Renderer to display interactive figures in Deepnote Notebooks.

This renderer is enabled by default when running in a Deepnote notebook.

mime type: 'text/html'
"""

def __init__(
self, config=None, auto_play=False, post_script=None, animation_opts=None
):

super(DeepnoteRenderer, self).__init__(
connected=True,
full_html=True,
requirejs=False,
global_init=False,
config=config,
auto_play=auto_play,
post_script=post_script,
animation_opts=animation_opts,
)


class IFrameRenderer(MimetypeRenderer):
"""
Renderer to display interactive figures using an IFrame. HTML
Expand Down
2 changes: 2 additions & 0 deletions packages/python/plotly/plotly/io/_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
KaggleRenderer,
AzureRenderer,
ColabRenderer,
DeepnoteRenderer,
JsonRenderer,
PngRenderer,
JpegRenderer,
Expand Down Expand Up @@ -418,6 +419,7 @@ def show(fig, renderer=None, validate=True, **kwargs):
renderers["kaggle"] = KaggleRenderer(config=config)
renderers["azure"] = AzureRenderer(config=config)
renderers["colab"] = ColabRenderer(config=config)
renderers["deepnote"] = DeepnoteRenderer(config=config)
renderers["cocalc"] = CoCalcRenderer()
renderers["databricks"] = DatabricksRenderer()

Expand Down
1 change: 1 addition & 0 deletions packages/python/plotly/plotly/io/base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
JpegRenderer,
HtmlRenderer,
ColabRenderer,
DeepnoteRenderer,
KaggleRenderer,
NotebookRenderer,
ExternalRenderer,
Expand Down
25 changes: 25 additions & 0 deletions packages/python/plotly/plotly/tests/test_io/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def test_colab_renderer_show(fig1):
assert mock_kwargs == {"raw": True}


def test_deepnote_renderer_show(fig1):
pio.renderers.default = "deepnote"

with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1)

# Get display call arguments
mock_call_args = mock_display.call_args
mock_arg1 = mock_call_args[0][0]

# Check for html bundle
assert list(mock_arg1) == ["text/html"]

# Check html contents
html = mock_arg1["text/html"]
assert_full_html(html)
assert_html_renderer_connected(html)
assert_not_requirejs(html)

# check kwargs
mock_kwargs = mock_call_args[1]
assert mock_kwargs == {"raw": True}


@pytest.mark.parametrize(
"name,connected",
[("notebook", False), ("notebook_connected", True), ("kaggle", True)],
Expand Down Expand Up @@ -344,6 +368,7 @@ def test_repr_html(renderer):
"colab",
"cocalc",
"databricks",
"deepnote",
"json",
"browser",
"firefox",
Expand Down