【问题标题】:Pass a variable in IPython / Jupyter to a block of html (%%html)将 IPython / Jupyter 中的变量传递给 html 块 (%%html)
【发布时间】:2016-03-24 11:00:31
【问题描述】:

我想将一个简单的变量传递给 Jupyter 上的 html 单元格:

单元格 1:

a=5

单元格 2:

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

这将返回一个错误:

Javascript error adding output!
ReferenceError: a is not defined
See your browser Javascript console for more details.

【问题讨论】:

    标签: python html ipython jupyter jupyter-notebook


    【解决方案1】:

    这是一个 hacky 解决方案:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多