import pandas as pd
import numpy as np
# generate a DataFrame with 100 rows and 50 columns
= pd.DataFrame(data=np.random.randint(0, 100, (100, 50)))
df
df
Kernel Management
What is a kernel?
A ‘kernel’ in Jupyter parlance is the computational engine that executes the code written in the notebook. The kernel also handles tasks such as computations for widgets and tab completion.
When you open a notebook, the associated kernel is automatically launched for you. The kernel’s language corresponds to the language of the notebook. For instance, if you’re working in a Python notebook, the kernel is responsible for executing Python code.
Selecting and switching kernels
The ability to select and switch kernels is an integral part of kernel management. This functionality allows users to run different code cells within the same notebook using different programming languages.
How to Select a Kernel
When you create a new notebook, you need to specify which kernel to use. The kernel you choose is dependent on the programming language you intend to use. Here are the steps:
- Click on the “File” tab on the Jupyter notebook interface.
- Scroll down and click “New Notebook.”
- A side menu will pop up with a list of available kernels.
- Select the kernel that corresponds to the programming language you wish to use.
How to Switch Kernels
If you want to change the kernel in an existing notebook, follow these steps:
- Open the notebook you want to switch kernels in.
- Click on the “Kernel” tab on the notebook interface.
- Scroll down and click “Change Kernel.”
- A side menu will pop up with a list of available kernels.
- Select the kernel that you wish to switch to.
Example
Let’s say you’re working on a Python notebook but you want to switch to an R kernel to leverage some of the exclusive statistical packages that R provides.
Here’s how you’d do it:
- Write your Python code as you normally would. Execute all the cells you need.
- Go to the
Kernel
menu, thenChange kernel
, and selectR
. - Now your notebook is in R mode. You can write and execute R code.
Please note that the Python and R variables are not shared. That means an R kernel is oblivious to what happens in a Python kernel and vice versa.
Important
Keep in mind that for you to switch to a given kernel, you must have that kernel installed in your machine. If it’s not installed, Jupyter will not list it as an option in the Change kernel
dropdown menu.
Restarting, interrupting, and shutting down kernels
Restarting kernels
There are situations where you might want to restart the kernel— for example, if your code is stuck in an infinite loop or if the kernel is consuming too much memory.
Another example, if you’re debugging your code and you want to ensure that your results are not due to variables from previous cells, you might choose to restart your kernel to clear all loaded variables.
To restart the kernel:
- Go to the
Kernel
menu. - Click
Restart
.
This will restart the kernel, meaning the kernel process is shut down and started anew. This is a pretty “hard” operation, but sometimes necessary. Any variables or functions defined will be lost.
Interrupting Kernels
If you want to stop the execution of a code cell, you can interrupt the kernel. This is useful if a computation is taking longer than expected and you want to stop it to modify and rerun the code.
Let’s say you’re running a machine learning model with a large dataset, but you realize you’ve made a mistake in your code that will affect the output. Rather than waiting for the lengthy process to finish, you can interrupt the kernel, fix your code, and rerun the cell.
To interrupt the kernel:
- Go to the
Kernel
menu. - Click
Interrupt
.
This sends a signal to the kernel to stop execution as soon as possible. If the kernel is currently executing a system call or similar that cannot be interrupted, you’ll have to wait for it to return.
Shutting Down Kernels
Shutting down a kernel removes it from the kernel menu and frees up the resources it was using. If you’re done with your analysis and no longer need the notebook, you can shut down the kernel to free up system resources.
To shut down the kernel:
- Go to the
File
menu. - Click
Close and Halt
.
Note: Restarting, interrupting, and shutting down kernels are actions that cannot be undone. Hence, use these options wisely and save your work frequently.
Clearing cell outputs
Let’s say you have a notebook with a lot of cells that generate long outputs, like computation results or visualizations. Over time, the notebook can get quite cluttered, making it difficult to navigate and causing it to load slowly.
For instance, you might be working with a cell that generates a large DataFrame output:
The output of this cell would be a large DataFrame that takes up a lot of space in your notebook.
To make your notebook more manageable, you can clear the output of this cell. After running the cell, you would go to the Cell
menu, select Current Outputs
, and then Clear
. The large DataFrame output will now be removed from your view, making your notebook cleaner and more concise.