问题
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