streamlit.maidr_html
widget.streamlit.maidr_html(
plot=None
*
use_cdn=None
hover_mode=None
_stacklevel=3
)Return an accessible chart as a self-contained HTML string.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| plot | Any | The plot to render – a matplotlib or seaborn artist, a Plotly Figure, a Bokeh figure or layout, or an Altair chart. None uses the current matplotlib figure, and warns: that figure is process-global, and Streamlit runs sessions on separate threads, so by the time it is rendered it may be another session’s. Pass the plot explicitly. |
None |
| use_cdn | bool, {"auto"}, or None | Where the chart loads maidr.js from; see :func:maidr.render. None defers to the process-wide default. |
None |
| hover_mode | ('pointermove', 'click', 'off') | The chart’s starting value for the reader’s Hover Mode setting; see :func:maidr.render. None leaves maidr.js’s default. |
"pointermove" |
| _stacklevel | int | Internal. Frames to skip when warning, so a warning points at the caller’s own line; :func:render_maidr raises it by one because it sits a frame further out. Not part of the public API – the underscore is the only thing stopping an IDE from offering it. |
3 |
Returns
| Name | Type | Description |
|---|---|---|
| str | A complete HTML fragment for one iframe. An unchanged chart is the same string on every call, which is what lets Streamlit keep its frame across a rerun (the module notes say what that keeps, and why a Bokeh chart is the exception). Its ids are derived from the chart, so they are unique within the string but not between two calls for the same chart: give each its own frame. |
Notes
Exists as its own entry point so the string can be cached, which spares rendering the chart again when Streamlit reruns the whole script on every widget interaction. Except for a Bokeh chart, the frame no longer depends on it – an unchanged chart’s frame is kept either way – so this is about what the render costs, not about the reader::
@st.cache_data
def chart_html(_fig, key):
return maidr_html(_fig)
html = chart_html(fig, key=selected_day)
Both arguments are load-bearing. The underscore on _fig tells Streamlit not to hash it, which a matplotlib Figure does not support – and key is then the only thing left to hash. Without it every argument is skipped, the cache key is constant, and the first chart is returned for the rest of the session: a chart that silently stops matching its own controls.
Under use_cdn=False the ~1.9 MB bundle is embedded in the string. Serializing to HTML is what makes an embed possible at all, and it drops :class:htmltools.HTMLDependency children on the way, so a reference to the bundle would not survive; the source itself has to.