Jupyter Notebook Magic Commands Cheat Sheet



Jupyter Notebook. Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together. In this post, we’ve collected some of the top Jupyter notebook tips to quickly turn you into a Jupyter power user! Jupyter Notebook Cheat Sheet Jupyter IPython Notebook, Here are some of the commonly used Magic commands in jupyter Notebook. Statement, Explanation, Example.%magic, Comprehensively lists The text of the quick referance sheets comes from the IPython%quickref magic command. In a Jupyter Notebook, it can be used to link to any section of the notebook for easy navigation. Note: The ID used for the linking should be unique to avoid misdirection. Find any kind of errors in the cheat sheet and create a New issue with the details or fork the project and do a Pull Request. Suggest a better or more pythonic way for existing examples. Website; Github; PDF; Jupyter Notebook; Python Cheatsheet. The Zen of Python; Python Basics. Math Operators; Data Types; String Concatenation.

  1. Jupiter Notebook Magic Commands Cheat Sheet Download
  2. Jupiter Notebook Magic Commands Cheat Sheet Pdf
  3. Jupyter Notebook Cheat Sheet
  4. Jupyter Cheat Sheet Pdf
  5. Jupyter Notebook Magic Functions
  • Jupyter Tutorial
  • IPython
  • Jupyter
  • QtConsole
  • JupyterLab
  • Jupyter Resources
  • Selected Reading

Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.

Magic commands act as convenient functions where Python syntax is not the most natural one. They are useful to embed invalid python syntax in their work flow.

Types of Magic Commands

There are two types of magic commands −

  • Line magics
  • Cell magics

Line Magics

They are similar to command line calls. They start with % character. Rest of the line is its argument passed without parentheses or quotes. Line magics can be used as expression and their return value can be assigned to variable.

Cell Magics

They have %% character prefix. Unlike line magic functions, they can operate on multiple lines below their call. They can in fact make arbitrary modifications to the input they receive, which need not even be a valid Python code at all. They receive the whole block as a single string.

To know more about magic functions, the built-in magics and their docstrings, use the magic command. Information of a specific magic function is obtained by %magicfunction? Command. Let us now describe some of the built-in line and cell magic commands.

Built-in line magics

This magic function makes a function automatically callable without having to use parentheses. It takes three possible mode parameters: 0 (off), 1 (smart) is default or 2 (always on).

%automagic

Magic functions are callable without having to type the initial % if set to 1. Without arguments it toggles on/off. To deactivate, set to 0.

The following example shows a magic function %pwd (displays present working directory) being called without leading % when %automagic set to 1

%cd

This line magic changes the current directory. This command automatically maintains an internal list of directories you visit during your IPython session, in the variable _dh. You can also do ‘cd -<tab>’ to see directory history conveniently.

Usage

The %cd command can be used in the following ways −

  • %cd <dir> − Changes current working directory to <dir>

  • %cd. − Changes current directory to parent directory

  • %cd − changes to last visited directory.

%dhist

This magic command prints all directories you have visited in current session. Every time %cd command is used, this list is updated in _dh variable.

%edit

This magic command calls upon the default text editor of current operating system (Notepad for Windows) for editing a Python script. The script is executed as the editor is closed.

%env

This magic command will list all environment variables. It also reads value of particular variable or set the value of environment variable.

Usage

The %cd command can be used in the following ways −

  • %env − Lists all environment variables

  • %env var − Gets value for var

  • Zipeg for mac sierra. %env var val − Sets value for var

%gui [GUINAME]

When used without argument this command enables or disables IPython GUI event loop integration. With GUINAME argument, this magic replaces the default GUI toolkits by the specified one.

Sr.No.Command & Description
1

%gui wx

enable wxPython event loop integration

2

%gui qt4|qt Ubisoft for mac.

enable PyQt4 event loop integration

3

%gui qt5

enable PyQt5 event loop integration

4

%gui gtk

enable PyGTK event loop integration

5

%gui gtk3

enable Gtk3 event loop integration

6

%gui tk

enable Tk event loop integration

7

%gui osx

enable Cocoa event loop integration

8

(requires %matplotlib 1.1)

9

%gui

disable all event loop integration

%lsmagic

Displays all magic functions currently available

%matplotlib

This function activates matplotlib interactive support during an IPython session. However, it does not import matplotlib library. The matplotlib default GUI toolkit is TkAgg. But you can explicitly request a different GUI backend. You can see a list of the available backends as shown −

The IPython session shown here plots a sine wave using qt toolkit −

While using Jupyter notebook, %matplotlib inline directive displays plot output in the browser only.

%notebook

This function converts current IPython history into an IPython notebook file with ipynb extension. The input cells in previous example are saved as sine.ipynb

%pinfo

This function is similar to object introspection ? character. To obtain information about an object, use the following command −

This is synonymous to object? or ?object.

Jupyter notebook magics

%precision

This magic function restricts a floating point result to specified digits after decimal.

%pwd

This magic function returns the present working directory.

%pylab

This function populates current IPython session with matplotlib, and numpy libraries.

%recall

Jupiter

When executed without any parameter, this function executes previous command.

Note that in %recall n, number in front of it is input cell number. Hence the command in the nth cell is recalled. You can recall commands in section of cells by using command such as %recall 1-4. Current input cell is populated with recalled cell and the cursor blinks till the enter key is pressed.

%run

This command runs a Python script from within IPython shell.

%time

This command displays time required by IPython environment to execute a Python expression.

%timeit

This function also displays time required by IPython environment to execute a Python expression. Time execution of a Python statement or expression uses the timeit module. This function can be used both as a line and cell magic as explained here −

  • In line mode you can time a single-line.

  • In cell mode, the statement in the first line is used as setup code and the body of the cell is timed. The cell body has access to any variables created in the setup code.

%who

This line magic prints all interactive variables, with some minimal formatting. If any arguments are given, only variables whose type matches one of these are printed.

Jupiter Notebook Magic Commands Cheat Sheet Download

IPython Custom Line Magic function

IPython’s core library contains register_line_magic decorator. A user defined function is converted into a line magic function using this decorator.

  • Jupyter Tutorial
  • IPython
  • Jupyter
  • QtConsole
  • JupyterLab
Notebook
  • Jupyter Resources
  • Selected Reading

Markdown cell displays text which can be formatted using markdown language. In order to enter a text which should not be treated as code by Notebook server, it must be first converted as markdown cell either from cell menu or by using keyboard shortcut M while in command mode. The In[] prompt before cell disappears.

Header cell

A markdown cell can display header text of 6 sizes, similar to HTML headers. Start the text in markdown cell by # symbol. Use as many # symbols corresponding to level of header you want. It means single # will render biggest header line, and six # symbols renders header of smallest font size. The rendering will take place when you run the cell either from cell menu or run button of toolbar.

Following screenshot shows markdown cells in edit mode with headers of three different levels.

When cells are run, the output is as follows −

Note that Jupyter notebook markdown doesn’t support WYSWYG feature. The effect of formatting will be rendered only after the markdown cell is run.

Ordered Lists

To render a numbered list as is done by <ol> tag of HTML, the First item in the list should be numbered as 1. Subsequent items may be given any number. It will be rendered serially when the markdown cell is run. To show an indented list, press tab key and start first item in each sublist with 1.

If you give the following data for markdown −

It will display the following list −

Bullet lists

Each item in the list will display a solid circle if it starts with – symbol where as solid square symbol will be displayed if list starts with * symbol. The following example explains this feature −

The rendered markdown shows up as below −

Hyperlinks

Markdown text starting with http or https automatically renders hyperlink. To attach link to text, place text in square brackets [] and link in parentheses () optionally including hovering text. Following screenshot will explain this.

The rendered markdown appears as shown below −

Bold and Italics

Jupiter Notebook Magic Commands Cheat Sheet Pdf

To show a text in bold face, put it in between double underscores or two asterisks. To show in italics, put it between single underscores or single asterisks.

The result is as shown below −

Jupyter Notebook Cheat Sheet

Images

Jupyter Cheat Sheet Pdf

To display image in a markdown cell, choose ‘Insert image’ option from Edit menu and browse to desired image file. The markdown cell shows its syntax as follows −

Image will be rendered on the notebook as shown below −

Table

In a markdown cell, a table can be constructed using | (pipe symbol) and – (dash) to mark columns and rows. Note that the symbols need not be exactly aligned while typing. It should only take respective place of column borders and row border. Notebook will automatically resize according to content. A table is constructed as shown below −

Jupyter Notebook Magic Functions

The output table will be rendered as shown below −