render_widget

render_widget class

Reactively render an ipywidget.

Usage Source

render_widget(
    _fn: Optional[ValueFn[ValueT]] = None,
    *,
    width: Optional[str] = None,
    height: Optional[str] = None,
    fill: Optional[bool] = None,
    fillable: Optional[bool] = None,
    rate_policy: Optional[RatePolicy] = None,
    rate_delay: Optional[float] = None,
)

@render_widget is a decorator that creates a render function – a function that returns the widget to display and re-runs when the reactive values it reads change:

from ipywidgets import IntSlider

from shinywidgets import render_widget


@render_widget
def slider():
    return IntSlider(value=7, min=0, max=10)

The render function should return an ipywidgets.Widget. (Other return values are passed through as_widget(), which coerces objects from several popular packages, but prefer the dedicated decorators like render_plotly for those, since they provide better type checking.)

Note

Accessing the rendered widget: the render function gains two read-only attributes: .value, the value it most recently returned, and .widget, the rendered ipywidgets.Widget instance. Widget state can also be read reactively with .read("some_trait") (a shortcut for reactive_read() on .widget).

Responding to user interaction: this renderer has no accessors; read widget traits with .read("some_trait") (above).

Mutating the widget: use .widget to update the rendered widget in place; trait changes on the widget are synced to the browser.

Parameters

width : Optional[str] = None

A CSS length unit (e.g., "400px" or "100%") for the output’s width.

height : Optional[str] = None

A CSS length unit for the output’s height. When None (the default), the output is designed to fill the height of its parent container (see fill and fillable).

fill : Optional[bool] = None

Whether the output should grow or shrink to fit its parent container. When None (the default), this is True if height is not specified.

fillable : Optional[bool] = None

Whether the output container should fill available space (scrolling its contents on overflow). When None (the default), this is True if height is not specified.

rate_policy : Optional[RatePolicy] = None

How to rate-limit accessors on this renderer that declare a default policy, and reactive_read() on its widget. None (the default) leaves each accessor’s own default in place, and debounces reactive_read() only if rate_delay is given.

rate_delay : Optional[float] = None

The rate-limit window in seconds. 0 disables rate limiting. Defaults to each accessor’s own setting (0.25 for those that declare one).

Examples

from shiny import App, ui
from shinywidgets import output_widget, render_widget
import ipywidgets as widgets

app_ui = ui.page_fluid(output_widget("slider"))


def server(input, output, session):
    @render_widget
    def slider():
        return widgets.IntSlider(value=5, min=0, max=10)


app = App(app_ui, server)

See also

output_widget(): in Shiny Core, the UI needs this to hold the output (Shiny Express displays it where the render function is defined).