execute a Jupyter Notebook cell programmatically

陌路散爱 提交于 2020-04-10 07:45:39

问题


Is it possible for a Jupyter Notebook cell to execute another cell programmatically? (i.e. using Python)

And if so, is it possible to specify the cell number to execute?


回答1:


There is a javascript function called execute_cells that when given an list of cell indices runs those cells.

%%javascript
Jupyter.notebook.execute_cells([0]) # 0 to run first cell in notebook etc.

If you need to run it specifically in a Python code cell, one can use the Javascript function in the IPython.display module to execute javascript

from IPython.display import Javascript
Javascript("Jupyter.notebook.execute_cells([2])")

see: https://github.com/jupyter/notebook/blob/881268f64245e0829173d1a6dc7aad7db39795d3/notebook/static/notebook/js/notebook.js#L2407



来源:https://stackoverflow.com/questions/47567834/execute-a-jupyter-notebook-cell-programmatically

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