#!/usr/bin/env python # coding: utf-8 # # Introduction to IPython Notebook # ###Why do I use IPython notebook: # * Interactive like a REPL # * Durrable artifacts in cells # * Shareable like a document...with built-in annotations! # # A motivating example... # In[7]: from __future__ import division, print_function import numpy as np import pandas as pd # In[8]: get_ipython().system('ls') # In[9]: iris = pd.read_csv("iris.csv") iris.columns # In[10]: #iris['SepalLength'].head() iris.SepalLength.head() # In[11]: iris.head() # In[12]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[13]: plt.scatter(iris.PetalLength, iris.PetalWidth) plt.scatter(iris['PetalLength'], iris['PetalWidth'], c=iris['Species'].factorize()[0]) # --------- # ## Notebook startup # # IPython notebook uses a client/server application. It can be accessed remotely if it lives on a accessible server. However, it is commonly used through localhost. # # * On a command line, navigate to the directory/folder you want to hold the notebook files # * Execute: `ipython notebook` (default port is 8888, specify a port with `--port`) # * In a web browser, open [localhost:8888](localhost:8888) to get the list of notebooks # # You can create multiple notebooks in one directory. You can have multiple servers running at once (different port numbers). I tend to start a server for each project in a directory convenient to the project data. # -------- # ##IPython notebook overview # # Press 'h' in command mode to get a list of keyboard shortcuts # # ### Things I use a lot -- # * ![command] -- Runs [command] in the shell and tries to turn the result into a python object # * %[magic command] -- They call it magic, it means something was made to work in the notebook # * ??[thing] -- Get more help on the thing # * %pdoc [thing] -- Standard python help on the thing # * [stuff]+[tab] does context-aware autocompletion (often used in place of dir([stuff]) # * Shift+Enter -- runs a cell and selects the next # * Ctrl+Enter -- Runs a cell but leaves the current one selected # * Alt+Enter -- runs a cell and inserts a new cell # * Esc -- Stop editing, don't run # # ### Things I sometimes use # * %run [filename] -- Execute a script in the current directory # * %pdb -- toggles the python debugger on/off for the notebook session # * %prun [statement] -- profile the statement # * dd -- Delete the current cell # # ### Things I know other people use -- # * ? -- Get help on ipython notebook # * ?[thing] -- Get help on the thing (think docstring plus a bit) # * Output instavars so you don't have to name things (\_, \_\_, \_3) # * Input instavars...not sure why (\_i, \_\_i, \_\_i3) # # ### Things in the GUI to be aware of # * `File -> Download as` to get a static copy # * `Cell` menu has lots of execution options # * `Help` has handy links to python, markdown and common library's documentation # * The cell type dialog is important for writting documentation and tutorials # In[33]: get_ipython().run_line_magic('pinfo2', 'iris') # In[26]: get_ipython().run_line_magic('pinfo', 'iris') # In[25]: get_ipython().run_line_magic('pdoc', 'iris') # In[29]: get_ipython().system('ls') # In[31]: files = _ files2 = get_ipython().getoutput('ls') print(files2) # In[32]: get_ipython().run_line_magic('prun', 'print(100)') # ##Beyond execution: Sharing Notebooks # # * Markdown cells # * Results are stored in-place # * "Download as...html" for non-volatile version # In[20]: from IPython.display import display from IPython.display import Image display(Image(filename='interestingBugs.jpg')) # And an [honorable mention](http://www.nature.com/news/interactive-notebooks-sharing-the-code-1.16261)