Magic Jupyter Commands

Aug. 31, 2023 - 968 views

2 min read

Wissam Fawaz

This short article builds on a LinkedIn post I published about a week ago. It sheds light on the so-called magic commands supported by the Jupyter Notebooks

Have you ever felt like your Jupyter Notebook could use a sprinkle of magic? Well, it turns out that these Notebooks have been hiding enchanting spells all along! So, let us investigate some of Jupyter's hidden treasures, namely the magic commands. 

 

1. A Glimpse Into the Magic Realm:

Did you know your Jupyter Notebook has been training at Hogwarts? The magic commands offered by IPython (Jupyter's default wand... I mean, kernel) are not your business-as-usual Python spells. Characterized by % or %% prefixes, they are ready to dazzle you! The official documentation provides a comprehensive overview of these commands.  

 

2. Five Spells You Must Know:

%lsmagic: Think of this as your Marauder's Map, unveiling all the secret spells within Jupyter.

%run: Do you want to funnel another Python script's magic into your notebook? Just type: %run file_name.py.

%%time: Have you ever wondered how long your potion... err, code takes to brew? This spell times your concoctions down to the millisecond.

%who: Misplaced your magical items (or variables)? Summon them with a swift %who.

%load: Channel external scripts right into your notebook cauldron using %load file_name.py.

 

3. Why Embrace The Magic?

The power of these commands lies in their ability to simplify your Notebook experience. They tackle some of the most commonly encountered challenges in Jupyter, eliminating the need for verbose code. Essentially, they are your quick fixes for optimizing both time and effort.

 

Jupyter's magic commands, while often overlooked, are tools that data scientists should have up their sleeves. They save time, are easy to use, and make data analysis a tad more magical. So next time you dive into a notebook, remember the magic is just one command away.