首页 > 解决方案 > 从脚本以编程方式调用 Jupyter Notebook 魔术命令

问题描述

您可以从脚本中调用 Jupyter 中的魔术命令,如下所示:

ipython = get_ipython()
mgc = ipython.run_line_magic

mgc(magic_name = "my_magic_command", line="line_to_call")

但是,如果我尝试使用 HTML 魔术命令执行此操作:

mgc(magic_name = "%%html", line="<iframe src='my_url' width='100%' height='400'></iframe>")

我收到以下错误:

UsageError: Line magic function `%%%html` not found.

让我觉得我应该删除 % 之一:

mgc(magic_name = "%html", line="<iframe src='my_url' width='100%' height='400'>")

UsageError: Line magic function `%%html` not found.

...或删除两个 %:

mgc(magic_name = "html", line="<iframe src='my_url' width='100%' height='400'>")

UsageError: Line magic function `%html` not found (But cell magic `%%html` exists, did you mean that instead?).

这可能与import 的顺序有关,但我似乎无法解决这个问题。

标签: pythonjupyter-notebookjupyterjupyter-lab

解决方案


推荐阅读