5

I would like to pass a simple variable to an html cell on Jupyter:

cell 1:

a=5

cell 2:

%%html
<html>
  <head>
    <script type="text/javascript">
    window.alert(a);
    </script>
  </head>
</html>

This will return an error:

Javascript error adding output!
ReferenceError: a is not defined
See your browser Javascript console for more details.
Thomas K
  • 39,200
  • 7
  • 84
  • 86
thecheech
  • 2,041
  • 3
  • 18
  • 25

1 Answers1

5

This is a hacky solution:

a=5

html_code ="""
<html>
  <head>
    <script type="text/javascript">
    window.alert(%s);
    </script>
  </head>
</html>""" % a

from IPython.display import HTML
HTML(html_code)
Qululu
  • 1,040
  • 2
  • 12
  • 23
thecheech
  • 2,041
  • 3
  • 18
  • 25