Jupyter/IPython笔记本可以在URL中接受参数吗? [英] Can a Jupyter / IPython notebook take arguments in the URL?

查看:141
本文介绍了Jupyter/IPython笔记本可以在URL中接受参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写Jupyter笔记本,以便可以通过笔记本的URL传递参数?

Is it possible to write an Jupyter notebook such that parameters can be passed in via the URL of the notebook?

例如,对于这样的URL:

Example, for a URL such as this:

http://jupyter.example.com/user/me/notebooks/notebook1.ipynb?Variable1=Value1&Variable2=Value2

如何访问Jupyter单元中的Variable1Variable2?

how could access Variable1 and Variable2 inside the Jupyter cell?

推荐答案

您需要使用JavaScript找出URL,并将其传递给IPython内核:

You need to find out the URL using JavaScript and pass it to the IPython kernel:

from IPython.display import HTML
HTML('''
    <script type="text/javascript">
        IPython.notebook.kernel.execute("URL = '" + window.location + "'")
    </script>''')

或:

%%javascript
IPython.notebook.kernel.execute("URL = '" + window.location + "'");

然后在下一个单元格中

print(URL)

之后,您可以使用标准库中的工具(或纯字符串操作)提取查询参数.

After this you can use the tools in the standard library (or plain string operations) to pull out the query parameters.

这篇关于Jupyter/IPython笔记本可以在URL中接受参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆