Skip to content

Interactive plots: Access to current cell output (CURRENT_HTML_OUTPUT_ELEMENT) from python possible? #142

Open
@jeffreyfq

Description

Hi,

I figured out that it is indeed possible to include interactive matplotlib plots,
I do it like this:

from matplotlib.backends.html5_canvas_backend import FigureCanvasHTMLCanvas
from matplotlib.backends.html5_canvas_backend import FigureCanvasWasm
import pyodide
from js import document, window

resultelement = document.createElement("div");
def create_root_element(self=None):
  el = document.createElement("div");
  resultelement.appendChild(el)
  return el

# this has no effect, I change the method for each canvas down below...  
FigureCanvasWasm.create_root_element=create_root_element

# plot figure as usual
plt.figure()
plt.scatter(... plot something)
plt.title("data")
matplotlib.pyplot.gcf().canvas.create_root_element=create_root_element
cv = matplotlib.pyplot.gcf().canvas
cv.draw()
cv.show()

#... even multiple plots possible, all collected in div resultelement

# this is necessary to show the figures created and collected in this div...
resultelement

I just have two problems:

  1. The interactive figure controls are shown, but not the data (plot is white), I need to click on it to force a refresh of the plot.
    Question: What would be the best option to enforce a refresh of the interactive plot after/while cell is/was executed?

  2. I have to put resultelement at the end of the cell, it is ok, but it would be nice if I could attach the div resultelement manually in the function create_root_element or similar.
    Question: Is there a way to attach a div to the results of the cell without returning the object in the last line of the cell. Internally the global variable CURRENT_HTML_OUTPUT_ELEMENT is used for this, can I access this somehow from the python code? Can I access the current cell ID from python somehow?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions