Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jeffreyfq opened this issue May 12, 2023 · 0 comments

Comments

@jeffreyfq
Copy link

jeffreyfq commented May 12, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant