Skip to content

Commit

Permalink
Add DeepnoteRenderer to pio.renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubzitny committed Oct 20, 2023
1 parent 57e4d1d commit 681376d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
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)]

## [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/).

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

0 comments on commit 681376d

Please sign in to comment.