API Reference¶
Integrations¶
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’.
0x7f368bb32140> (<function <lambda> at)
0x7f368bb321f0> (<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’.
0x7f368bb32140> (<function <lambda> at)
0x7f368bb321f0> (<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
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¶
- 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.