##// END OF EJS Templates
Merge pull request #8675 from jnothman/force_interact_comment...
Merge pull request #8675 from jnothman/force_interact_comment Note use of -- for -i to behave like cpython

File last commit:

r20547:8f4e2b41
r21557:c1047b2f merge
Show More
Cell Magics.ipynb
689 lines | 16.5 KiB | text/plain | TextLexer
Brian Granger
Adding Cell Magics notebook.
r9195 {
Min RK
upate exmaple notebooks to nbformat v4
r18669 "cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cell Magics in IPython"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "IPython has a system of commands we call 'magics' that provide a mini command language that is orthogonal to the syntax of Python and is extensible by the user with new commands. Magics are meant to be typed interactively, so they use command-line conventions, such as using whitespace for separating arguments, dashes for options and other conventions typical of a command-line environment.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"Magics come in two kinds:\n",
"\n",
"* Line magics: these are commands prepended by one `%` character and whose arguments only extend to the end of the current line.\n",
"* Cell magics: these use *two* percent characters as a marker (`%%`), and they receive as argument *both* the current line where they are declared and the whole body of the cell. Note that cell magics can *only* be used as the first line in a cell, and as a general principle they can't be 'stacked' (i.e. you can only use one cell magic per cell). A few of them, because of how they operate, can be stacked, but that is something you will discover on a case by case basis.\n",
"\n",
"The `%lsmagic` magic is used to list all available magics, and it will show both line and cell magics currently defined:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/json": {
"cell": {
"!": "OSMagics",
"HTML": "Other",
"SVG": "Other",
"bash": "Other",
"capture": "ExecutionMagics",
"debug": "ExecutionMagics",
"file": "Other",
"html": "DisplayMagics",
"javascript": "DisplayMagics",
"latex": "DisplayMagics",
"perl": "Other",
"prun": "ExecutionMagics",
"pypy": "Other",
"python": "Other",
Min RK
pass on IPython Kernel example notebooks...
r20547 "python2": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "python3": "Other",
"ruby": "Other",
"script": "ScriptMagics",
"sh": "Other",
"svg": "DisplayMagics",
"sx": "OSMagics",
"system": "OSMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"writefile": "OSMagics"
},
"line": {
"alias": "OSMagics",
"alias_magic": "BasicMagics",
"autocall": "AutoMagics",
"automagic": "AutoMagics",
"autosave": "KernelMagics",
"bookmark": "OSMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "cat": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "cd": "OSMagics",
"clear": "KernelMagics",
"colors": "BasicMagics",
"config": "ConfigMagics",
"connect_info": "KernelMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "cp": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "debug": "ExecutionMagics",
"dhist": "OSMagics",
"dirs": "OSMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "doctest_mode": "BasicMagics",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "ed": "Other",
"edit": "KernelMagics",
"env": "OSMagics",
"gui": "BasicMagics",
"hist": "Other",
"history": "HistoryMagics",
"install_default_config": "DeprecatedMagics",
"install_ext": "ExtensionMagics",
"install_profiles": "DeprecatedMagics",
"killbgscripts": "ScriptMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "ldir": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "less": "KernelMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "lf": "Other",
"lk": "Other",
"ll": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "load": "CodeMagics",
"load_ext": "ExtensionMagics",
"loadpy": "CodeMagics",
"logoff": "LoggingMagics",
"logon": "LoggingMagics",
"logstart": "LoggingMagics",
"logstate": "LoggingMagics",
"logstop": "LoggingMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "ls": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "lsmagic": "BasicMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "lx": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "macro": "ExecutionMagics",
"magic": "BasicMagics",
"man": "KernelMagics",
"matplotlib": "PylabMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "mkdir": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "more": "KernelMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "mv": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "notebook": "BasicMagics",
"page": "BasicMagics",
"pastebin": "CodeMagics",
"pdb": "ExecutionMagics",
"pdef": "NamespaceMagics",
"pdoc": "NamespaceMagics",
"pfile": "NamespaceMagics",
"pinfo": "NamespaceMagics",
"pinfo2": "NamespaceMagics",
"popd": "OSMagics",
"pprint": "BasicMagics",
"precision": "BasicMagics",
"profile": "BasicMagics",
"prun": "ExecutionMagics",
"psearch": "NamespaceMagics",
"psource": "NamespaceMagics",
"pushd": "OSMagics",
"pwd": "OSMagics",
"pycat": "OSMagics",
"pylab": "PylabMagics",
"qtconsole": "KernelMagics",
"quickref": "BasicMagics",
"recall": "HistoryMagics",
"rehashx": "OSMagics",
"reload_ext": "ExtensionMagics",
"rep": "Other",
"rerun": "HistoryMagics",
"reset": "NamespaceMagics",
"reset_selective": "NamespaceMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "rm": "Other",
"rmdir": "Other",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "run": "ExecutionMagics",
"save": "CodeMagics",
"sc": "OSMagics",
Min RK
pass on IPython Kernel example notebooks...
r20547 "set_env": "OSMagics",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "store": "StoreMagics",
"sx": "OSMagics",
"system": "OSMagics",
"tb": "ExecutionMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"unalias": "OSMagics",
"unload_ext": "ExtensionMagics",
"who": "NamespaceMagics",
"who_ls": "NamespaceMagics",
"whos": "NamespaceMagics",
"xdel": "NamespaceMagics",
"xmode": "BasicMagics"
}
},
"text/plain": [
"Available line magics:\n",
Min RK
pass on IPython Kernel example notebooks...
r20547 "%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %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 %popd %pprint %precision %profile %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\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"Available cell magics:\n",
Min RK
pass on IPython Kernel example notebooks...
r20547 "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"Automagic is ON, % prefix IS NOT needed for line magics."
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%lsmagic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since in the introductory section we already covered the most frequently used line magics, we will focus here on the cell magics, which offer a great amount of power.\n",
"\n",
"Let's load matplotlib and numpy so we can use numerics/plotting at will later on."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <!--====--> Some simple cell magics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Timing the execution of code; the 'timeit' magic exists both in line and cell form:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "100 loops, best of 3: 7.06 ms per loop\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%timeit np.linalg.eigvals(np.random.rand(100,100))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "100 loops, best of 3: 7.4 ms per loop\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%timeit a = np.random.rand(100, 100)\n",
"np.linalg.eigvals(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%%capture` magic can be used to capture the stdout/err of any block of python code, either to discard it (if it's noise to you) or to store it in a variable for later use:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%capture capt\n",
"from __future__ import print_function\n",
"import sys\n",
"print('Hello stdout')\n",
"print('and stderr', file=sys.stderr)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('Hello stdout\\n', 'and stderr\\n')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"capt.stdout, capt.stderr"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello stdout\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
},
{
Min RK
upate exmaple notebooks to nbformat v4
r18669 "name": "stderr",
"output_type": "stream",
"text": [
"and stderr\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"capt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%%writefile` magic is a very useful tool that writes the cell contents as a named file:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing foo.py\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%writefile foo.py\n",
"print('Hello world')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%run foo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <!--====--> Magics for running code under other interpreters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IPython has a `%%script` cell magic, which lets you run a cell in\n",
"a subprocess of any interpreter on your system, such as: bash, ruby, perl, zsh, R, etc.\n",
"\n",
"It can even be a script of your own, which expects input on stdin."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use it, simply pass a path or shell command to the program you want to run on the `%%script` line,\n",
"and the rest of the cell will be run by that script, and stdout/err from the subprocess are captured and displayed."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "hello from Python 2.7.9 (default, Jan 29 2015, 06:27:40) \n",
"[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "%%script python2\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "import sys\n",
"print 'hello from Python %s' % sys.version"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "hello from Python: 3.4.2 |Continuum Analytics, Inc.| (default, Oct 21 2014, 17:42:20) \n",
"[GCC 4.2.1 (Apple Inc. build 5577)]\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%script python3\n",
"import sys\n",
"print('hello from Python: %s' % sys.version)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IPython also creates aliases for a few common interpreters, such as bash, ruby, perl, etc.\n",
"\n",
"These are all equivalent to `%%script <name>`"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "Hello from Ruby 2.0.0\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%ruby\n",
"puts \"Hello from Ruby #{RUBY_VERSION}\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello from /usr/local/bin/bash\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%bash\n",
"echo \"hello from $BASH\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Capturing output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also capture stdout/err from these subprocesses into Python variables, instead of letting them go directly to stdout/err"
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 14,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hi, stdout\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
},
{
Min RK
upate exmaple notebooks to nbformat v4
r18669 "name": "stderr",
"output_type": "stream",
"text": [
"hello, stderr\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"%%bash\n",
"echo \"hi, stdout\"\n",
"echo \"hello, stderr\" >&2\n"
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 15,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash --out output --err error\n",
"echo \"hi, stdout\"\n",
"echo \"hello, stderr\" >&2"
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 16,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello, stderr\n",
Brian Granger
Adding Cell Magics notebook.
r9195 "\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "hi, stdout\n",
"\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
Min RK
upate exmaple notebooks to nbformat v4
r18669 }
],
"source": [
"print(error)\n",
"print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Background Scripts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These scripts can be run in the background, by adding the `--bg` flag.\n",
"\n",
"When you do this, output is discarded unless you use the `--out/err`\n",
"flags to store output as above."
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 17,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting job # 0 in a separate thread.\n"
]
}
],
"source": [
"%%ruby --bg --out ruby_lines\n",
"for n in 1...10\n",
" sleep 1\n",
" puts \"line #{n}\"\n",
" STDOUT.flush\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you do store output of a background thread, these are the stdout/err *pipes*,\n",
"rather than the text of the output."
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 18,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "<_io.BufferedReader name=52>"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 18,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ruby_lines"
]
},
{
"cell_type": "code",
Min RK
pass on IPython Kernel example notebooks...
r20547 "execution_count": 19,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"line 1\n",
"line 2\n",
"line 3\n",
"line 4\n",
"line 5\n",
"line 6\n",
"line 7\n",
"line 8\n",
"line 9\n",
"\n"
Brian Granger
Adding Cell Magics notebook.
r9195 ]
}
],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
pass on IPython Kernel example notebooks...
r20547 "print(ruby_lines.read().decode('utf8'))"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
Brian Granger
Adding Cell Magics notebook.
r9195 }
Min RK
upate exmaple notebooks to nbformat v4
r18669 ],
Min RK
add kernel metadata to example notebooks
r20278 "metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
Min RK
upate exmaple notebooks to nbformat v4
r18669 "nbformat": 4,
"nbformat_minor": 0
Min RK
add kernel metadata to example notebooks
r20278 }