10

As I understand it, I should be able to print the variable foo in the snippet below.

from IPython.display import HTML
HTML('''
    <script type="text/javascript">
        IPython.notebook.kernel.execute("foo=97")
    </script>
     ''')
print(foo)

Instead, I see this error message:

NameErrorTraceback (most recent call last)
<ipython-input-2-91b73ee49ec6> in <module>()
      5     </script>
      6      ''')
----> 7 print(foo)

NameError: name 'foo' is not defined

I'm trying to use this answer but struggling to make it work.

FWIW, this is the latest Jupyter code (according to pip) running on Fedora 23. What are the prerequisites to make this work?

1
  • The reason this doesn't work is that it will finish executing the code in the cell before it executes the new code that the Javascript has sent it. Commented May 12, 2016 at 9:37

4 Answers 4

14

This is how I made your code work: enter image description here

or even simpler:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Are there any practical differences between the two?
How is possible to call external javascript scripts?
how to generate bi-directional communication between those two?
this does not work for me once I "restart and re-run all cells" using the >> button. Seems to be a timing issue.
Does not work in JupyterLab because JavaScript does not know IPython.
|
3

If you have a function in your js code that takes some parameters and you would like to call that from Jupyter Notebook, you can do following:

from IPython.display import HTML, Javascript

HTML('<script src="./yourfile.js"></script>
Javascript('yourFunction(%s)' % parameter)

In that case, that you are using pandas's dataframe, the last line should be looking following:

Javascript('yourFunction(%s)' % parameter.to_json(orient='records'))

Links:

Instead of '%' you can choose format function

Comments

2
from IPython.display import HTML
HTML('''
<script type="text/javascript">
    IPython.notebook.kernel.execute("foo=11")
</script>
 ''')
from time import sleep
sleep(3)
print(foo)

The reason this works is the HTML takes some time to work and you print it even before the value is set. With sleep, the wait time of 3s seems to be enough and the variable gets assigned.

1 Comment

This did not work for me on a Jupyter Notebook (running this code on the same cell).
2

The problem here is that the HTML object is not the last one in the cell. So it is ignored in the same way any other value without print is not shown, unless it is the last one in the cell. If you execute a cell with the next code, you won't see any alert window.

HTML('''
    <script type="text/javascript">
        alert("hello")
    </script>
''')
print("hello")

Ensure that the last object in the cell is the HTML object and you will see the alert window.

HTML('''
    <script type="text/javascript">
        alert("hello")
    </script>
''')

That's why the examples of Anthony Perot work, they are separated cells. This should also work:

HTML and print in separated cells

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.