In a previous post, Interactive coding with IPython, I introduced the ipython as a replacement for running python as a shell.  That alone provides a nice benefit with things like tab completion and history.  However, that's really only scratching the surface of what IPython can do...


The IPython Notebook is a web based interactive session that runs through your favorite browser.  It allows you to rerun commands in place, add comments, save a session, share it...


On the left is a typical ipython session in a text window that connects to a shark device, retrieves and prints the Profiler export settings.  On the right is the same session in an IPython Notebook:

Screen Shot 2013-03-28 at 12.55.45 PM.png

Screen Shot 2013-03-28 at 1.16.36 PM.png

At first glance, it may look like just sugar coating -- adding some pretty borders, syntax highlighting and the like.  That's all great, but the power of the notebook is in the ability to edit and rerun any command on the page at the click of a button.


Let's get started with a simple session like above, then we'll show how to actually modify the shark settings as well.


Step 1: Start IPython Notebook


Starting things up is as simple as adding the word 'notebook' from the command line:

$ ipython notebook

[NotebookApp] Created profile dir: u'/Users/cwhite/.ipython/profile_default'

[NotebookApp] Serving notebooks from /Users/cwhite

[NotebookApp] The IPython Notebook is running at:

[NotebookApp] Use Control-C to stop this server and shut down all kernels.

This starts up a session of ipython as the "kernel" that will actually execute any python commands and also starts up a web server on port 8888.  This should also automatically open up a browser window pointing to that address (if not, just open a browser and point it to


On that starting page, click on "New Notebook" to start up a new notebook.


Step 2: Entering Python Commands

Once the new notebook page starts up, the cursor is automatically placed in the first input box labeled "In[ ]".  At this point you can start typing python commands.  Type the following:

x = 1

y = x * 2

print y

Assuming you hit <enter> after each line, all three lines will show up in the same input box and you don't yet see any output.  This is because the commands are not actually executed yet.  Hit <shift-enter> to execute the entire block of commands (known as a cell) and you should see the following:


Each cell is executed as a block when you hit <shift-enter>.  This allows you to group together related commands, almost like defining a function.  If you're last line results in an object, you get an output block as well:

Try clicking back in the first input cell and modifying x to 4, then hit <shift-enter> twice to see how it re-executes both input blocks and changes the output.


Step 3: Modifying Shark Settings


Let's take this through a concrete example of adding restricting flow export to Profilers with a BPF filter (this is the interactive example of Set the BPF Filter for Flow Export on a Shark).


Enter the following lines (press <enter> between lines in the same input group, and <shift-enter> after the last line in each group):


The output result of that last line may be different for your shark.  To add a BPF Filter, we need to update the first entry in the adapter_ports section as follows:

(Again <shift-enter> after the last line to execute each all the commands).  The first line sets the filter, the second line enables filtering on this port (otherwise the filter would not take effect), and the last line pushes the updated configuration to the shark.


Now, to confirm that our update was taken, we want to retrieve the settings from the Shark again.  Rather than type that in, simply click on the input cell above that calls 'get_profiler_export()' and hit <shift-enter> to rerun that cell.  If all went well, you should see output that looks like the following:



Now if we needed to check a few other sharks, we can simply go back to the line that creates a Shark, change the host it connects to and rerun all the input cells to set up the same BPF filter on that shark as well.


Step 4: Save the Notebook for later


Now we can save our notebook in case we want to execute these commands at some point down the road.  First click on the label "Untitled0" to give it a better name like "Shark Flow Export".  Next, click File -> Save, and this will save a copy of the notebook, the close the session with File -> Close and Halt.  (Note, you may have to hit <control-c> to kill ipython, sometimes it doesn't always shutdown cleanly).


You should now see a file titled "Shark Flow Export.ipynb" in the same directory where you started ipython:

$ ls *.ipynb

Shark Flow Export.ipynb


Next time you start up "ipython notebook" in this directory, you will see this notebook listed and you can open it up to pick up right where you left off.