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

javascript magic shouldn't execute on nb load #4530

Open
stas00 opened this issue Mar 29, 2019 · 2 comments
Open

javascript magic shouldn't execute on nb load #4530

stas00 opened this issue Mar 29, 2019 · 2 comments

Comments

@stas00
Copy link

stas00 commented Mar 29, 2019

Create nb with a cell:

%%javascript
IPython.notebook.kernel.execute('')

Now save the notebook, close it and then re-open it. When you do that, under that cell suddenly you will see an error in red:

Javascript error adding output!
TypeError: Cannot read property 'execute' of null
See your browser Javascript console for more details.

That means the browser has parsed some js code and it tried to run it. This is the error in chrome, it will probably different in a different browser.

Perhaps a more demonstrative example is:

%%javascript
alert('Shoud not run')

and yet I get an alert pop up as soon as I open the nb in jupyter notebook.

Why do we have the js magic run by the browser?

Moreover this problem happens even if I don't use %%javascript, but delegate it to a function:

from IPython.display import display, Javascript
def my_code():
    display(Javascript("alert('Shoud not run')"))

and then have just:

my_code()

it still pops the alert on nb load.

I found a workaround that prevents the whole js code from running on nb load:

%%javascript 
if (IPython.notebook.kernel) {
    IPython.notebook.kernel.execute('')
}

It works since IPython.notebook.kernel is not yet defined when the nb just loads.

work environment:
Server 5.7.6
Python 3.7.2
IPython 7.3.0
and then also tested with the latest with the same results.

Thanks.

@minrk
Copy link
Member

minrk commented Apr 1, 2019

Why do we have the js magic run by the browser?

The js magic is literally doing:

display(Javascript("cell_contents"))

It is executed by the kernel. However, once the output is displayed and stored in the document, it is the javascript output that is evaluated on page load.

We could think about delaying this rendering until kernels are ready, but this would be worse for most javascript and html outputs that don't talk to the kernel. A futures-based approach that probably works best in JupyterLab is an alternative.

@stas00
Copy link
Author

stas00 commented Apr 1, 2019

Perhaps my workaround could be integrated into it? So when it's stored in the document it's wrapped with:

if (IPython.notebook.kernel) { <body of js magic> }

and thus it won't run on load. It's unlikely that a user will want js to run in the notebook automatically on load, no? Perhaps this assumption is wrong and then this idea is not a good one.

Second best is to document this workaround inside the js magic document?

Thank you.

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

2 participants