Run Jupyter notebooks in minimal time

You need to re-run a report your colleague has created in a notebook. Or perhaps, you want to pass a report you’ve made to a colleague to refresh. For either situation, these are some short cuts to run Jupyter notebooks in minimal time.

Run entire Jupyter notebooks or cells within notebooks

For instances when you want to run completed code, the “Cell” menu is your friend. Firstly, if you just want to run an entire notebook, click “Run All”. Often, you’ll do this if you are refreshing an existing report. But remember to update your data as well – you may have to refresh a flat file in your folder, or double-check the date filters in the SQL queries within the code.

Run Jupyter notebooks with the "Cell" menu.

Secondly, you might take a few days to build a report. Let’s say you worked on some code yesterday, then want to start again from where you left off. You can click the cell that you are working on, and then click “Run All Above” in the “Cell” menu. This will re-create all the variables and calculations that you had set up previously, so that you can continue working from that point onwards.

Clearing a kernel to erase your previous output

Let’s look at a different scenario now. Suppose you are in the middle of creating a report, and have rewritten different blocks of code during the day. You need to confirm that all your code makes sense, so you want to start on a clean slate and have your computer re-create all the calculations from scratch. In this case, you need to restart your kernel. Your kernel is the engine that runs your code, and it stores all the variables that you created when running code in your current session (i.e. since you last opened your notebook).

Choosing “Restart & Clear Output” is the best and quickest way to clear your variables and start anew, because:

  • Clearing your kernel will remove the variables that you created during the times you ran your code ever since you opened your notebook.
  • However, if you only click “Restart”, you won’t clear away anything that you asked Python to print within your notebook. For example, you might have validated your calculations by printing out parts of a DataFrame. They will remain there after you clicked “Restart”, and will only refresh after you re-run your notebook again. This might confuse you.
  • The “Clear Output” portion of “Restart & Clear Output” clears away everything that’s been printed as well, giving you a clean looking notebook to start again with.

If you are already sure of everything and you just want to purge everything in the notebook for a full re-run from the beginning, click “Restart & Run All”. Clicking the fast-forward icon on your toolbar will also achieve the same thing.

Code taking too long? Stop it by interrupting the kernel.

Sometimes, you might accidentally write a code that loops around and doesn’t know when to stop. When this happens and you want to stop and troubleshoot, there are two one-click ways to do this:

Run Jupyter notebooks - interrupt kernels

After interrupting your kernel, you can restart your kernel and use “Cell” -> “Run All Above” to get yourself back to the point where you left off.

In this post, we’ve covered:

Tasks you would do in ExcelEquivalent operation with Jupyter notebooks
Refresh data in a spreadsheet to re-run / update calculationsOne-click re-run of a Jupyter notebook using “Cell” -> “Run All”, “Kernel” -> “Restart & Run All”, or the fast-forward icon in the toolbar.
Interrupt code that’s taking too long by hitting the “Stop” icon in the toolbar or using “Kernel” -> “Interrupt”. Use “Kernel” -> “Restart & Clear Output” to clear everything and continue troubleshooting.

Ways to run specific cell(s) in Jupyter notebooks

I would be remiss not to mention the other ways that you can run cells in Jupyter notebooks, even though these are slower than the one-click options highlighted above. By the time you need to use these, you would be quite familiar with coding and want to run and troubleshoot specific cells of code.

Individual cells can be run by selecting the cell and hitting Ctrl + Enter (which keeps you on the same cell), or Shift + Enter (which brings you to the next cell down).

Additionally, you can select multiple cells by holding down either Ctrl (for non-consecutive cells) or Shift (for consecutive cells) and then clicking the “Run” icon on the toolbar or “Run Cells” in the “Cell” menu. Hitting either Ctrl + Enter or Shift + Enter also works, just as described in the previous paragraph.

Run Jupyter notebooks - options to run specific cells

Technical references

After you feel ready for all the technical details, you can dive into the official Jupyter Notebooks documentation here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.