Skip to content

a better method for configuring where matplotlib figures are added to the DOM #19

Open
@kmdupr33

Description

@kmdupr33

Currently, matplotlib figures are placed in an element returned by FigureCanvasWasm.create_root_element. Our options for modifying this behavior are limited, and I'd like to propose something better.

Current options

@personalizedrefrigerator, proposed here that we can build pyodide ourselves and subclass FigureCanvasWasm to get the behavior we want.

@bgailleton demonstrated a monkey-patch-based approach here where he modifies create_root_element on a Canvas instance. (@gzuidhof does something similar here in starboard-notebook)

A better approach?

FigureCanvasWasm.create_root_element can return js.document.pyodideMatplotlibPlotTarget if it refers to a DOM element and fallback on its default behavior of creating an unattached div if that element isn't present. The code would look something like this:

def create_root_element(self):
  if document.pyodideMatplotlibPlotTarget:
    return document.pyodideMatplotlibPlotTarget
  else:
    return document.createElement("div")

With this in place, clients could control where plots are rendered simply by populating document.pyodideMatplotlibPlotTarget. I think this is much easier than doing a custom build of pyodide and much cleaner than monkey-patching instances. If we did this, we also wouldn't need to have subclasses override the method. They could control where plots are rendered in the same way that external clients do. I'm happy to contribute a PR if we think this is a good approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions