IPython tips, tricks & notes – Part 1
The course was very useful, and I’m going to post a series of blog posts covering some of the things I’ve learnt. All of the posts will be written for people like me: people who already use IPython or Pandas but may not know some of the slightly more hidden tips and techniques.
Useful Keyboard Shortcuts
Everyone knows the Shift-Return keyboard shortcut to run the current cell in the IPython Notebook, but there are actually three ‘running’ shortcuts that you should know:
- Shift-Return: Run the current cell and move to the cell below
- Ctrl-Return: Run the current cell and stay in that cell
- Opt-Return: Run the current cell, create a new cell below, and move to it
Once you know these you’ll find all sorts of useful opportunities to use them. I now use Ctrl-Return a lot when writing code, running it, changing it, running it again etc – it really speeds that process up!
Also, everyone knows that TAB does autocompletion in IPython, but did you know that Shift-TAB pops up a handy little tooltip giving information about the currently selected item (for example, the argument list for a function, the type of a variable etc. This popup box can be expanded to its full size by clicking the + button on the top right – or, by pressing Shift-TAB again.
%run
and %debug
, but there are loads more that can be really useful. A couple of really useful ones that I wasn’t aware of are:
%%writefile test.txt
This is a test file!
It can contain anything I want...
And more...
Writing test.txt
!cat test.txt
This is a test file! It can contain anything I want... And more...
%xmode
This changes the way that exceptions are displayed in IPython. It can take three options: plain
, context
and verbose
. Let’s have a look at these.
First we create a simple module with a couple of functions, this basically just gives us a way to have a stack trace with multiple functions that leads to a ZeroDivisionError
.
%%writefile mod.py
def f(x):
return 1.0/(x-1)
def g(y):
return f(y+1)
Writing mod.py
context
import mod
mod.g(0)
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-6-a54c5799f57e> in <module>() 1 import mod ----> 2 mod.g(0) /Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py in g(y) 4 5 def g(y): ----> 6 return f(y+1) /Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py in f(x) 1 2 def f(x): ----> 3 return 1.0/(x-1) 4 5 def g(y): ZeroDivisionError: float division by zero
plain
. You can see that you don’t get any context on the lines surrounding the exception – not so helpful!
%xmode plain
Exception reporting mode: Plain
import mod
mod.g(0)
Traceback (most recent call last): File "<ipython-input-8-a54c5799f57e>", line 2, in <module> mod.g(0) File "/Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py", line 6, in g return f(y+1) File "/Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py", line 3, in f return 1.0/(x-1) ZeroDivisionError: float division by zero
verbose
, which gives all of the information that is given by context
but also gives you the values of local and global variables. In the example below you can see that g
was called as g(0)
and f
was called as f(1)
.
%xmode verbose
Exception reporting mode: Verbose
import mod
mod.g(0)
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-10-a54c5799f57e> in <module>() 1 import mod ----> 2 mod.g(0) global mod.g = <function g at 0x10899aa60> /Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py in g(y=0) 4 5 def g(y): ----> 6 return f(y+1) global f = <function f at 0x10899a9d8> y = 0 /Users/robin/code/ngcm/ngcm_ipython_tutorial/Robin'sNotes/mod.py in f(x=1) 1 2 def f(x): ----> 3 return 1.0/(x-1) x = 1 4 5 def g(y): ZeroDivisionError: float division by zero
%load
The load magic loads a Python file, from a filepath or URL, and replaces the contents of the cell with the contents of the file. One really useful application of this is to get example code from the internet. For example, the code %load http://matplotlib.org/mpl_examples/showcase/integral_demo.py
will create a cell containing that matplotlib example.
%connect_info
{ "control_port": 49569, "signature_scheme": "hmac-sha256", "transport": "tcp", "stdin_port": 49568, "key": "59de1682-ef3e-42ca-b393-487693cfc9a2", "ip": "127.0.0.1", "shell_port": 49566, "hb_port": 49570, "iopub_port": 49567 } Paste the above JSON into a file, and connect with: $> ipython <app> --existing <file> or, if you are local, you can connect with just: $> ipython <app> --existing kernel-a5c50dd5-12d3-46dc-81a9-09c0c5b2c974.json or even just: $> ipython <app> --existing if this is the most recent IPython session you have started.
%qtconsole
5+10
15
5+10;
If you found this post useful, please consider buying me a coffee.
This post originally appeared on Robin's Blog.
Categorised as: Programming, Python
That’s cool.
[…] by one_eyed_golfer [link] […]
[…] is really Part 2 of IPython tips, tricks & notes – Part 1, but I thought I’d give it a more self-explanatory […]