It works like magic
Do you believe in magic? If you are not, then you should definitely believe in the Jupyter Notebook magic command. The name sounds absurd, but Jupyter Notebook contains a special command we called Magic Command.
True to its name, Magic Command or Line Magic is a special command we execute to achieve some result within the Jupyter Notebook. The magic command works by using the %
symbol with the command you want to run.
There are many kinds of Magic Command, but I would only show my top 9 Magic Command in this article.
Let’s get into it.
1. %who
Who? what is who? This command is a Magic command that would show all the available variables you had in your Jupyter Notebook Environment. Let me show you an example below.
import seaborn as snsdf = sns.load_dataset('mpg')a = 'simple'b = 2
Above, we create 3 different variables; df, a, and b. If you type %who
in your Jupyter Notebook cell, it would show all existing variables.
In the above image, we can see that we have all the variables, including the environment's pre-existing variables.
What if you want to see specific variables, say only the str variable? It is easy. You need to type the object type after the magic command. In this case, it was %who str
.
2. %timeit
This magic command is an interesting one. This magic command was used to evaluate the code execution speed by running it multiple times and produce the average + standard deviation of the execution time. Let’s try it with an example.
import numpy as np%timeit np.random.normal(size=1000)
Using %timeit
magic command, we know that the execution time only deviates around 341ns per execution time.
This command is useful when you want to pinpoint the stability of your code execution and looping process.
3. %store
What if you work on a project in one notebook and want to pass around the variables you had into another notebook. You do not need to pickle it or save it in some object file. What you need is to use the %store
magic command.
This is our previous Jupyter Notebook with the ‘df’ variable containing the mpg data frame. Next, I want to move this ‘df’ variable into another Notebook. I only need to type %store df
.
The ‘df’ variable is now stored within the Jupyter Notebook and ready to use in a different Jupyter Notebook. Let’s try to create a new notebook and type %store -r df
.
Just like that, our ‘df’ variable has moved into another Notebook and ready to use for another analysis. What is unique about the %store
magic command is that you could delete the variable in the notebook, and you would still have the variable you store within the %store
magic command.
4. %prun
Another magic command that has something to do with time. %prun
is a specific magic command to evaluate how much time your function or program to execute each function.
What is amazing about %prun
is that shows the table where you could see the number of times each internal function was called within the statement, the time each call took, and the cumulative time of all runs of the function.
Let’s try to run the %prun
magic command with an example.
%prun sns.load_dataset('mpg')
As you can see in the image above, we can see each function's execution time within the statement and their time plus the cumulative time.
5. %history or %hist
Have you been working so much on your analysis and wondering what kind of thing you have done and your current status. You might also confuse than ever when you have to jump around between the cells to run your function.
For this case, we could use the %history
magic command to see the log of your activity and trace back what you already did.
Try to run the %history
in your Jupyter Notebook cell and see what’s the output. Here is mine below.
6. %pinfo
Sometimes when you are working with a new object or packages, you want to get all the detailed information. If you are the lazy type like me, we can use the magic command %pinfo2
to get all the detailed information in our Jupyter Notebook cell.
Let’s try to run the magic command using our previous DataFrame object.
%pinfo df
Using this magic command, we can see all the information regarding the object and all the parameter available that we can use.
7. %%writefile
We know that Jupyter Notebook is not the best IDE for the development and production environment, but it doesn’t mean that we cannot do it in the Jupyter Cell.
What if you already have all that amazing function you code and want to save it in the python file. Sure, you can open another IDE and copy+paste the file, but there is an easier way to do it. We can use the magic command %%writefile
to achieve the same result.
Let’s try running the following code.
%%writefile test.pydef number_awesome(x): return 9
Check your current directory; you should have a new Python file right now.
8. %pycat
What if you want to do it the other way, like reading the Python file into your Jupyter Notebook? You could do it as well using the %pycat
magic command.
Let’s try to read our previous Python file.
%pycat test.py
A new pop up would show up with all the code within the Python file into the Jupyter Notebook.
This magic command is useful when you have many production and development codes you want to experiment with within the Jupyter Notebook.
9. %quickref
The last magic command you should know is %quickref
. Why is this the should know magic command? Because this magic command explains all the magic command that exists in the Jupyter Notebook with detail.
Let’s try to run it in your notebook.
%quickref
Just like that, you now are presented with all the explanations of every single magic command you could use. How useful is that!
Conclusion
Magic command is a special command within the Jupyter Notebook to improve our everyday activities as a Data Scientist. There are 9 magic commands that I feel necessary to know for people to use; they are:
%who
%timeit
%store
%prun
%history or %hist
%pinfo
%%writefile
%pycat
%quickref
I hope it helps!