init_notebook

init_notebook(use_cdn=None, force=False)

Inject the bundled maidr.js / maidr-math.css into the notebook DOM.

Mirrors the plotly.offline.init_notebook_mode / bokeh.io.output_notebook pattern: load the library once at the top of the notebook instead of duplicating the ~1.7 MB bundle in every iframe srcdoc. The source strings are stashed on window.__maidrJsSource / window.__maidrMathCssSource in the parent document so that later iframe outputs can evaluate them in their own JS context without the bundle re-appearing in the notebook file.

The stylesheet stashed alongside the script is maidr-math.css, the KaTeX rules that style LaTeX in AI chat responses. It travels as a source string for the same reason the script does: an iframe rendered from srcdoc has no base URL, so maidr.js cannot fetch the file for itself the way it does on a page that loaded it over HTTP.

Parameters

Name Type Description Default
use_cdn bool, {"auto"}, or None * True: inject a <script src="{CDN}"> tag so the notebook loads the CDN copy once. * False: read the bundled maidr.js / maidr-math.css from the installed package and embed them as strings on window. * "auto": try the CDN first and fall back to the bundled source client-side. * None (default): defer to :func:get_use_cdn. None
force bool Re-inject even if :data:_NOTEBOOK_LOADED is already True. Useful in Colab where each cell renders in isolation and the parent context is reset between cells. False

Notes

No-op outside notebook environments (Environment.is_notebook() returns False). Safe to call multiple times — the guard flag prevents re-injection unless force=True.

Pins its tags to the bundled version rather than resolving one. maidr/__init__.py calls this at import, and resolving here would put a blocking network request inside import maidr — before the user has run anything, and before the documented opt-outs could be applied. A stalled DNS resolver is not reliably bounded by a socket timeout, so that wait has no dependable ceiling.

The bundled version needs no request and is still immutable, so these tags are cache-safe: emitting @latest here would have left the parent document subject to the same seven-day cache lifetime this module exists to remove. Plots themselves render in iframes that inject their own <script> at the resolved version, so the first render() / save_html() is where a lookup happens.

That split has a known, accepted cost: whenever the bundle is behind the published release — the very case :func:maidr.bundle_status exists to report — a notebook session fetches two different maidr.js builds, and this parent-document tag is a prefetch the plots do not end up using. It is still the right trade: the alternative is resolving at import time, which is the blocking request described above, and the duplicate cost falls away as soon as the bundle is refreshed at release time. Please do not “fix” this by resolving here.