Pressing esc in editing mode will take you to command mode. Pressing enter on any cell will take you into editing mode.
Python Interpreter behavior on Notebook
print 'Hello World'
1 + 3
Hello World
4
22 * 36
792
ans = raw_input('Do you find IPython Awesome? : ')
print ans
Do you find IPython Awesome? : yay yay
Stop execution in the middle using I + I
import time
for _ in xrange(1000):
time.sleep(10)
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-3-61e98a0e0bd2> in <module>() 1 import time 2 for _ in xrange(1000): ----> 3 time.sleep(10) KeyboardInterrupt:
Press tab, when you are stuck and it will auto-complete.
import random
random.randint
Auto-complete also works with file names
open('IPython Notebook.ipynb')
<open file 'IPython Notebook.ipynb', mode 'r' at 0x7f6924fe8ae0>
List IPython Features
?
import time
Get help related to the module
time?
Get function help
time.sleep?
Open 'source code' of the module or function
random??
random.randint??
Use '!'(bang) infront of the shell command and execute it.
!pwd
/home/hummer/Study/talk/IPython
for _ in xrange(2):
!espeak 'Hello World'
Mix python code and shell code
files = !ls
print files
['data.txt', 'demo.ipynb', 'file.txt', 'ipython-in-depth', 'IPython Notebook.ipynb', 'jupyter_logo.svg', 'Jupyter.png', 'kernel_workflow.png', 'Talk.ipynb', 'test.py', 'untitled.txt']
Add python code in shell code using curly braces
files = !ls
print files
print
!echo {files[0].upper()}
['data.txt', 'demo.ipynb', 'file.txt', 'ipython-in-depth', 'IPython Notebook.ipynb', 'jupyter_logo.svg', 'Jupyter.png', 'kernel_workflow.png', 'Talk.ipynb', 'test.py', 'untitled.txt'] DATA.TXT
Wildcard search
import os
os.*path*?
Output history
Out
{2: 792, 5: <open file 'IPython Notebook.ipynb', mode 'r' at 0x7f6924fe8ae0>, 19: 792}
Input History
In
10
History in bash style
%history -n
It can also interpret commands copied from interpreter
>>> from collections import defaultdict
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features.
Ref: %magic
Open IPython quick reference
%quickref
Show help for all IPython Magic functions
%magic
List currently available magic functions.
%lsmagic
Available line magics: %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 Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
Using cell magic to add text to a file
%%file file.txt
You can create a new file in this way.
Just include the syntax above and then write the content below it and file will be created in the server
directory.
Writing file.txt
Reading file the python way
#print open('file.txt').read()
!cat file.txt
You can create a new file in this way. Just include the syntax above and then write the content below it and file will be created in the server directory.
Time execution of a Python statement or expression (This is line magic)
%timeit range(100)
The slowest run took 4.78 times longer than the fastest. This could mean that an intermediate result is being cached 1000000 loops, best of 3: 798 ns per loop
%timeit xrange(100)
The slowest run took 36.86 times longer than the fastest. This could mean that an intermediate result is being cached 10000000 loops, best of 3: 136 ns per loop
This is a cell magic
%%timeit range(100)
range(1000)
100000 loops, best of 3: 9.47 µs per loop
%%bash
echo 'I am in :' $PWD
echo 'Name of this pc is '
whoami
echo 'Files and directories in current directory include: '
ls
I am in : /home/hummer/Study/talk/IPython Name of this pc is hummer Files and directories in current directory include: data.txt demo.ipynb file.txt ipython-in-depth IPython Notebook.ipynb jupyter_logo.svg Jupyter.png kernel_workflow.png Talk.ipynb test.py untitled.txt
def a():
p = 1
q = 'hi'
print p + q
def b():
a()
b()
This command will make trace more verbose than it is.
%xmode verbose
def a():
p = 1
q = 'hi'
print p + q
def b():
a()
b()
This is default mode of the trace.
%xmode context