Sunday, December 29, 2024
Google search engine
HomeLanguagesUseful IPython 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.

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:

  1. Line Magic Commands
  2. Cell 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:

%alias, %alias_magic, %autoawait, %autocall, %automagic, %autosave, %bookmark, %cat, %cd, %clear, %colors, %conda, %config, %connect_info, %cp, %debug, %dhist, %dirs, %doctest_mode, %ed, %edit, %env, %gui, %hist, %history, %killbgscripts, %ldir, %less, %lf, %lk, %ll, %load, %load_ext, %loadpy, %logoff, %logon, %logstart, %logstate, %logstop, %ls, %lsmagic, %lx, %macro, %magic, %man, %matplotlib, %mkdir, %more, %mv, %notebook, %page, %pastebin, %pdb, %pdef, %pdoc, %pfile, %pinfo, %pinfo2, %pip, %popd, %pprint, %precision, %prun, %psearch, %psource, %pushd, %pwd, %pycat, %pylab, %qtconsole, %quickref, %recall, %rehashx, %reload_ext, %rep, %rerun, %reset, %reset_selective, %rm, %rmdir, %run, %save, %sc, %set_env, %store, %sx, %system, %tb, %time, %timeit, %unalias, %unload_ext, %who, %who_ls, %whos, %xdel, %xmode

Since it is not possible to cover all the 97 line magic commands we will only cover the most widely and helpful commands in this article, we will look some line magic commands along with the short description followed by example of some line magic commands:

Line Magic Command

Description

%alias

Define an alias for a system command.

%alias_magic

Create an alias for an existing line or cell magic

%automagic

Make magic functions callable without having to type the initial %

%cd

Change the current working directory

%debug

Activate the interactive debugger

%dhist

Print your history of visited directories

%dirs

Return the current directory stack

%env

Get, set, or list environment variables.

%gui

Enable or disable IPython GUI event loop integration

%load

Load code into the current frontend

%load_ext

Load an IPython extension by its module name

%lsmagic

List All Available Magic Commands

%macro

Define a macro for future re-execution. It accepts ranges of history, filenames or string objects

%magic

Print Information About Magic Commands System

%matplotlib

Set up matplotlib to work interactively.

%notebook

Export and convert IPython notebooks

%pfile

Print (or run through pager) the file where an object is defined

%pip

Run the pip package manager within the current kernel

%prun

Run a statement through the python code profiler

%pwd

Return the current working directory path

%reload_ext

Reload an IPython extension by its module name

%reset

Resets the namespace by removing all names defined by the user

%run

Run the named file inside IPython as a program

%save

Save a set of lines or a macro to a given filename

%set_env

Set environment variables

%system

Shell execute – run shell command and capture output

%time

Measures time execution of a Python statement or expression

%timeit

Measures Execution Time of Line

%unalias

Remove an alias

%who

print all interactive variables, with some minimal formatting

%who_ls

Return a sorted list of all interactive variables

%whos

Like %who, but gives some extra information about each variable

%xdel

Delete a variable, trying to clear it from anywhere that IPython’s machinery has references to it.

%xmode

Switch modes for the exception handlers

Example of Line Magic Functions

1. %load

  • Load the code from external file and insert in into the cell
  • Syntax:

%load file_name.py

Example:

Python3




%load fibo.py


2. %run

  • Run the named file inside IPython as a program
  • Syntax:

%run file_name.py

Example:

Python3




%run tricks.py


Output:

Screenshot-from-2023-09-25-12-02-40

3. %time

  • Measure the execution time of a single Python statement or expression
  • Syntax:

%time some_funtion()

Example:

Python3




def fibonacci(n):
     if n == 0 or n == 1: return n
     return fibonacci(n-1)+fibonacci(n-2)
%time fibonacci(10)


Output:

CPU times: total: user 20 ns, sys:4 ns, total: 24 ns
Wall time: 26.2 ns
55

4. %timeit

  • Measure the execution time of a single Python statement or expression by performing executions multiple time to get more accurate results.
  • Syntax:

%timeit some_function()

Example:

Python3




def fibonacci(n):
     if n == 0 or n == 1: return n
     return fibonacci(n-1)+fibonacci(n-2)
%timeit fibonacci(10)


Output:

14.7 µs ± 869 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

5. %cd

  • Change the current working directory
  • Syntax:

%cd directory/sub_dir

Example:

Python3




%cd Form_data_visualization


Output:

Screenshot-from-2023-09-25-12-22-15

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.

Example of Cell Magic Functions

1. %%bash

Allows user to run Bash shell commands within the cell.

Example:

Python3




%%bash
ls -l


Output Screenshot:

Screenshot-from-2023-09-14-23-44-21

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:

Screenshot-from-2023-09-14-23-48-21

3. %%javascript or %%js

Allows you to execute JavaScript code within the cell

Example:

Python3




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


Output Screenshot:

Screenshot-from-2023-09-25-11-42-20

4. %%latex

Renders LaTeX equations and expressions in the cell.

Python3




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


Output Screenshot:

Screenshot-from-2023-09-14-23-54-16

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:

Screenshot-from-2023-09-14-23-40-27

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments