API Reference

Integrations

NiceGUI

Install the optional integration with uv add "mplbed[nicegui]".

mplbed.integration.nicegui.matplotlib(*, prevent_default_navigation=False, **figure_kwargs)

Create an interactive Matplotlib element with an owned figure.

Parameters:
  • prevent_default_navigation (bool) – Whether to prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events. Defaults to False.

  • **figure_kwargs (Any) – Keyword arguments forwarded to matplotlib.figure.Figure.

Returns:

The newly created NiceGUI element.

Return type:

Matplotlib

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 before ui.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 under prefix.

  • 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’.

  • 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.

  • 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.

mplbed.integration.starlette.figure_standalone(fig, **kwargs)

Create a Starlette Response object containing the HTML for the given figure using a function taking and returning raw strings.

Example

def my_handler(request):

fig = Figure() return figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

async mplbed.integration.starlette.figure_standalone_async(fig, **kwargs)

Create a Starlette Response object containing the HTML for the given figure using a function taking and returning raw strings.

Example

async def my_handler(request):

fig = Figure() return await figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

async mplbed.integration.starlette.figure_standalone_jinja(fig, **kwargs)

Create a Starlette Response object containing the HTML for the given figure using a Jinja template.

Example

async def my_handler(request):

fig = Figure() return await figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (str) – The path to a Jinja template which will be passed the template variables mplbed_head and fig.

  • app (Starlette, optional) – The Starlette app to use for rendering the Jinja template. If not provided, the app passed to setup(…) and/or MplbedMiddleware will be used. Typically this does not need to be provided.

mplbed.integration.starlette.figure_page(inner=None, **figure_page_kwargs)

Decorator to create a Starlette Response object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone, e.g. @figure_page(template=...).

All usages are generic with regards to async/sync

Internally this decorator uses the function figure_standalone to generate the HTML and create the Starlette Response.

Example

Usage 1:

@figure_page
(async)  def my_handler(request):
    return Figure()

Usage 2:

(async)  def my_handler(request):
    # ...
    @figure_page
    (async)  def create_figure():
        return Figure()
    return (await)  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.starlette.figure_page_async(inner=None, **figure_page_kwargs)

Decorator to create a Starlette Response object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone_async, e.g. @figure_page(template=...).

Internally this decorator uses the function figure_standalone_async to generate the HTML and create the Starlette Response.

Example

Usage 1:

@figure_page
 def my_handler(request):
    return Figure()

Usage 2:

def my_handler(request):
   # ...
   @figure_page
    def create_figure():
       return Figure()
   return  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.starlette.figure_page_jinja(inner=None, **figure_page_kwargs)

Decorator to create a Starlette Response object containing the HTML for the given figure using a Jinja template.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone_jinja, e.g. @figure_page(template=...).

Internally this decorator uses the function figure_standalone_jinja to generate the HTML and create the Starlette Response.

Example

Usage 1:

@figure_page
 def my_handler(request):
    return Figure()

Usage 2:

def my_handler(request):
   # ...
   @figure_page
    def create_figure():
       return Figure()
   return  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (str) – The path to a Jinja template which will be passed the template variables mplbed_head and fig.

  • app (Starlette, optional) – The Starlette app to use for rendering the Jinja template. If not provided, the app passed to setup(…) and/or MplbedMiddleware will be used. Typically this does not need to be provided.

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’.

  • 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.

  • 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.figure_standalone(fig, **kwargs)

Create a Quart Response object containing the HTML for the given figure using a function taking and returning raw strings.

Example

def my_handler(request):

fig = Figure() return figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

async mplbed.integration.quart.figure_standalone_async(fig, **kwargs)

Create a Quart Response object containing the HTML for the given figure using a function taking and returning raw strings.

Example

async def my_handler(request):

fig = Figure() return await figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

async mplbed.integration.quart.figure_standalone_jinja(fig, **kwargs)

Create a Quart Response object containing the HTML for the given figure using a Jinja template.

Example

async def my_handler(request):

fig = Figure() return await figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (str) – The path to a Jinja template which will be passed the template variables mplbed_head and fig.

  • app (Quart, optional) – The Quart app to use for rendering the Jinja template. If not provided, the app passed to setup(…) and/or MplbedMiddleware will be used. Typically this does not need to be provided.

mplbed.integration.quart.figure_page(inner=None, **figure_page_kwargs)

Decorator to create a Quart Response object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone, e.g. @figure_page(template=...).

All usages are generic with regards to async/sync

Internally this decorator uses the function figure_standalone to generate the HTML and create the Quart Response.

Example

Usage 1:

@figure_page
(async)  def my_handler(request):
    return Figure()

Usage 2:

(async)  def my_handler(request):
    # ...
    @figure_page
    (async)  def create_figure():
        return Figure()
    return (await)  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.quart.figure_page_async(inner=None, **figure_page_kwargs)

Decorator to create a Quart Response object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone_async, e.g. @figure_page(template=...).

Internally this decorator uses the function figure_standalone_async to generate the HTML and create the Quart Response.

Example

Usage 1:

@figure_page
 def my_handler(request):
    return Figure()

Usage 2:

def my_handler(request):
   # ...
   @figure_page
    def create_figure():
       return Figure()
   return  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.quart.figure_page_jinja(inner=None, **figure_page_kwargs)

Decorator to create a Quart Response object containing the HTML for the given figure using a Jinja template.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone_jinja, e.g. @figure_page(template=...).

Internally this decorator uses the function figure_standalone_jinja to generate the HTML and create the Quart Response.

Example

Usage 1:

@figure_page
 def my_handler(request):
    return Figure()

Usage 2:

def my_handler(request):
   # ...
   @figure_page
    def create_figure():
       return Figure()
   return  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (str) – The path to a Jinja template which will be passed the template variables mplbed_head and fig.

  • app (Quart, optional) – The Quart app to use for rendering the Jinja template. If not provided, the app passed to setup(…) and/or MplbedMiddleware will be used. Typically this does not need to be provided.

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_MODULE to 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 application in your asgi.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_processors option 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_MODULE to 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’.

  • 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.

  • 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.figure_standalone(fig, **kwargs)

Create a Django HttpResponse object containing the HTML for the given figure using a function taking and returning raw strings.

Example

def my_handler(request):

fig = Figure() return figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

async mplbed.integration.django.figure_standalone_async(fig, **kwargs)

Create a Django HttpResponse object containing the HTML for the given figure using a function taking and returning raw strings.

Example

async def my_handler(request):

fig = Figure() return await figure_page(fig)

Parameters:
  • fig (matplotlib.figure.Figure or callable) – A matplotlib Figure object or a callable that returns a Figure object.

  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.django.figure_page(inner=None, **figure_page_kwargs)

Decorator to create a Django HttpResponse object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone, e.g. @figure_page(template=...).

All usages are generic with regards to async/sync

Internally this decorator uses the function figure_standalone to generate the HTML and create the Django HttpResponse.

Example

Usage 1:

@figure_page
(async)  def my_handler(request):
    return Figure()

Usage 2:

(async)  def my_handler(request):
    # ...
    @figure_page
    (async)  def create_figure():
        return Figure()
    return (await)  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

mplbed.integration.django.figure_page_async(inner=None, **figure_page_kwargs)

Decorator to create a Django HttpResponse object containing the HTML for the given figure using a function taking and returning raw strings.

This function can be called in three ways:

  1. As a decorator for a function taking any number of arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for view/handler function.

  2. As a decorator for a function taking no arguments and returning a matplotlib Figure object, making it appropriate to use as a decorator for a figure closure within a view/handler.

  3. As a decorator factory taking keyword arguments to pass through to the underlying function figure_standalone_async, e.g. @figure_page(template=...).

Internally this decorator uses the function figure_standalone_async to generate the HTML and create the Django HttpResponse.

Example

Usage 1:

@figure_page
 def my_handler(request):
    return Figure()

Usage 2:

def my_handler(request):
   # ...
   @figure_page
    def create_figure():
       return Figure()
   return  create_figure()
Parameters:
  • prevent_default_navigation (bool, optional) – Prevent the browser’s default scrolling behavior for wheel, Arrow, PageUp/PageDown, Home/End, and Space events directed at the plot. Events are still sent to Matplotlib. Default is False.

  • template (callable, optional) – A callable taking the raw strings head, title and fig as keyword arguments and returning the HTML template to use for rendering the figure page as a raw string. Will default to default_figure_page_template if not provided.

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: object

ASGI 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, prevent_default_navigation=False)

Generate embeddable HTML for an existing figure manager.

Parameters:
  • fig_id – The server identifier returned by add_manager, independent of the Matplotlib figure number.

  • target – Where to create the figure: "inline", "body", or "modal".

  • on_close – Browser behavior to use when the figure WebSocket closes.

  • prefix_and_app – Optional explicit mplbed URL prefix and ASGI application pair.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

Returns:

The figure’s HTML container and setup script.

Return type:

str

mplbed.html.raw.figure_html(figure, *retains, target='inline', on_close='msg_discrete', prevent_default_navigation=False)

Register and generate embeddable HTML for a Matplotlib figure.

Parameters:
  • figure – The Matplotlib figure to embed.

  • *retains – Objects to keep alive with the figure manager.

  • target – Where to create the figure: "inline", "body", or "modal".

  • on_close – Browser behavior to use when the figure WebSocket closes.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

Returns:

The figure’s HTML container and setup script.

Return type:

str

mplbed.html.raw.head_content(*, core=False, prefix_and_app=None)
mplbed.html.raw.figure_page_html(fig, *, template=<function default_figure_page_template>, prevent_default_navigation=False)

Generate a complete HTML page containing a Matplotlib figure.

Parameters:
  • fig – The Matplotlib figure to embed.

  • template – A callable receiving the head, title, and fig HTML strings.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

Returns:

A complete HTML page.

Return type:

str

mplbed.html.raw.default_figure_page_template(*, head, fig, title)

Applies the following template to the given head, fig and title:

<!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(fig_id, *, target='inline', on_close='msg_discrete', prefix_and_app=None, prevent_default_navigation=False)

Generate embeddable HTML for an existing figure manager.

Parameters:
  • fig_id – The server identifier returned by add_manager, independent of the Matplotlib figure number.

  • target – Where to create the figure: "inline", "body", or "modal".

  • on_close – Browser behavior to use when the figure WebSocket closes.

  • prefix_and_app – Optional explicit mplbed URL prefix and ASGI application pair.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

Returns:

The figure’s HTML container and setup script.

Return type:

str

mplbed.html.safe.figure_html(figure, *retains, target='inline', on_close='msg_discrete', prevent_default_navigation=False)

Register and generate embeddable HTML for a Matplotlib figure.

Parameters:
  • figure – The Matplotlib figure to embed.

  • *retains – Objects to keep alive with the figure manager.

  • target – Where to create the figure: "inline", "body", or "modal".

  • on_close – Browser behavior to use when the figure WebSocket closes.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

Returns:

The figure’s HTML container and setup script.

Return type:

str

mplbed.html.safe.head_content(*, core=False, prefix_and_app=None)

App Factory

mplbed_app_factory accepts shared browser request-flow options. Resize requests default to one request in flight, so repeated observations retain only the latest pending non-zero size until rendering completes, even when input events are queued between observations. Pending refresh requests likewise coalesce to the latest request. Set resize_max_in_flight to another positive integer only when experimenting with additional concurrent resize work.

While the navigation toolbar is in pan mode, button-held motion uses the same completion-controlled flow: one motion is rendered at a time and only the latest pending position is retained.

motion_throttle_ms and scroll_throttle_ms default to None, preserving non-pan motion and scroll callbacks and their current ordering. A positive integer enables independent leading-and-trailing throttling in milliseconds. Motion callbacks then receive the latest sampled state, while scroll callbacks receive summed steps with the latest coordinates and modifiers. Zero and negative values are invalid. Pass these options through an integration’s mplbed_starlette_app_kwargs, or create and supply a configured app directly.

mplbed.server.mplbed_app_factory(*, resize_max_in_flight=1, motion_throttle_ms=None, scroll_throttle_ms=None)

Create the Starlette backend app for WebAgg or WebAggExt.

Parameters:
  • resize_max_in_flight (int) – Maximum resize requests awaiting completion. Must be positive; the default is one.

  • motion_throttle_ms (int | None) – Leading-and-trailing throttle interval for the corresponding browser input stream. None (the default) preserves every event. Positive values opt into sampled motion callbacks or aggregated scroll steps.

  • scroll_throttle_ms (int | None) – Leading-and-trailing throttle interval for the corresponding browser input stream. None (the default) preserves every event. Positive values opt into sampled motion callbacks or aggregated scroll steps.

Return type:

Starlette

Backend

class mplbed.webaggext.FigureCollector(*, target, on_close, prevent_default_navigation=False, global_scope=False)

Bases: object

Collect HTML generated by pyplot.show within a context.

Collected figures leave pyplot’s registry on exit but keep independent server IDs, so later collectors can safely reuse pyplot figure numbers.

Parameters:
  • target – Where the collected figures should be created in the page.

  • on_close – Browser behavior to use when a figure WebSocket closes.

  • prevent_default_navigation – Prevent browser scrolling for wheel, Arrow, PageUp/PageDown, Home/End, and Space events while still forwarding them to Matplotlib.

  • global_scope – Whether to use the process-global collection instead of context-local collection.

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.