Render ipywidgets in Shiny applications
Project description
shinywidgets
Render ipywidgets inside a Shiny app.
Installation
pip install shinywidgets
Overview
See the using ipywidgets section of the Shiny for Python website.
Frequently asked questions
What ipywidgets are supported?
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.
{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:
- Altair charts (i.e.,
altair.Chart()
instances). - Plotly figures (i.e.,
plotly.go.Figure()
) - Pydeck's
Deck
class (via it's.show()
method). - Bokeh widgets (via the jupyter_bokeh package).
- 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? section below for more details.
See here 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.
Bokeh setup
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:
from shiny import ui
from shinywidgets import bokeh_dependencies
app_ui = ui.page_fluid(
bokeh_dependencies(),
# ...
)
Does {shinywidgets}
work with Shinylive?
To some extent, yes. As shown on the official shinylive examples, packages like plotly and ipyleaflet work (as long as you've provided the proper dependencies in a requirements.txt
file), but other packages like altair and qgrid may not work (at least currently) due to missing wheel files and/or dependencies with compiled code that can't be compiled to WebAssembly.
How do I size the widget?
{ipywidgets}
' Widget
class has it's own API for setting inline CSS
styles,
including height
and width
. So, given a Widget
instance w
, you should be able to
do something like:
w.layout.height = "100%"
w.layout.width = "100%"
How do I hide/show a widget?
As mentioned above, a Widget
class should have a layout
attribute, which can be
used to set all sorts of CSS styles, including display and visibility. So, if you wanted
to hide a widget and still have space allocated for it:
w.layout.visibility = "hidden"
Or, to not give it any space:
w.layout.display = "none"
Can I render widgets that contain other widgets?
Yes! In fact this a crucial aspect to how packages like {ipyleaflet}
work. In
{ipyleaflet}
's case, each individual marker is a widget which gets attached to a Map()
via .add_layer()
.
Troubleshooting
If after installing {shinywidgets}
, you have trouble rendering widgets,
first try running the "hello world" ipywidgets example. If that doesn't work, it could be that you have an unsupported version
of a dependency like {ipywidgets}
or {shiny}
.
If you can run the "hello world" example, but "3rd party" widget(s) don't work, first
check that the extension is properly configured with jupyter nbextension list
. Even if
the extension is properly configured, it still may not work right away, especially if
that widget requires initialization code in a notebook environment. In this case,
{shinywidgets}
probably won't work without providing the equivalent setup information to
Shiny. Some known cases of this are:
Development
If you want to do development on {shinywidgets}
, run:
pip install -e ".[dev,test]"
cd js && yarn watch
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file shinywidgets-0.2.4.tar.gz
.
File metadata
- Download URL: shinywidgets-0.2.4.tar.gz
- Upload date:
- Size: 1.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb7e6d6e9a58199b38f5c05caad63ec6590b259b4f87dc42d51714aa9467f807 |
|
MD5 | 5f400065ce7eb861b513193c792c2f8a |
|
BLAKE2b-256 | 3acd2964d44813887f884f5c726fa831bfc71cad41d4622609c01f4bcb922356 |
File details
Details for the file shinywidgets-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: shinywidgets-0.2.4-py3-none-any.whl
- Upload date:
- Size: 1.8 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 955450b1a2b66e73a9cc16910d9938a8bd1b23d5fe57b69b6e94f78c8ba09955 |
|
MD5 | 016e152cf51bb1868d2ef5561791ef99 |
|
BLAKE2b-256 | 5fe17a5f6d2b1c8bc7360018f4c85e6aad44abd77c5bab4394108117a28a8531 |