Example of Cell Magic Functions

1. %%bash

Allows user to run Bash shell commands within the cell.

Example:

Python3




%%bash
ls -l


Output Screenshot:

2. %%html

This cell magic function renders contents of code cell as html script

Example:

Python3




%%html
<font size=10 color='hotpink'>Hello World</font>


Output Screenshot:

3. %%javascript or %%js

Allows you to execute JavaScript code within the cell

Example:

Python3




%%javascript
alert("This is a JavaScript alert!")


Output Screenshot:

4. %%latex

Renders LaTeX equations and expressions in the cell.

Python3




%%latex
$e^{i\pi} + 1 = 0$


Output Screenshot:

5. %%markdown or %%md

Renders the content of the cell as Markdown

Example:

Python3




%%markdown
# This is a Markdown Heading
* This is a bullet point


Output Screenshot:



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

...