sudo apt-get update && sudo apt-get upgrade
brew install ipython
pip install ipython
It's not so much about IPython as the other things in your toolkit.
An enhanced command line with these features
+------------------+----------------------------+
| Python Help | Magic |
| Sytem Aliases | Dynamic Object Information |
| Tab Completion | Searchable History |
| Logging | Session save and restore |
| System Commands | Colorized output |
| Input Caching | Output Caching |
+------------------+----------------------------+
Knowing these two is sufficient
Describes IPython features with hints.
(%q TAB - for the lazy)
%
(or not)?
to see docs on a magic - %reset?
execute a file:
%run file.py
%run
reloads from the disk every time so its good for iterative development.
open foo.py
in your editor and execute it on exit:
%edit foo.py
save IPython lines 9 through 12 into file2.py
:
%save file2.py 9-12
print out all variables in interactive namespace:
%who
print more!!!!!
%whos
reset interactive namespace, clears variables:
%reset
toggle the need for % in front of magics:
%automagic
show all of the many more magics:
%magic
!
!ls
!ls *.py
py_files = !ls *.py
a = range(10)
g = !ping google.com -c 5
g
g. TAB
g.grep('ttl')
who
whos
In [1]: def sq(n):
...: return n*n
...:
In [2]: sq(5)
Out[2]: 25
In [4]: %save sq.py 1
In a new session
In [1]: %run sq.py
In [2]: sq(6)
Out[2]: 36
from IPython.display import Image
Image(filename="austin-relax.png")
Yes, thats what I look like when I relax.
We are going from this ...
Image(filename="ipython-cli.png")
to this ...
Image(filename="ipython-notebook.png")
Just type
ipython notebook
or
ipython notebook --ip=0.0.0.0 --port=9999 -no-browser
# This is a cell
a = 'This '
print a + "is cell output"
This is cell output
code
- in the previous example, executes python codemarkdown
- is a markup language that gets converted to HTMLheader
- HTML Header cellsraw
- Raw Text too!%%
%%capture
- capture cell contents%%writefile
- write cell contents to file%%ruby
puts "Hi #{RUBY_VERSION}"
Hi 1.9.3
%%bash
echo "hi from $BASH"
hi from /bin/bash
To get inline graphics (shown as output in the browser) start each notebook with:
%matplotlib inline
or when you start your notebook server start it with the option:
--pylab=inline
import numpy as np
import matplotlib.pyplot as plt
# don't forget the
# %matplotlib inline
x = np.linspace(0, 10, 20)
y = x ** 2
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x3acda10>]
# from http://scipy-lectures.github.io/packages/scikit-image/
import skimage
from skimage import io
import os
filename = os.path.join(skimage.data_dir, 'camera.png')
camera = skimage.io.imread(filename)
camera.view()
# image is displayed
(3 * camera).view()
The demo server has my Pandas presentation checked out into it. You can use cd
, ls
and %load
or %run
to run my examples.
cd pandas_examples/
ls
%load
# or
%run
vim
and use %run
to load it into notebooknbconvert
subcommand of ipython (it had been a separate tool but was merged).ipython nbconvert mynotebook.ipynb --to html --template basic
ipython nbconvert mynotebook.ipynb --to latex
ipython nbconvert mynotebook.ipynb --to latex --post PDF
ipython nbconvert mynotebook.ipynb --to rst
ipython nbconvert IPython_Presentation.ipynb --to slides
Generate a password hash:
>>> from IPython.lib import passwd
>>> passwd()
Enter password:
Verify password:
'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
Generate the notebook config file
$ ipython profile create nbserver
Place the bash in the config file
# Password to use for web authentication
c = get_config()
c.NotebookApp.password =
u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
Launch the Notebook Server:
ipython notebook --profile==nbserver
Serve it publicly with ngrok:
~/bin/ngrok -proto='https' 8888
# now running on https://e082589425.ngrok.com