Image by author
Whether you are a beginner or a data professional, you must have used Jupyter Notebook and discovered how easy it is for you to run Python code and visualize the result in a report format.
But what if I told you that you can improve your development experience with Jupyter? In this post, we will learn about 10 Jupyter Notebook tips to improve the productivity and performance of data professionals.
Keyboard shortcuts are important for performing repetitive tasks and saving time. You can get information about all the default keyboard shortcuts by clicking Help > Keyboard Shortcuts or by pressing the h key.
The easiest and most popular way to access commands on the fly is a command palette similar to VSCode. you can press Ctrl + Shift + P to invoke the command palette. It allows you to search and run commands or scroll through all the commands to find the one you want to run.
Gif by author
You can access all IPython Magic commands inside Jupyter Notebook. These commands give you additional capabilities to run your code.
For example, you can use %%time
Magic command to show the execution time of the cell. In our case, the code took 1.09 seconds to execute 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 find out all the available Magic commands by running the %lsmagic
command or checking the Built-in magic commands.
List of commonly used commands:
- %env to set environment variables.
- %run to run the Python code.
- %store to access variables between multiple notebooks.
- %%time gives the execution time of a cell.
- %%write file saves the cell contents to a file.
- %pycat displays the contents of the external file.
- %pdb used for debugging.
- % matplotlib online to suppress the output of the function on the final line.
You can run shell and bash commands inside the Jupyter Notebook cell using !
As shown below. It gives you additional ability to run Unix or Linux based commands and tools.
The most popular use of this command is to install Python packages on the fly.
You can also install python packages using the Magic command %pip
When you create your data analysis report, you need to provide mathematical or statistical equations, and Jupyter Notebook allows you to represent complex equations using Latex formulas.
Simply 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$
Production:
We all know about the Python kernel, but you can also install other kernels and run code in any language.
For example, if you want to run the R programming language in Jupyter Notebook, you must install R and install IRkernel inside the R environment.
install.packages('IRkernel')
IRkernel::installspec()
Or, if you have Anaconda installed, you can simply run the following command in the terminal to configure R for Jupyter Notebook.
conda install -c r r-essentials
For lovers of Julia languages, I have created a simple guide on how to set up Julia in Jupyter Notebook.
You can also run code from multiple cores within Python Jupyter Notebook using magic commands like:
- %%tried
- %%html
- %%javascript
- %%pearl
- %%python3
- %%Ruby
In the example, we will try to execute HTML code inside the Python kernel using %%HTML
magic command.
%%HTML
<html>
<body>
<h1>Hello World</h1>
<p>Welcome to my website</p>
</body>
</html>
Production:
Similar to !
you can run the shell script using %%script
, which allows you to run all the kernels installed on your machine. For example, you can run an R script.
%%script R --no-save
print("KDnuggets")
Production:
> print("KDnuggets")
[1] "KDnuggets"
>
You can use multiple cursors to edit multiple variables and syntax, or add multiple lines of code. To create multiple cursors, you need to click and drag the mouse while holding down the key alternative key.
Gif by author
You can display images, videos, and audio without installing additional python packages.
you just have to import IPython.display
to obtain the functions of image, video and audio. It is quite useful when dealing with unstructured data sets and machine learning applications.
Image by author
You can process and query large data sets using the IPython Parallel library. It is the collection of CLI scripts for controlling clusters of IPython processes based on the Jupyter protocol.
Also, you can use the PySpark session with the sparkmagic domain.
Take a look at the example from the sparkmagic repository.
%%spark -c sql -o df_employee--maxrows 5
SELECT * FROM employee
Production:
age name
0 40.0 abid
1 20.0 Matt
2 36.0 Chris
It is important to share reports or source code with outputs, and you can do this in several ways:
- Convert Notebooks to HTML files using the File > Download As > HTML.
- Save notebooks as PDF files using File > Download As > PDF.
- Save notebooks as Markdown File > Download As > Markdown.
- Create blogs using Pelican.
- Upload the .ipynb file to Google Co. and share it with colleagues.
- Share the Notebook file with the public using GitHub Gits.
- Host your file in the cloud or external server and use it nbviewer to render the Notebook.
I hope you found my list of 10 Jupyter Notebook Tips helpful. If you have any additional tips or advice for Jupyter Notebook that you’d like to share, 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 creating content and writing technical blogs on machine learning and data science technologies. Abid has a Master’s in Technology Management and a Bachelor’s in Telecommunications Engineering. His vision is to build an AI product using a graphical neural network for students battling mental illness.