Useful iPython Notebook Features!

ipy-notebook-008

The more I use it the more awesome it becomes!

 

Feel free to checkout the installation guides Introduction to the iPython Notebook or Running Spark with iPython Notebook. Otherwise this will be a mere collection of things I’ve discovered while using the Notebook.

iPython Notebooks Official Reference

Keyboard Shortcuts ( h )

Screen Shot 2015-06-24 at 5.33.03 PM

Commands

hello = [1,2,3,4]
hello?
------------------------------------------------------------
Type:        list
String form: [1, 3, 3, 2]
Length:      4
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items


%quickref   --------- list of all magic commands and shortcuts

Text Operations

Select a lines of text and press Tab to indent them to the right and Shit+Tab to indent left. Pressing Command+ / will comment out the selected lines.

Option drag in an active cell allows you to select a box of text.

 

Function Tooltip

If you ever need help with a function or can’t decipher what it’s trying to do, just press Shift+Enter to get a pop up with a description and variable information!

pickle.dump()

Create portable serialized representations of Python objects.

See module copyreg for a mechanism for registering custom picklers.
See module pickletools source for extensive comments.

Screen Shot 2015-06-24 at 1.34.52 PM

It also acts as a debugging tool and let’s you know the values of variables being passed in!

Type:        SList
String form: ['PING google.com (192.124.233.158): 56 data bytes', '64 bytes from 192.124.233.158: icmp_seq=0 t <...> ackets received, 0.0% packet loss', 'round-trip min/avg/max/stddev = 3.760/4.725/7.972/1.626 ms']

Screen Shot 2015-06-24 at 2.29.05 PM

OS Magic Operations

https://ipython.org/ipython-doc/3/interactive/shell.html

To execute a shell command just prepend it with !! and to get a list of all magic commands use %quickref

!!echo Hello world
['Hello World']
!!time ping www.google.com
['PING google.com (192.124.233.173): 56 data bytes',
 '64 bytes from 192.124.233.173: icmp_seq=0 ttl=58 time=3.765 ms',
  ... 
 'round-trip min/avg/max/stddev = 3.626/3.714/3.765/0.050 ms']

and we also have basic directory commands such as

ls -l
total 176464
-rw-r--r--  1 guest  staff      6355 Jun 24 07:13 Netflix.html
-rw-r--r--  1 guest  staff       969 Jun 24 07:13 RunNetflix.in

and now we can combine the commands with variables and process them

ping = !ping -c 5 google.com
['PING google.com (192.124.233.158): 56 data bytes',
 '64 bytes from 192.124.233.158: icmp_seq=0 ttl=58 time=4.977 ms',
 '64 bytes from 192.124.233.158: icmp_seq=1 ttl=58 time=4.411 ms',
 '64 bytes from 192.124.233.158: icmp_seq=2 ttl=58 time=4.147 ms',
 '64 bytes from 192.124.233.158: icmp_seq=3 ttl=58 time=1.668 ms',
 '64 bytes from 192.124.233.158: icmp_seq=4 ttl=58 time=4.086 ms',
 '',
 '--- google.com ping statistics ---',
 '5 packets transmitted, 5 packets received, 0.0% packet loss',
 'round-trip min/avg/max/stddev = 1.668/3.858/4.977/1.139 ms']
l2 = ping.grep('icmp_seq')
['64 bytes from 192.124.233.158: icmp_seq=0 ttl=58 time=4.977 ms',
 '64 bytes from 192.124.233.158: icmp_seq=1 ttl=58 time=4.411 ms',
 '64 bytes from 192.124.233.158: icmp_seq=2 ttl=58 time=4.147 ms',
 '64 bytes from 192.124.233.158: icmp_seq=3 ttl=58 time=1.668 ms',
 '64 bytes from 192.124.233.158: icmp_seq=4 ttl=58 time=4.086 ms']
l3 = l2.fields(1,6)
['bytes time=4.977',
 'bytes time=4.411',
 'bytes time=4.147',
 'bytes time=1.668',
 'bytes time=4.086']
https://damontallen.github.io/IPython-quick-ref-sheets/
From https://damontallen.github.io/IPython-quick-ref-sheets/

Debugging

Debugging will hang the iPython Notebook when not exited properly! So make sure to do an exit() or finish the debugging session before using the Notebook again.

y = [1,2,3,4]
x = "snow"
[x for x in z]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-649b723b21b7> in <module>()
      1 y = [1,2,3,4]
      2 x = "BitTest"
----> 3 [x for x in z]

NameError: name 'z' is not defined

In the very next cell

%debug
> <ipython-input-1-649b723b21b7>(3)<module>()
      1 y = [1,2,3,4]
      2 x = "BitTest"
----> 3 [x for x in z]

ipdb> help
Documented commands (type help ):
========================================
EOF    cl         disable  interact  pdef     quit     source     up    
a      clear      display  j         pdoc     r        step       w     
alias  commands   down     jump      pfile    restart  tbreak     whatis
args   condition  enable   ll        pinfo    return   u          where 
b      cont       exit     longlist  pinfo2   retval   unalias  
break  continue   h        n         pp       run      undisplay
bt     d          help     next      psource  rv       unt      
c      debug      ignore   p         q        s        until    

ipdb> print(x)
BitTest
ipdb> print(y)
[1,2,3,4]

Screen Shot 2015-06-24 at 4.55.21 PM

Advertisements

One thought on “Useful iPython Notebook Features!”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s