Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter keyboard shortcut for running a specific cell

In my workflow with Jupyter notebook, I have a cell like:

%run myscript.py

Which loads the latest version of a script with functions I'm using. I am constantly updating this script and using its function in my notebook. Therefore, I find myself constantly going back to the first cell and running it. It would be very convenient if there was a keyboard shortcut to do that (e.g. run the first cell). Does it exist?

like image 393
R S Avatar asked Dec 06 '25 06:12

R S


2 Answers

You can click "Ctrl + Enter" to run only the chosen cell no matter it is "edit mode" or "command mode".

If you want to know more available commands, just input Esc to change you current mode to "command mode" and input "h" to show the Keyboard shortcuts modal.

like image 166
Kane Blueriver Avatar answered Dec 08 '25 11:12

Kane Blueriver


An important update If you install nbextensions then there is a nice widget for that called

"Initialization cells"

Here is the link to nbextensions https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html

The following comments are my old comments and can be neglected.

You can do it by running the following code:

%%javascript
Jupyter.notebook.execute_cells([0])

Source: https://stackoverflow.com/a/47572981/7538689

Note: the cell number in this command is different from the cell number that appears on the left side of the cell in Jupyter. For this command, the cell number is based on the order of the cell in the Jupyter Notebook, from top to bottom.

like image 39
Matt Najarian Avatar answered Dec 08 '25 10:12

Matt Najarian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!