Skip to content

Commit

Permalink
Add "jupyter" renderer based on JupyterChart (#3283)
Browse files Browse the repository at this point in the history
* Add "jupyter" renderer based on JupyterChart

* Add release note

* rename to jupyter_renderer

* # type: ignore[attr-defined]

* Add jupyter renderer doc section

* Update docs to be clear that you can't access params with jupyter renderer

* Update doc/releases/changes.rst

Co-authored-by: Stefan Binder <binder_stefan@outlook.com>

---------

Co-authored-by: Stefan Binder <binder_stefan@outlook.com>
Co-authored-by: Mattijn van Hoek <mattijn@gmail.com>
  • Loading branch information
3 people authored Dec 26, 2023
1 parent ebf9da5 commit b081237
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions altair/vegalite/v5/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def svg_renderer(spec: dict, **metadata) -> Dict[str, str]:
)


def jupyter_renderer(spec: dict):
"""Render chart using the JupyterChart Jupyter Widget"""
from altair import Chart, JupyterChart

# Need to ignore attr-defined mypy rule because mypy doesn't see _repr_mimebundle_
# conditionally defined in AnyWidget
return JupyterChart(chart=Chart.from_dict(spec))._repr_mimebundle_() # type: ignore[attr-defined]


html_renderer = HTMLRenderer(
mode="vega-lite",
template="universal",
Expand All @@ -105,6 +114,7 @@ def svg_renderer(spec: dict, **metadata) -> Dict[str, str]:
renderers.register("json", json_renderer)
renderers.register("png", png_renderer)
renderers.register("svg", svg_renderer)
renderers.register("jupyter", jupyter_renderer)
renderers.enable("default")


Expand Down
1 change: 1 addition & 0 deletions doc/releases/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Version 5.3.0 (unreleased month day, year)

Enhancements
~~~~~~~~~~~~
- Add "jupyter" renderer which uses JupyterChart for rendering (#3283). See :ref:`renderers` for more information.
- Support restrictive FIPS-compliant environment (#3291)

Bug Fixes
Expand Down
10 changes: 10 additions & 0 deletions doc/user_guide/display_frontends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ The most used built-in renderers are:
newer versions of JupyterLab_, nteract_, and `VSCode-Python`_, but does not work
with the `Jupyter Notebook`_, or with tools like nbviewer_ and nbconvert_.

``alt.renderers.enable("jupyter")``
*(added in version 5.3):* Output the chart using :ref:`user-guide-jupyterchart`. This renderer
is compatible with environments that support third-party Jupyter Widgets including
JupyterLab_, `Jupyter Notebook`_, `VSCode-Python`_, and `Colab`_.
It requires a web connection in order to load relevant Javascript libraries. Note that,
although this renderer uses ``JupyterChart``, it does not provide the
ability to access value and selection params in Python. To do so, create a ``JupyterChart``
object explicitly following the instructions in the :ref:`user-guide-jupyterchart`
documentation.

In addition, Altair includes the following renderers:

- ``"default"``, ``"colab"``, ``"kaggle"``, ``"zeppelin"``: identical to ``"html"``
Expand Down
4 changes: 2 additions & 2 deletions doc/user_guide/jupyter_chart.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _user-guide-jupyterchart:

JupyterChart Interactivity
==========================
JupyterChart
============
The ``JupyterChart`` class, introduced in Vega-Altair 5.1, makes it possible to update charts
after they have been displayed and access the state of :ref:`user-guide-interactions` from Python.

Expand Down

0 comments on commit b081237

Please sign in to comment.