API Reference¶
Integrations¶
NiceGUI¶
Install the optional integration with uv add "mplbed[nicegui]".
- class mplbed.integration.nicegui.Matplotlib(**figure_kwargs)¶
Bases:
ElementEmbed an owned Matplotlib figure as an interactive NiceGUI element.
- Parameters:
**figure_kwargs (
Any) – Keyword arguments forwarded tomatplotlib.figure.Figure.
- property figure: Figure¶
The Matplotlib figure owned by this element.
- update()¶
Schedule a redraw of the figure for connected clients.
- Return type:
None
- component: Component | None = JsComponent(key='fe05174b31b42bc63644ad224ae83f8e/nicegui.js', name='nicegui', path=PosixPath('/tmp/tmp1qcrns1h/src/mplbed/integration/nicegui.js'))¶
- exposed_libraries: ClassVar[list[Library]] = []¶
- mplbed.integration.nicegui.matplotlib(**figure_kwargs)¶
Create an interactive Matplotlib element with an owned figure.
- Parameters:
**figure_kwargs (
Any) – Keyword arguments forwarded tomatplotlib.figure.Figure.- Returns:
The newly created NiceGUI element.
- Return type:
- mplbed.integration.nicegui.setup(app, *, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True, do_use_mpl_backend=True, use_webaggext_backend=True)¶
Configure a NiceGUI app for interactive mplbed elements.
- Parameters:
app (
Any) – The NiceGUI application to configure beforeui.run().prefix (
str) – URL prefix used for mplbed’s routes.mplbed_starlette_app (
Any) – An optional pre-built mplbed Starlette sub-application.mplbed_starlette_app_kwargs (
dict[str,Any] |None) – Optional arguments used to create the mplbed Starlette application.manage_routing (
bool) – Whether mplbed should route requests underprefix.do_use_mpl_backend (
bool) – Whether to select mplbed’s Matplotlib backend.use_webaggext_backend (
bool) – Whether to select WebAggExt instead of Matplotlib’s base WebAgg backend.
- Return type:
None
Starlette¶
- mplbed.integration.starlette.install_middleware(app, *, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True)¶
Install the mplbed middleware on the given Starlette app.
- Parameters:
app (Starlette) – The Starlette app to install the middleware on.
prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
mplbed_starlette_app (Starlette, optional) – The Starlette app to use for the mplbed routes, as returned by mplbed_starlette_app_factory. If not provided, a new app will be created using the provided mplbed_starlette_app_kwargs.
mplbed_starlette_app_kwargs (dict, optional) – Keyword arguments to pass to the Mplbed app factory if mplbed_starlette_app is not provided.
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
- mplbed.integration.starlette.register_context_processor(templates)¶
Register a context processor for the given Starlette Jinja2Templates instance to inject head content.
- Parameters:
templates (
Jinja2Templates) – The Jinja2Templates instance to register the context processor on.
- mplbed.integration.starlette.setup(app, *, templates=None, do_install_middleware=True, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True, do_register_context_processor=None, do_use_mpl_backend=True, use_webaggext_backend=True)¶
Setup the mplbed integration for the given Starlette app.
This function performs all the integration steps necessary to use mplbed with Starlette, including installing middleware, registering context processors, and configuring the matplotlib backend. The default parameter values are suitable for most use cases, but can be overridden as needed.
- Parameters:
app (Starlette) – The Starlette app to integrate with mplbed.
templates (
Jinja2Templates|None) – The Jinja2Templates instance to use for rendering templates. If None, no context processor will be registered. Default is None.do_install_middleware (bool, optional) – Whether to install the mplbed middleware on the given app. Default is True.
prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
0x7a0a4fd71170> (<function <lambda> at)
0x7a0a4fd71220> (<function <lambda> at)
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
do_register_context_processor (bool, optional) – Whether to register the mplbed context processor on the given app. By default this will be true when the templates parameter is set to a Jinja2Templates instance.
do_use_mpl_backend (bool, optional) – Whether to setup the matplotlib backend for rendering figures. Default is True.
use_webaggext_backend (bool, optional) – Whether to use webaggext rather than the basic webagg backend for rendering figures. Default is True.
Quart¶
- mplbed.integration.quart.install_middleware(app, *, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True)¶
Install the mplbed middleware on the given Quart app.
- Parameters:
app (Quart) – The Quart app to install the middleware on.
prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
mplbed_starlette_app (Starlette, optional) – The Starlette app to use for the mplbed routes, as returned by mplbed_starlette_app_factory. If not provided, a new app will be created using the provided mplbed_starlette_app_kwargs.
mplbed_starlette_app_kwargs (dict, optional) – Keyword arguments to pass to the Mplbed app factory if mplbed_starlette_app is not provided.
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
native_app (Any, optional) – The native app, e.g. the Starlette or Quart instance, which will typically be saved in cased it is needed by the specific integration, e.g. for rendering templates.
- mplbed.integration.quart.register_context_processor(app)¶
Register a context processor for the given Quart app to inject head content.
- Parameters:
app (Quart) – The Quart app to register the context processor on.
- mplbed.integration.quart.setup(app, *, do_install_middleware=True, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True, do_register_context_processor=True, do_use_mpl_backend=True, use_webaggext_backend=True)¶
Setup the mplbed integration for the given Quart app.
This function performs all the integration steps necessary to use mplbed with Quart, including installing middleware, registering context processors, and configuring the matplotlib backend. The default parameter values are suitable for most use cases, but can be overridden as needed.
- Parameters:
app (Quart) – The Quart app to integrate with mplbed.
do_install_middleware (bool, optional) – Whether to install the mplbed middleware on the given app. Default is True.
prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
0x7a0a4fd71170> (<function <lambda> at)
0x7a0a4fd71220> (<function <lambda> at)
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
do_register_context_processor (bool, optional) – Whether to register the mplbed context processor on the given app. Default is True.
do_use_mpl_backend (bool, optional) – Whether to setup the matplotlib backend for rendering figures. Default is True.
use_webaggext_backend (bool, optional) – Whether to use webaggext rather than the basic webagg backend for rendering figures. Default is True.
- mplbed.integration.quart.iframe_for(endpoint, *, app=None, **kwargs)¶
Generate an iframe HTML snippet for the given Quart endpoint.
- Parameters:
endpoint (str) – The name of the Quart endpoint to generate the iframe for.
app (Quart, optional) – The Quart app to use for URL generation. If not provided, the current app will be used.
**kwargs – Additional keyword arguments to pass to quart.url_for.
- Returns:
The HTML snippet for the iframe.
- Return type:
str
Django¶
- mplbed.integration.django.install_middleware(application=None, *, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True)¶
Install the mplbed middleware on the given Django ASGI application.
This is intended to be used in your project’s
asgi.py, wrapping the application returned by django.core.asgi.get_asgi_application, as described in the Django documentation for deploying with ASGI.- Parameters:
application (ASGI3Application, optional) – The Django ASGI application to install the middleware on, e.g. the result of django.core.asgi.get_asgi_application. If not provided it will be fetched via get_asgi_application, which requires
DJANGO_SETTINGS_MODULEto be set in the environment.prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
mplbed_starlette_app (Starlette, optional) – The Starlette app to use for the mplbed routes, as returned by mplbed_starlette_app_factory. If not provided, a new app will be created using the provided mplbed_starlette_app_kwargs.
mplbed_starlette_app_kwargs (dict, optional) – Keyword arguments to pass to the Mplbed app factory if mplbed_starlette_app is not provided.
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
- Returns:
The wrapped application, ready to be assigned to
applicationin yourasgi.py.- Return type:
ASGI3Application
- mplbed.integration.django.context_processor(request)¶
Return the mplbed head content as a context processor.
Add this function to the
context_processorsoption of your template backend settings to make the template variable mplbed_head available in all templates.- Parameters:
request (django.http.HttpRequest) – The request being processed.
- Returns:
A dict setting the mplbed_head template variable to the head content as a markup-safe string.
- Return type:
dict
- mplbed.integration.django.setup(application=None, *, do_install_middleware=True, prefix='/webagg', mplbed_starlette_app=None, mplbed_starlette_app_kwargs=None, manage_routing=True, do_use_mpl_backend=True, use_webaggext_backend=True)¶
Setup the mplbed integration for a Django ASGI application.
This function performs all the integration steps necessary to use mplbed with Django, including installing middleware and configuring the matplotlib backend. The default parameter values are suitable for most use cases, but can be overridden as needed.
Unlike the other integrations, the wrapped application is returned rather than mutated, so that it can be exported from your
asgi.py, e.g.:application = mplbed_django.setup(get_asgi_application())
- Parameters:
application (ASGI3Application, optional) – The Django ASGI application to integrate with mplbed, e.g. the result of django.core.asgi.get_asgi_application. If not provided it will be fetched via get_asgi_application, which requires
DJANGO_SETTINGS_MODULEto be set in the environment.do_install_middleware (bool, optional) – Whether to install the mplbed middleware on the given app. Default is True.
prefix (str, optional) – The URL prefix for the routes handleded by mplbed. Default is ‘/webagg’.
0x7a0a4fd71170> (<function <lambda> at)
0x7a0a4fd71220> (<function <lambda> at)
manage_routing (bool, optional) – Whether the ASGI middleware should manage routing. Default is True. If you set this to False, you are responsible for routing requests to the Mplbed app under the given prefix.
do_use_mpl_backend (bool, optional) – Whether to setup the matplotlib backend for rendering figures. Default is True.
use_webaggext_backend (bool, optional) – Whether to use webaggext rather than the basic webagg backend for rendering figures. Default is True.
- mplbed.integration.django.iframe_for(endpoint, **kwargs)¶
Generate an iframe HTML snippet for the given Django URL name.
- Parameters:
endpoint (str) – The name of the Django URL to generate the iframe for.
**kwargs – Additional keyword arguments to pass to django.urls.reverse.
- Returns:
The HTML snippet for the iframe.
- Return type:
markupsafe.Markup
ASGI Middleware¶
- class mplbed.asgi.MplbedMiddleware(default_app, *, prefix, app=None, app_kwargs=None, manage_routing=True, native_app=None)¶
Bases:
objectASGI middleware to sets up routing and ensures context the correct context is available so that the integrations work correctly.
- mplbed.asgi.get_native_app()¶
Get the native app from the current context.
- Returns:
The native app, or None if not set.
- Return type:
Any
- mplbed.asgi.get_asgi_app()¶
Get the ASGI app from the current context.
- Returns:
The ASGI app, or None if not set.
- Return type:
ASGI3Application | None
- mplbed.asgi.url_path_for(name, **path_params)¶
Get the URL path for a given route name and parameters.
HTML Helpers¶
Two variants are provided: mplbed.html.raw returns plain Python strings,
while mplbed.html.safe returns markupsafe.Markup objects safe for direct
use in Jinja2 templates.
Raw strings (mplbed.html.raw)¶
- mplbed.html.raw.figure_html_from_id(fig_id, *, target='inline', on_close='msg_discrete', prefix_and_app=None)¶
- mplbed.html.raw.figure_html(figure, *retains, target='inline', on_close='msg_discrete')¶
- mplbed.html.raw.head_content(*, core=False, prefix_and_app=None)¶
- mplbed.html.raw.figure_page_html(fig, *, template=<function default_figure_page_template>)¶
- mplbed.html.raw.default_figure_page_template(*, head, fig, title)¶
Applies the following template to the given
head,figandtitle:<!DOCTYPE html> <html lang="en"> <head> {head} <title>{title}</title> </head> <body> {fig} </body> </html>
Markup-safe strings (mplbed.html.safe)¶
- mplbed.html.safe.figure_html_from_id(*args, **kwargs)¶
- mplbed.html.safe.figure_html(*args, **kwargs)¶
- mplbed.html.safe.head_content(*args, **kwargs)¶
App Factory¶
- mplbed.server.mplbed_app_factory()¶
Create a Starlette app to act as the backend for webagg or webaggext.
Backend¶
- class mplbed.webaggext.FigureCollector(**kwargs)¶
Bases:
object- consume_one()¶
- consume_many(required=False)¶
- mplbed.webaggext.use(ext=True)¶
Set the matplib backend to webaggext.
- Parameters:
ext (bool, optional) – Use the webaggext backend. True by default. Set to False to use the webagg backend.