本文介绍了Jupyter/IPython 笔记本可以在 URL 中接受参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以编写一个 Jupyter Notebook,以便可以通过 Notebook 的 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 单元内的 Variable1
和 Variable2
?
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 + "'");
然后在下一个单元格中:
Then in the next cell:
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 中接受参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!