jupyter-book / mystmd

Command line tools for working with MyST Markdown.
https://mystmd.org/guide
MIT License
191 stars 62 forks source link

trouble with programatically injecting HTML with ipywidgets #1264

Open parmentelat opened 3 months ago

parmentelat commented 3 months ago

Hey

I'm facing an issue when porting the following page from jb1

specifically I have a IPython extension ipythontutor that streamlines the insertion of an iframe with pythontutor illustrating the Python code, by calling ipywidgets.HTML()

jb1 is able to do behave as expected and to produce an iframe (see below)

but when rendered by myst, I am getting an error message (see in context below) that says

a Jupyter kernel connection is required ... HTML(...)


trying to understand better I then added (the second half of the test notebook) a simple call to HTML() and that one goes through properly in myst, so I guess the next step would be to narrow this down a bit, but before I do I could really use a better understanding of why a kernel would be needed at all at read-time in this scenario ?!? particularly as jb1 behaves as expected

PS. after writing this I realize that in this specific instance maybe I could/should have used IPython.display.HTML instead; however I have a few other similar discrepancies as soon as ipywidgets are involved, so I guess this issue is still worth discussing, even if only from a more general standpoint

here's jb1 output ![image](https://github.com/executablebooks/mystmd/assets/4519227/3dec9cda-4886-40a8-9a50-c49de8fae686) see also
and here's the output I get from `myst` ![image](https://github.com/executablebooks/mystmd/assets/4519227/c40dae81-cc53-4ab8-8b94-9edfc42e1ffb)
the code of the IPython magic https://github.com/parmentelat/ipythontutor/blob/main/ipythontutor.py
parmentelat commented 3 months ago

and indeed I have just checked that using HTML() from ipywidgets indeed fails with the simple table:

image

but my general question still stands I guess...

stevejpurves commented 3 months ago

@parmentelat rendering of general ipywidgets without a kernel is a not implemented yet issue in myst and something we should probably begin tackling.

The message you are seeing is displayed by myst-theme whenever an ipywidgets mimetype is encountered.

Saying that display.HTML() should just add to the output's mime bundle adding a text/html key, and if that is the case then the myst-theme is probably being too conservative and we should just display html if it's there, even when there's an ipywidgets mimietype in the same bundle.

Do you have a repo with your example in to take a look at?

(Some background on why this message is shown: mystmd's unpacking of a notebook is limited to lifting out an array of cells and it does nothing right now to resolve or make resolvable the ipywidgets state data that (might be) is stored in the notebook metadata section and referenced in cell metadata. We need to implement proper lifting and resolving of the ipywidgets state - with a strategy that makes the state available while avoiding bloat in the main mdast.json, widgets state can become very large)

parmentelat commented 3 months ago

hey @stevejpurves thanks for your feedback

this notebook here https://github.com/parmentelat/jupyterlab-examples/blob/main/notebooks/1-06-pythontutor-nb.md

is rendered with jb1 here, where it works fine https://jupyterlab-examples.readthedocs.io/en/main/1-06-pythontutor-nb.html

the code for the magic is here https://github.com/parmentelat/ipythontutor/blob/main/ipythontutor.py I wrote this magic a very long time ago, and upon reviewing it again the other day I realized it probably could have been done without ipywidgets at all (the optional buttons are seldom used in practice), but well, that's how it is...

I have plenty of other examples that do use ipywidgets in a more meaningful way, so regardless of this example I would love to see this supported in myst :)

PS. I think I might have been influenced by this page in the docs: after seeing the first interactive example, I might have unconsciously concluded that I could build interactivity within myst; apparently it's not that simple :)

thanks !