Running a Jupyter notebook from another notebook

别说谁变了你拦得住时间么 提交于 2020-11-30 04:54:01

问题


I wonder if it is possible to run a *.ipynb file from another *.ipynb file and get a returned value. I know that we can run like this:

%run ./called_notebook.ipynb

the called_notebook contains:

def foo():
    print(1)
    return 2
foo()

But it only prints "1" without giving me the opportunity to handle the returned value. Is it even possible ? Does the following kind of code even exist :

a = %run ./called_notebook.ipynb

?

Thanks !


回答1:


I'd suggest running the foo function from the new notebook. In other words:

%run ./called_notebook.ipynb
foo()

In my opinion, this is best practices for using the %run magic command. Store your high level APIs in a separate notebook (such as foo), but keep your function calls visible in the master notebook.



来源:https://stackoverflow.com/questions/49817409/running-a-jupyter-notebook-from-another-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!