Getting JS to work in a notebook. #1
thorwhalen
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Links
A Simple Pattern for Embedding Third Party Javascript Generated Graphics in Jupyter Notebooks
StackOverflow: How do I call a Javascript function from Python?
js2py
A GPT conversation about calling JS from a notebook
I had the oddest insane-making bug running some simple JS code in a notebook.
My function worked (behavior X) the first time it was called, but if I ran the exact same code again in a different cell, it didn't give me the same behavior (behavior Y). The first cell gave me systematically behavior X, but the second systematically behavior Y.
Some higher power was at play.
This is that story. A story where GPT-4 saved the day (but not immediately).
See the final, correct code, and how it behaves, in the "Calling JS mermaid from python, in a notebook" section of this notebook
html not even working
Me
I made an html file with the following content:
I'm expecting to see an image of a diagram, generated by the mermaid script, but instead I'm just getting the text:
Troubleshoot.
GPT-4
The issue here might be because of the incorrect usage of the mermaid.initialize function.
You should use it as mermaid.initialize({startOnLoad: true}); instead of mermaid.initialize({{ startOnLoad: true }});.
Also, to render the diagrams, you need to call mermaid.init after initializing.
Here is the corrected code:
You can use mermaid.init to initialize a specific element or a collection of elements, by passing the element or collection as the second argument to mermaid.init. The undefined as the first argument to mermaid.init means that it uses the default configuration.
Please note that this script will not work if you open the HTML file directly in your browser due to CORS policy. You must serve your HTML file from a server. For local development, you can use a simple server like Python's built-in HTTP server. If you have Python installed, you can start a simple HTTP server by running python -m http.server in the directory containing your HTML file, and then access your file at http://localhost:8000/your-file.html.
IPython
display
functionNote that GPT-4's answer here wasn't really the problem, but it is good to know of the
display
function if you don't.Me
I wrote this python code and executed in a jupyter notebook cell:
When I execute this in the next cell:
I see my graph image. The code has been rendered.
But when I copy that exact same code in the cell after that and execute, I do get the
IPython.core.display.HTML
object I'm expecting, but it is not rendered. I don't see the image.Why?
GPT-4
The issue you're encountering is likely due to the way Jupyter Notebook renders output. When you execute a cell that returns an
IPython.core.display.HTML
object, the notebook will render the HTML content automatically. However, when you copy the same code to another cell and execute it, the HTML object is not automatically rendered. You need to explicitly display the HTML object using the display function from theIPython.display
module.Here's how you can modify your code to display the mermaid graph properly in both cases:
Now, when you copy this code to another cell and execute it, the mermaid graph should be displayed as expected. The display function is used to explicitly display the HTML object, ensuring that the notebook renders the HTML content in both cases.
DOMContentLoaded
Again, GPT-4 didn't repair the problem with the following proposed solution, but it's included in the final solution. I've tried without it in the final solution, and it still works, but perhaps it's good to know about anyway, so including here.
Me
Actually, I still get the same effect here.
I still get my graph displayed in the first cell, but it renders the text in the second one.
GPT-4
Apologies for the confusion earlier. It seems that the issue is related to the execution order of the JavaScript code and the rendering of the HTML content in the Jupyter Notebook.
When the code runs for the first time, the JavaScript code is executed, and then the HTML content is rendered. However, when you run the code again in another cell, the JavaScript code might not have a chance to execute before the HTML content is displayed.
To fix this issue, you can modify the JavaScript code to use the DOMContentLoaded event. This ensures that the JavaScript code runs only after the HTML content has been fully loaded.
Here's the updated
_mermaid_display_template
:Now, when you run the following code in multiple cells, the mermaid graph should display correctly:
The
initMermaid
function is defined as an async function and is called when theDOMContentLoaded
event is triggered, or immediately if the document is already loaded. This ensures that the JavaScript code is executed after the HTML content has been rendered, which should resolve the issue you were experiencing.unique identifiers
Me
It still doesn't work.
GPT-4
I apologize for the inconvenience. Upon further investigation, it seems that the issue is related to the fact that the Jupyter Notebook loads each cell's output into an iframe. The
document.querySelector('.mermaid')
in the JavaScript code may not select the correct element across different cells.A solution to this issue is to use a unique identifier for each mermaid graph and update the JavaScript code to select the element using the unique identifier. Here's the updated code:
Now, when you run the following code in multiple cells, the mermaid graph should display correctly:
The
uuid.uuid4()
function is used to generate a unique identifier for each mermaid graph, and the JavaScript code is updated to use this unique identifier when selecting the element to render the graph. This should resolve the issue you were experiencing with displaying the mermaid graph in multiple cells.Beta Was this translation helpful? Give feedback.
All reactions