Cell Magic Commands

Cell Magic functions are special commands that allow the user to modify the behavior of a code cell explicitly. Cell Magic functions have the prefix ‘%%’ followed by the command name. Cell magic functions serve various tasks and customizations which we discuss thoroughly further in this article. As per the official python documentation there are 16 cell magic functions available now which are described below,

Cell Magic Command

Description

%%bash

Run cells with bash in a subprocess

%%capture

run the cell, capturing stdout, stderr, and IPython’s rich display() calls

%%html

Render the cell as a block of HTML

%%javascript or %%js

Run the cell block of Javascript code

%%latex

Render the cell as a block of LaTeX

%%markdown

Render the cell as Markdown text block

%%perl

Run cells with perl in a subprocess

%%pypy

Run cells with pypy in a subprocess

%%python

Run cells with python in a subprocess

%%python2

Run cells with python2 in a subprocess

%%python3

Run cells with python3 in a subprocess

%%ruby

Run cells with ruby in a subprocess

%%script

Run a cell via a shell command

%%sh

Run cells with sh in a subprocess

%%svg

Render the cell as an SVG literal

%%writefile

Write the contents of the cell to a file

Since, its not possible to explain each cell magic command with example we will only cover some helpful and widely used cell magic commands.

Useful IPython magic commands

In this article, we will cover IPython Magic commands/functions and discuss various types of magic commands available. In this article we will be using Jupyter Notebook to execute the magic commands first, we look at what are Magic functions and why we use them, then different types of magic functions followed by examples. There are a lot of magic functions but in this article, we discuss the most commonly used magic functions.

Jupyter Notebook

The Jupyter Notebook is the original web application for creating and sharing computational documents that contain live code, equations, visualizations, and narrative text. It offers a simple, streamlined, document-centric experience. Jupyter has support for over 40 different programming languages and Python is one of them.

Similar Reads

Magic Commands

Magic commands generally known as magic functions are special commands in IPython that provide special functionalities to users like modifying the behavior of a code cell explicitly, simplifying common tasks like timing code execution, profiling, etc. Magic commands have the prefix ‘%’ or ‘%%’ followed by the command name. There are two types of magic commands:...

Line Magic Commands

Line magic commands are used to provide a special functionality to a single line of code, line magic commands begin with ‘%’ followed by the line command. To check the available magic commands run ‘%lsmagic’ this command will show all the magic commands, as of now there are 97 line magic commands which are as follows:...

Cell Magic Commands

...

Example of Cell Magic Functions

...