Skip to content

Commit 45441ae

Browse files
authored
Update as_widget() to use the new altair.JupyterChart (#113)
1 parent f3200e9 commit 45441ae

File tree

4 files changed

+24
-39
lines changed

4 files changed

+24
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to shinywidgets will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
* `as_widget()` uses the new `altair.JupyterChart()` to coerce `altair.Chart()` into a `ipywidgets.widgets.Widget` instance. (#120)
11+
812
## [0.2.2] - 2023-10-31
913

1014
* `@render_widget` now builds on `shiny`'s `render.transformer` infrastructure, and as a result, it works more seamlessly in `shiny.express` mode. (#110)

README.md

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ See the [using ipywidgets section](https://shiny.rstudio.com/py/docs/ipywidgets.
1818

1919
### What ipywidgets are supported?
2020

21-
In theory, shinywidgets supports any instance that inherits from `{ipywidgets}`' `Widget` class.
21+
In theory, shinywidgets supports any instance that inherits from `{ipywidgets}`' `Widget` class. That is, if `isinstance(obj, ipywidgets.widgets.Widget)` returns `True` for some object `obj`, then `{shinywidgets}` should be able to render it.
2222

23-
That said, `{shinywidgets}` can also "directly" render objects that don't inherit from `Widget`, but have a known way of coercing into a `Widget` instance. This list currently includes:
23+
`{shinywidgets}` can also render objects that don't inherit from `Widget`, but have a known way of coercing into a `Widget` instance. This list currently includes:
2424

25-
* Altair charts (via the [vega](https://pypi.org/project/vega/) package).
26-
* Bokeh widgets (via the [jupyter_bokeh](https://github.com/bokeh/jupyter_bokeh) package).
27-
* Plotly's `Figure` class (via `FigureWidget`).
25+
* Altair charts (i.e., `altair.Chart()` instances).
26+
* Plotly figures (i.e., `plotly.go.Figure()`)
2827
* Pydeck's `Deck` class (via it's `.show()` method).
28+
* Bokeh widgets (via the [jupyter_bokeh](https://github.com/bokeh/jupyter_bokeh) package).
29+
* Bokeh widgets are a bit of a special case, as they require some extra setup to work in Shiny. See the [Bokeh widgets aren't displaying, what gives?](#bokeh-widgets-arent-displaying-what-gives) section below for more details.
2930

30-
[See here](https://github.com/rstudio/py-shinywidgets/blob/main/shinywidgets/_as_widget.py) for more details on how these objects are coerced into `Widget` instances, and if you know of other packages that should be added to this list, please [let us know](https://github.com/rstudio/py-shinywidgets/issues/new)
31+
[See here](https://github.com/rstudio/py-shinywidgets/blob/main/shinywidgets/_as_widget.py) for more details on how these objects are coerced into `Widget` instances, and if you know of other packages that should be added to this list, please [let us know](https://github.com/rstudio/py-shinywidgets/issues/new).
3132

32-
### Bokeh widgets aren't displaying, what gives?
33+
### Bokeh setup
3334

3435
Similar to how you have to run `bokeh.io.output_notebook()` to run `{bokeh}` in notebook, you also have to explicitly bring the JS/CSS dependencies to the Shiny app, which can be done this way:
3536

@@ -94,26 +95,6 @@ that widget requires initialization code in a notebook environment. In this case
9495
`{shinywidgets}` probably won't work without providing the equivalent setup information to
9596
Shiny. Some known cases of this are:
9697

97-
#### bokeh
98-
99-
To use `{bokeh}` in notebook, you have to run `bokeh.io.output_notebook()`. The
100-
equivalent thing in Shiny is to include the following in the UI definition:
101-
102-
```py
103-
from shiny import ui
104-
from shinywidgets import bokeh_dependencies
105-
106-
app_ui = ui.page_fluid(
107-
bokeh_dependencies(),
108-
# ...
109-
)
110-
```
111-
112-
113-
#### Other widgets?
114-
115-
Know of another widget that requires initialization code? [Please let us know about
116-
it](https://github.com/rstudio/py-shinywidgets/issues/new)!
11798

11899
## Development
119100

shinywidgets/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
__author__ = """Carson Sievert"""
44
__email__ = "[email protected]"
5-
__version__ = "0.2.2"
5+
__version__ = "0.2.2.9000"
66

77
from ._dependencies import bokeh_dependency
8-
from ._shinywidgets import output_widget, reactive_read, register_widget, render_widget, as_widget
8+
from ._shinywidgets import (
9+
as_widget,
10+
output_widget,
11+
reactive_read,
12+
register_widget,
13+
render_widget,
14+
)
915

1016
__all__ = ("output_widget", "register_widget", "render_widget", "reactive_read", "bokeh_dependency", "as_widget")

shinywidgets/_as_widget.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,13 @@ def as_widget(x: object) -> Widget:
3131

3232
def as_widget_altair(x: object) -> Optional[Widget]:
3333
try:
34-
from vega.widget import VegaWidget
34+
from altair import JupyterChart
3535
except ImportError:
36-
raise ImportError("Install the vega package to use altair with shinywidgets.")
37-
38-
if not hasattr(x, "to_dict"):
39-
raise TypeError(
40-
f"Don't know how to coerce {x} (an altair object) into an ipywidget without a .to_dict() method."
36+
raise RuntimeError(
37+
"Failed to import altair.JupyterChart (do you need to pip install -U altair?)"
4138
)
4239

43-
try:
44-
return VegaWidget(x.to_dict()) # type: ignore
45-
except Exception as e:
46-
raise RuntimeError(f"Failed to coerce {x} into a VegaWidget: {e}")
40+
return JupyterChart(x) # type: ignore
4741

4842

4943
def as_widget_bokeh(x: object) -> Optional[Widget]:

0 commit comments

Comments
 (0)