Is there magic to restart kernel / change kernel?

I am wondering if there is an option to change / restart kernel from the notebook’s cell?

I am working on multiple runbooks that often mixes %%bash cells with actual Python code. In some tutorials I would really use an option to to trigger change of kernel from the code cell.

My use case woudl be to have first set of commands that would create a specific environment and install kernel - so it is in principle selectable from the drop down - but instead of telling the reader to go and choose different kernel in markdown comment I would prefer to just be able to execute a cell with a magic command t hat would restart the notebook with a new kernel.

1 Like

I guess you could try to use ipylab (GitHub - jtpio/ipylab: Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍) for that:

from ipylab import JupyterFrontEnd

app = JupyterFrontEnd()
app.commands.execute('kernelmenu:restart')
# or:
# app.commands.execute('notebook:change-kernel')

Both of the commands bring up a dialog window, but I would be in favour of adding an argument that allows to skip the confirmation for kernelmenu:restart and pre-specify kernel for notebook:change-kernel - feel welcome to open an issue on GitHub (and if there is no arguments against, send a PR if you think that it would be useful).

1 Like