Learn These 15 Magic Commands in Jupyter Notebook to Save Time
What are Magic Commands?
Magic commands, also known as magic functions, are special commands that IPython offers compared to the standard Python shell. They are available in Jupyter Notebook, the official IPython Notebook.
Magic commands are simple functions that assist you in solving frequent problems while working with Jupyter notebooks. They greatly expand your notebook capabilities and improve your productivity!
There are two types of magic commands:
- Line Magics: They operate on a single line of input. They are denoted by a %
prefix that is followed by the argument without parenthesis or quotations.
- Cell Magics: They operate on an entire cell of the notebook, that is on multiple lines. They are denoted by a double %% prefix.
In this article, I’ll go over the 15 most commonly used magic commands and demonstrate practical examples.
1) %lsmagic — List the Magic Commands
This command will give you a list of all the available magic commands in Jupyter Notebook. It’s a great way to explore the different options.
%lsmagic
2) %quickref — Magic Commands Cheat Sheet
%quickref
is similar to %lsmagic
. If you’re ever stuck and need a quick reference for Jupyter Notebook’s basic functionality, use %quickref
to see a list of common commands and their descriptions.
%quickref
3) %who — List the Variables
Want to see a list of all the variables in your current notebook? Just type %who
and it will display all the variable names, types, and values.
Let’s define some variables and display them using %who
:
number = 5
text = "some text"
boolean = True
%who
You can also pass a specific data type after the command %who
to list only the variables of that type:
%who str
Another variant of %who
, %whos
, provides additional information about each variable:
%whos
%who_ls
returns a sorted list of the current variables:
%who_ls
4) %xdel — Delete Variable
Sometimes you need to delete a variable from your notebook. %xdel
will not only delete the object but also free up the memory it was using.
x = 4
%xdel x
5) %%time — Measure Execution Time
This magic command will time how long it takes to run a specific block of code in your notebook. Simply put it at the beginning of the cell containing the code you want to time, and it will display the elapsed time when the cell finishes executing.
Let’s measure the execution time of the following cell:
%%time
import numpy as np
for i in range(1000):
np.random.random_sample()
We can also measure the execution time of only one line by using %time
:
import numpy as np
%time for i in range(1000): np.random.random_sample()
6) %pinfo — Get Information about an Object
If you want to see the documentation for a particular object, use %pinfo
followed by the object name. In case of a function, this will display the docstring for the function, which contains useful information such as the function’s parameters, return type, and examples.
x = "Hello World!"
%pinfo x
7) %run — Run External File as a Program
Need to run a Python script from within your Jupyter Notebook? Just use %run
followed by the path to the script. This command can be particularly useful if you want to apply functions that are stored in external Python files.
The filename argument should be either a pure Python script (with extension .py), or a file with custom IPython syntax (such as magic commands).
As an example, I created the following Python script which onlain contains a print()
function:
PrintHelloWorld.py
print('Hello World!')
Now I can run it from Jupyter Notebook using the magic command %run
:
%run PrintHelloWorld.py
8) %load — Load Code from File
%load
is pretty similar to %run
. This command will load the contents of an external script into a cell in your current Jupyter Notebook. Simply type %load
followed by the path to the script.
Using the same file as for the previous example, I use %load
to load the code in a cell:
%load PrintHelloWorld.py
9) %pycat — Load and Highlight Code from File
The %pycat
command is similar to %load
. If you want to quickly view the contents of an external file, use %pycat
followed by the path to the file. This will display the contents of the file in a syntax-highlighted fashion through a pager, making it easy to scroll through and read.
Remember the ‘PrintHelloWorld.py’ file I made earlier? Let’s use %pycat
command to display its content again.
%pycat PrintHelloWorld.py
10) %%writefile — Write Content to a File
Need to save the contents of a cell in your Jupyter Notebook to an external file? Just use %%writefile
followed by the path to the file, and the contents of the cell will be saved to the file.
This command is useful if you want to create a file with your code directly from your Jupyter notebook. The command will create a new file if it doesn’t exist. If it already exist, it will be overwritten until you add the -a
(append) after the command.
%%writefile test.py
a = 5
b = 10
a, b = b, a
print("a =", a)
print("b =", b)
11) %pwd — Display Working Directory Path
If you’re not sure what your current working directory is, use %pwd
to display the current path.
%pwd
12) %cd — Change Current Working Directory
Need to change your working directory? Just use %cd
followed by the path to the directory you want to switch to.
%cd C:\Users\johndoe\Desktop\new_directory
13) %history and %recall — Display Previous Commands and Recall them
Here’s one of the commands I find the most useful. If you accidentally delete a cell and its output, you can use the magic command %history
to display all the previous commands you’ve executed in your current Jupyter Notebook session.
%history -n
displays the n-last commands with line numbers.
If you need to quickly re-execute a previous command, use %recall
followed by the line number of the command you want to execute again.
14) %env — Get and Set Environment Variables
Need to list, get, set environment variables? Just use the%env
magic command.
Running the command without any arguments will display a list of all the environment variables:
%env
Add a variable name after the %env
to get its value:
%env HOMEDRIVE
We may also use %env
to set the value of an environment variable:
%env: HOMEDRIVE=F
15) %automagic — Call Magic Functions without %
Here’s a magic command that makes it easier to use magic commands! %automagic
make magic commands callable without having to type the % prefix.
%automagic 1
activates the mode while %automagic 2
desactivates it. When automagic is on, you can use magic commands without the % prefix.
Conclusion
And there you have it! 15 magic commands that can save you time and boost your productivity in Jupyter Notebook. By using these commands, you can streamline your workflow and focus on what really matters: analyzing and visualizing data.
Of course, this is just the tip of the iceberg. There are many more magic commands available in Jupyter Notebook, and I encourage you to explore them and discover how they can help you in your data analysis projects.
One thing to keep in mind is that magic commands are specific to Jupyter Notebook and may not work in other Python environments. However, they can be a powerful tool for those who use Jupyter Notebook on a daily basis.
In conclusion, I hope that this article has been helpful in introducing you to these useful commands. If you have any other favorite magic commands that you use in Jupyter Notebook, feel free to share them in the comments below! Happy coding!