Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable in IPython / Jupyter to a block of html (%%html)

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.
like image 927
thecheech Avatar asked Mar 24 '16 11:03

thecheech


1 Answers

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)
like image 144
thecheech Avatar answered Sep 29 '22 00:09

thecheech