10 Jupyter Notebook Tips and Tricks for Data Scientists
Unlock the full potential of Jupyter Notebook with expert tips and techniques, including time-saving shortcuts, powerful magic functions, and advanced features, to boost your productivity.
Image by Author
Whether you are a beginner or a data professional, you must have used the Jupyter Notebook and found out how easy it is for you to run Python code and visualize the output in a report format.Â
But what if I told you that you can improve your Jupyter development experience? In this post, we will be learning about 10 Jupyter Notebook tips for enhancing the productivity and performance of data professionals.Â
1. Keyboard Shortcuts
Keyboard shortcuts are important for performing repetitive tasks and saving time. You can learn about all of the default keyboard shortcuts by Help > Keyboard Shortcuts or pressing the H key.
The easiest and most popular way of accessing commands on the go is a command palette similar to VSCode. You can press Ctrl + Shift + P to invoke the command palette. It allows you to search and execute commands or scroll through all of the commands to discover the one you want to run.
Gif by Author
2. IPython Magic Commands
You can access all of the IPython Magic commands within the Jupyter Notebook. These commands give you extra capabilities in executing the code.Â
For example, you can use %%time
Magic command to display the cell execution time. In our case, it took the code 1.09 seconds to run 1000 iterations.Â
%%time
import time
for i in range(1_000):
time.sleep(0.001)
CPU times: user 10.2 ms, sys: 1.68 ms, total: 11.9 ms
Wall time: 1.09 s
You can learn about all of the available Magic commands by running the %lsmagic
command or checking out the Built-in magic commands.Â
List of commonly used commands:
- %env for setting environment variables.
- %run for executing the Python code.
- %store for accessing the variables in between multiple notebooks.Â
- %%time gives the execution time of a cell.
- %%writefile saves the content of the cell into a file.Â
- %pycat shows the content of the external file.Â
- %pdb is used for debugging.
- %matplotlib inline for suppressing the output of the function in the final line.
3. Executing Shell Commands
You can run Shell and Bash commands within the Jupyter Notebook cell by using !
as shown below. It provides you extra ability to run Unix or Linux based commands and tools.Â
!git push origin
Most popular use of this command is to install Python packages on the go.Â
!pip install numpy
You can also install Python packages using Magic command %pip
%pip install numpy
4. Using LaTeX for Formulas
While creating the data analytics report, you need to provide statistical or mathematical equations, and Jupyter Notebook lets you render complex equations using Latex formulas.Â
Just create a Markdown cell and enclose your Latex formula with a dollar $ sign, as shown below.Â
$\int \frac{1}{x} dx = \ln \left| x \right| + C$
Output:
5. Installing Other Kernels for Jupyter Notebook
We all know about the Python Kernel, but you can also install other Kernels and run the code in any language.Â
For example, if you want to run the R programming language in Jupyter Notebook, you need to install R and install IRkernel within the R environment. Â
install.packages('IRkernel')
IRkernel::installspec()
Or, if you have installed Anaconda, you can simply run the command below in the terminal to set up R for Jupyter Notebook.Â
conda install -c r r-essentials
For Julia language lovers, I have created a simple guide on how to set up Julia on Jupyter Notebook.
6. Run Code from a Different Kernel
You can also run code from multiple kernels within Python Jupyter Notebook by using Magic commands like:
- %%bash
- %%html
- %%javascript
- %%perl
- %%python3
- %%ruby
In the example, we will try to run HTML code within the Python Kernel using %%HTML
Magic command.Â
%%HTML
<html>
<body>
<h1>Hello World</h1>
<p>Welcome to my website</p>
</body>
</html>
Output:
Similar to !
, you can run Shell script using %%script
, which allows you to run all of the kernels installed on your machine. For example, you can run an R script.
%%script R --no-save
print("KDnuggets")
Output:
> print("KDnuggets")
[1] "KDnuggets"
>
7. Multicursor Support
You can use multiple cursors for editing multiple variables and syntax or adding multiple lines of code. To create multiple cursors, you need to click and drag your mouse while holding down the Alt key.
Gif by Author
8. Output Images, Videos, and Audio
You can display Images, Videos, and Audio without installing additional Python packages.Â
You just have to import IPython.display
to get the Image, Video, and Audio functions. It is quite useful when you are dealing with unstructured datasets and machine learning applications.Â
Image by Author
9. Processing Large Data
You can process and query large datasets by using the IPython Parallel library. It is the collection of CLI scripts for controlling clusters of IPython processes built on the Jupyter protocol.
Moreover, you can use the PySpark session with the sparkmagic command.Â
Check out the example from the sparkmagic repository.Â
%%spark -c sql -o df_employee--maxrows 5
SELECT * FROM employee
Output:Â
age name
0 40.0 abid
1 20.0 Matt
2 36.0 Chris
10. Sharing Notebooks
Sharing the reports or code source with outputs is important, and you can do it in multiple ways:
- Convert Notebooks into HTML files using the File > Download as > HTML.
- Save Notebooks as PDFs using File > Download as > PDF.
- Save Notebooks as Markdown File > Download as > Markdown.
- Create blogs using Pelican.
- Upload the .ipynb file to Google Colab and share it among colleagues.
- Share the Notebook file with the public using GitHub Gits.Â
- Host your file on the cloud or the external server and use nbviewer to render the Notebook.
I hope you found my list of 10 tips on Jupyter Notebook helpful. If you have any additional suggestions or tips for Jupyter Notebook that you'd like to share, please feel free to mention them in the comments below. Thank you for reading.
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master's degree in Technology Management and a bachelor's degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.