Shiny Widgets Integration
shinywidgets facilitates the rendering and interactive use of `ipywidgets` within `Shiny for Python` applications. It bridges the gap between the rich interactive capabilities of `ipywidgets` and the reactive framework of Shiny, allowing users to embed existing widget ecosystems like `ipyleaflet`, `plotly.graph_objects.FigureWidget`, and `ipyvolume` directly into their Shiny apps. The current version is 0.8.0, with a fairly active release cadence, often addressing compatibility with new versions of `ipywidgets`, `plotly`, and `websockets`.
Common errors
-
AttributeError: object has no attribute '_repr_mimebundle_'
cause This error occurred in `shinywidgets` versions prior to 0.5.1 due to an internal handling issue with widget representation, particularly with certain `ipywidgets`.fixUpgrade `shinywidgets` to version `0.5.1` or higher: `pip install --upgrade shinywidgets`. -
Plotly graph not rendering or failing to update in Shiny app.
cause Common causes include missing `anywidget` dependency, an outdated `plotly` version (pre-6.0.0), or incompatibility with `websockets>=16.0` in `shinywidgets < 0.7.1`.fixEnsure `pip install anywidget plotly>=6.0.0` and `pip install --upgrade shinywidgets` (to at least v0.7.1) are executed. -
ipyleaflet erroring out when attempting to read the .model_id property of a closed widget object.
cause This issue was specific to `ipyleaflet` integration in `shinywidgets` versions prior to 0.5.0, related to how closed widget objects were handled.fixUpgrade `shinywidgets` to version `0.5.0` or higher: `pip install --upgrade shinywidgets`.
Warnings
- gotcha Widgets initialized inside a `reactive.effect()` are no longer automatically removed when the effect invalidates. This change requires manual management of widget lifecycle if automatic cleanup was previously relied upon.
- breaking For `altair` and `plotly` integrations, `anywidget` is now a direct dependency and must be installed separately. Additionally, `plotly` versions prior to 6.0.0 might be incompatible.
- gotcha `anywidget`-based widgets (e.g., Plotly, Altair) may fail to initialize or update correctly with `websockets` versions 16.0 or higher due to a fix introduced in `shinywidgets` v0.7.1.
- gotcha In `shinywidgets` versions before 0.7.2, widget cleanup might not occur correctly if the widget was created inside a render output's context that differs from the current context, potentially leading to resource leaks or unexpected behavior.
Install
-
pip install shinywidgets -
pip install shinywidgets ipywidgets ipyleaflet
Imports
- output_widget
from shinywidgets import output_widget
- render_widget
from shinywidgets import render_widget
Quickstart
from shiny import App, ui, render
from shinywidgets import output_widget, render_widget
import ipywidgets as widgets
app_ui = ui.page_fluid(
ui.h2('Shiny + ipywidgets'),
output_widget('my_slider'),
ui.output_text('slider_value'),
)
def server(input, output, session):
@render_widget
def my_slider():
return widgets.IntSlider(min=0, max=100, value=50, description='Slider:')
@output
@render.text
def slider_value():
return f'Slider value: {input.my_slider()}'
app = App(app_ui, server)