jupyter / jupyter/notebook

javascript magic shouldn't execute on nb load

Open
#4,530 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
13.3k
Forks
5.8k
Avg merge
6d 11h
Merged PRs (30d)
7

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.