1. Line Magics

1.1. %activity

(poll, classroom response, clicker-like activity)

This magic will load the JSON in the filename.

Examples: %activity /home/teacher/activity1 %activity /home/teacher/activity1 new %activity /home/teacher/activity1 edit

1.2. %cd

This line magic is used to change the directory of the notebook or console.

Note that this is not the same directory as used by the %shell magics.

Example: %cd ..

1.3. %connect_info

This line magic will show the connection information for this language kernel instance. This information is only necessary if you are interested in making additional connections to the running kernel.

Example: %connect_info

Paste the given JSON into a file, and connect with:

$> ipython <app> --existing <file>

or, if you are local, you can connect with just:

$> ipython <app> --existing %(key)s

or even just: $> ipython –existing

if this is the most recent Jupyter session you have started.

1.4. %conversation

1.5. %dot

This line magic will render the Graphiz CODE, and render it as an image.

Example: %dot graph A { a->b };

1.6. %download

This line magic will download and save a file. By default it will use the same filename as the URL. You can give it another name using -f.

Examples: %%download http://some/file/from/internet.txt -f myfile.txt %%download http://some/file/from/program.ss

1.7. Options:

-f –filename use the provided name as filename [default: None]

1.8. %edit

This line magic will open the file in the next cell, and allow you edit it.

This is a shortcut for %load, and appending a “%%file” as first line.

Example: %edit myprogram.ss

1.9. %get

This line magic is used to get a variable.

Examples: %get x

1.10. %help

This is MetaKernel Python. It implements a Python interpreter.

1.11. %html

This line magic will send the CODE to the browser as HTML.

Example: %html This is underlined!

1.12. %include

This line magic will get the contents of a file and include it in this cell evaluation.

You can have multiple %include’s at the beginning of a cell, and they will be included in order.

Examples: %include myprog.py %include myprog1.py myprog2.py

1.13. %install

Example: %install calico-spell-check

1.14. %install_magic

This line magic will copy the file at the URL into your personal magic folder.

Example: %install_magic http://path/to/some/magic.py

1.15. %javascript

This line magic will execute the CODE on the line as JavaScript in the browser.

Example: %javascript console.log(“Print in the browser console”)

1.16. %jigsaw

This line magic will allow visual code editing or generation.

Examples: %jigsaw Processing %jigsaw Python %jigsaw Processing –workspace workspace1 –height 600

1.17. Options:

-h –height set height of iframe [default: 350] -w –workspace use the provided name as workspace filename [default: None]

1.18. %kernel

This line magic will construct a kernel language so that you can communicate.

Example:

%kernel bash_kernel BashKernel -k bash

Use %kx or %%kx to send code to the kernel.

Also returns the kernel as output.

1.19. Options:

-k –kernel_name kernel name given to use for execution [default: default]

1.20. %kx

This line magic will send the CODE to the kernel for execution.

Returns the result of the execution as output.

Example:

%kernel ls -al

Use %kernel MODULE CLASS [-k NAME] to create a kernel.

1.21. Options:

-k –kernel_name kernel name given to use for execution [default: None]

1.22. %latex

This line magic will display the TEXT on the line as LaTeX.

Example: %latex $x_1 = \dfrac{a}{b}$

1.23. %load

This line magic will get the contents of a file and load it into the next cell.

Example: %load myprog.py

1.24. %ls

This line magic is used to list the directory contents.

Examples: %ls . %ls ..

1.25. Options:

-r –recursive recursively descend into subdirectories [default: False]

1.26. %lsmagic

This line magic will list all of the available cell and line magics installed in the system and in your personal magic folder.

Example: %lsmagic

1.27. %macro

This line macro will execute, show, list, or delete the named macro.

Examples: %macro renumber-cells

%%macro test
print "Ok!"

%macro -l all

%macro -d test

1.28. Options:

-s –show show macro [default: False] -l –list list macros [default: False] -d –delete delete a named macro [default: False]

1.29. %magic

This line magic shows all of the install magics, either from the system magic folder, or your own private magic folder.

1.30. %matplotlib

This line magic will set (and reload) the items associated with the matplotlib backend.

Also, monkeypatches the IPython.display.display to work with metakernel-based kernels.

Example: %matplotlib notebook

import matplotlib.pyplot as plt
plt.plot([3, 8, 2, 5, 1])
plt.show()

1.31. %parallel

Example:

%parallel bash_kernel BashKernel
%parallel bash_kernel BashKernel -k bash
%parallel bash_kernel BashKernel -i [0,2:5,9,...]

cluster_size and cluster_rank variables are set upon initialization of the remote node (if the kernel supports %set).

Use %px or %%px to send code to the cluster.

1.32. Options:

-i –ids the machine ids to use from the cluster [default: None] -k –kernel_name arbitrary name given to reference kernel [default: default]

1.33. %plot

This line magic will configure the plot settings for this language.

Examples: %plot qt –format=png %plot inline -w 640

Note: not all languages may support the %plot magic, and not all options may be supported.

1.34. Options:

-h –height Plot height in pixels -w –width Plot width in pixels -r –resolution Resolution in pixels per inch -b –backend Backend selection [default: inline] -f –format Plot format (png, svg or jpg). -s –size Pixel size of plots, “width,height”

1.35. %pmap

This line magic will apply a function name to all of the arguments given one at a time using a dynamic load balancing scheduler.

Currently, the args are provided as a Python expression (with no spaces).

You must first setup a cluster using the %parallel magic.

Examples:

%pmap function-name-in-language range(10)
%pmap function-name-in-language [1,2,3,4]
%pmap run_experiment range(1,100,5)
%pmap run_experiment ["test1","test2","test3"]
%pmap f [(1,4,7),(2,3,5),(7,2,2)]

The function name must be a function that is available on all nodes in the cluster. For example, you could:

%%px
(define myfunc
   (lambda (n)
     (+ n 1)))

to define myfunc on all machines (use %%px -e to also define it in the running notebook or console). Then you can apply it to a list of arguments:

%%pmap myfunc range(100)

The load balancer will run myfunc on the next available node in the cluster.

Note: not all languages may support running a function via this magic.

1.36. Options:

-s –set_variable set the variable with the parallel results rather than returning them [default: None]

1.37. %px

Example:

%px sys.version
%px -k scheme (define x 42)
%px x
%px cluster_rank

cluster_size and cluster_rank variables are set upon initialization of the remote node (if the kernel supports %set).

Use %parallel to initialize the cluster.

1.38. Options:

-s –set_variable set the variable with the parallel results rather than returning them [default: None] -e –evaluate evaluate code in the current kernel, too. The current kernel should be of the same language as the cluster. [default: False] -k –kernel_name kernel name given to use for execution [default: None]

1.39. %python

This line magic will evaluate the CODE (either expression or statement) as Python code.

Note that the version of Python is that of the notebook server.

Examples: %python x = 42 %python import math %python x + math.pi

1.40. %reload_magics

Example: %reload_magics

This line magic will reload the magics installed in the system, and in your private magic folder.

You only need to do this if you edit a magic file. It runs automatically if you install a new magic.

1.41. %restart

This line magic will restart the connection to the language kernel.

Example: %restart

Note that you will lose all computed values.

1.42. %run

kernel

This magic will take the code in FILENAME and run it. The exact details of how the code runs are determined by your language.

The –language LANG option will prefix the file contents with “%%LANG”. You may also put information in the cell which will appear before the contents of the file.

Examples: %run filename.ss %run -l python filename.py

%kx calysto_scheme.kernel CalystoScheme
%run --language kx filename.ss
%run --language "kx default" filename.ss

Note: not all languages may support %run.

1.43. Options:

-l –language use the provided language name as kernel [default: None]

1.44. %scheme

This line magic will evaluate the CODE (either expression or statement) as Scheme code.

Examples: %scheme (define x 42) %scheme (import “math”) %scheme (+ x + math.pi)

1.45. %set

This line magic is used to set a variable to a Python value.

Examples: %set x 42 %set x [1, 2, 3]

1.46. %shell

This line command will run the COMMAND in the bash shell.

Examples: %shell ls -al %shell cd

Note: this is a persistent connection to a shell. The working directory is synchronized to that of the notebook before and after each call.

You can also use “!” instead of “%shell”.

2. Cell Magics

2.1. %%activity

a JSON structure

This magic will construct a Python file from the cell’s content, a JSON structure.

Example: %%activity /home/teacher/activity1 {“activity”: “poll”, “instructors”: [“teacher01”], “results_file”: “/home/teacher/activity1.results”, “items”: [{“id”: “…”, “type”: “multiple choice”, “question”: “…”, “options”: [”…”, …] }, …] }

In this example, users will load /home/teacher/activity1

2.2. %%brain

for a calysto.simulation.

Requires calysto.

Examples:

robot.forward(1)

2.3. %%conversation

2.4. %%debug

This cell magic will step through the code in the cell, if the kernel supports debugging.

Example: %%debug

(define x 1)

2.5. %%dot

This cell magic will send the cell to the browser as HTML.

Example: %%dot

graph A { a->b };

2.6. %%file

This cell magic will create or append the cell contents into/onto a file.

Example: %%file -a log.txt This will append this line onto the file “log.txt”

2.7. Options:

-a –append append onto an existing file [default: False]

2.8. %%help

This is MetaKernel Python. It implements a Python interpreter.

2.9. %%html

This cell magic will send the cell to the browser as HTML.

Example: %%html

<script src="..."></script>

<div>Contents of div tag</div>

2.10. %%javascript

This cell magic will execute the contents of the cell as JavaScript in the browser.

Example: %%javascript

element.html("Hello this is <b>bold</b>!")

2.11. %%kx

This cell magic will send the cell to be evaluated by the kernel. The kernel must have been created use the

Returns the result of the execution as output.

Example:

%%kernel bash
ls -al

Use %kernel MODULE CLASS [-k NAME] to create a kernel.

2.12. Options:

-k –kernel_name kernel name given to use for execution [default: None]

2.13. %%latex

This cell magic will display the TEXT in the cell as LaTeX.

Example: %%latex $x_1 = \dfrac{a}{b}$

$x_2 = a^{n - 1}$

2.14. %%macro

This cell macro will learn the macro in the cell. The cell contents are just commands (macros or code in the kernel language).

Example: %%macro test print “Ok!”

%macro test
Ok!

2.15. %%pipe

The pipe cell will “pipe” the contents of a cell through a series of function calls. All of the functions must be defined in the language, and the kernel must support the do_function_direct method.

Example: %%pipe f1 | f2 | f3 CELL CONTENTS

is the same as issuing:

f3(f2(f1("CELL CONTENTS")))

2.16. %%processing

This cell magic will execute the contents of the cell as a Processing program. This uses the Java-based Processing language.

Example:

%%processing
setup() {
}
draw() {
}

2.17. %%px

Example:

%%px
(define x 42)

Use %parallel to initialize the cluster.

2.18. Options:

-s –set_variable set the variable with the parallel results rather than returning them [default: None] -e –evaluate evaluate code in the current kernel, too. The current kernel should be of the same language as the cluster. [default: False] -k –kernel_name kernel name given to use for execution [default: None]

2.19. %%python

This cell magic will evaluate the cell (either expression or statement) as Python code.

Unlike IPython’s Python, this does not return the last expression. To do that, you need to assign the last expression to the special variable “retval”.

The -e or –eval_output flag signals that the retval value expression will be used as code for the cell to be evaluated by the host language.

Note that the version of Python is that of the notebook server.

Examples: %%python x = 42

%%python
import math
retval = x + math.pi

%%python -e
retval = "'(this is code in the kernel language)"

%%python -e
"'(this is code in the kernel language)"

2.20. Options:

-e –eval_output Use the retval value from the Python cell as code in the kernel language. [default: False]

2.21. %%scheme

This cell magic will evaluate the cell (either expression or statement) as Scheme code.

The -e or –eval_output flag signals that the retval value expression will be used as code for the cell to be evaluated by the host language.

Examples: %%scheme (define x 42)

%%scheme
(import "math")
(define retval (+ x math.pi))

%%scheme -e
(define retval "this = code")

%%scheme -e
"this = code"

2.22. Options:

-e –eval_output Use the retval value from the Scheme cell as code in the kernel language. [default: False]

2.23. %%shell

This shell command will run the cell contents in the bash shell.

Example: %%shell cd .. ls -al

Note: this is a persistent connection to a shell. The working directory is synchronized to that of the notebook before and after each call.

You can also use “!!” instead of “%%shell”.

2.24. %%show

This cell magic will put the contents or results of the cell into the system pager.

Examples: %%show This information will appear in the pager.

%%show --output
retval = 54 * 54

2.25. Options:

-o –output rather than showing the contents, show the results [default: False]

2.26. %%time

Put this magic at the top of a cell and the amount of time taken to execute the code will be displayed before the output.

Example: %%time [code for your language goes here!]

This just reports real time taken to execute a program. This may fluctuate with number of users, system, load, etc.

2.27. %%tutor

Online Python Tutor.

Defaults to use the language of the current kernel. ‘python’ is an alias for ‘python3’.

Examples:

a = 1 b = 1 a + b

[You will see an iframe with the pythontutor.com page including the code above.]

public class Test { public Test() { } public static void main(String[] args) { int x = 1; System.out.println(“Hi”); } }

2.28. Options:

-l –language Possible languages to be displayed within the iframe. Possible values are: python, python2, python3, java, javascript