render_panel
render_panel class
Reactively render a panel object.
Usage Source
render_panel(
_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_panel is a decorator that creates a render function – a function that returns the panel object to display and re-runs when the reactive values it reads change:
import panel as pn
from shinywidgets import render_panel
pn.extension()
@render_panel
def hello():
return pn.pane.Markdown("# Hello, panel!")The render function may return any object that pn.panel() accepts (e.g., a pane, widget, layout, pn.viewable.Viewer, string, DataFrame, or figure), which is rendered through bokeh (requires pip install 'shinywidgets[panel]'), or the underlying jupyter_bokeh.BokehModel widget. Panel callbacks (e.g., pn.bind(), .param.watch(), and on_click()) run in the Shiny server, without a Panel server.
Note
Accessing the rendered widget: the render function gains two read-only attributes: .value, the object it most recently returned, and .widget, the rendered BokehModel instance. .value is the return value exactly as returned – to get a Panel object with a parameter interface, return a pane or widget explicitly rather than a raw value like a DataFrame.
Responding to user interaction: Panel parameters aren’t widget traits, so reactive_read() can’t see them. Instead, the output has a param(name) method that reads the rendered object’s name parameter reactively, much like input.my_slider() reads an input. Call it in a reactive function (a @render.* function, @reactive.calc, or @reactive.effect), which then re-runs whenever the parameter changes – from browser interaction or from Python::
@render_panel
def slider():
return pn.widgets.IntSlider(value=5, start=0, end=10)
@render.text
def value():
return str(slider.param("value"))
How it behaves:
- It’s seeded with the parameter’s value at render, and after a re-render it follows the new
.valueobject. - It raises if
.valueisn’t aparam.Parameterized(e.g., the render function returned a raw DataFrame or string) or has nonameparameter. - It can’t be read inside this output’s own render function, which builds a new object each time it runs.
- To limit how often a dragged slider (or another continuously changing parameter) re-runs readers, read Panel’s throttled variant of the parameter instead (e.g.,
slider.param("value_throttled")).
For anything more involved, .param.watch() on the object works too, and may set reactive.values.
Mutating the widget: to update the output without re-rendering, modify .value in place (e.g., table.value.value = df).
Some other things to know:
- Set
sizing_mode="stretch_both"on the panel object to fill the output. - Call
pn.extension()with the extensions you use (e.g.,pn.extension("tabulator")) at the top of the app, especially for components that may first appear after the initial render (e.g., insidepn.bind()). Otherwise they render blank. - Create panel objects per session (e.g., inside the render function), not at the top level of a Shiny Core app, where every visitor would share them (and see each other’s changes).
- Panel templates are full pages, so they can’t be rendered in an output; use Shiny’s page layouts instead.
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 (seefillandfillable). fill : Optional[bool] = None-
Whether the output should grow or shrink to fit its parent container. When
None(the default), this isTrueifheightis not specified. fillable : Optional[bool] = None-
Whether the output container should fill available space (scrolling its contents on overflow). When
None(the default), this isTrueifheightis not specified.
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).
Methods
param() |
The current value of the rendered object’s name parameter (e.g., slider.param("value") for a pn.widgets.IntSlider), read reactively: a reactive function that reads it re-runs whenever the parameter changes, from browser interaction or from Python. Seeded with the parameter’s value at render; follows the new .value object on re-render. Raises if .value isn’t a param.Parameterized or has no name parameter. |
param()
render_panel.param(name: str)The current value of the rendered object’s name parameter (e.g., slider.param("value") for a pn.widgets.IntSlider), read reactively: a reactive function that reads it re-runs whenever the parameter changes, from browser interaction or from Python. Seeded with the parameter’s value at render; follows the new .value object on re-render. Raises if .value isn’t a param.Parameterized or has no name parameter.