"The IPython Notebook is a web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media into a single document"
Extremely Easy Method:
$ipython or $ipython notebook
Can also be installed via pip
and building from master
1+1
2
%pylab inline
#(import numpy as np and matplotlib)
plt.xkcd()
x = np.arange(0,2*np.pi,.01)
plt.plot(x,np.sin(x))
Populating the interactive namespace from numpy and matplotlib
[<matplotlib.lines.Line2D at 0x105f639d0>]
Execute a cell with:
from IPython.display import HTML
s = """<marquee>DFW Pythoneers!!!</marquee>"""
h = HTML(s); h
%%HTML
<button type="button" id="loading-example-btn" data-loading-text="Loading..." class="btn btn-primary">
Loading state
</button>
%%javascript
console.log('hello world');
<IPython.core.display.Javascript at 0x105f51810>
import bokeh.plotting as bplt
bplt.output_notebook()
bplt.figure()
bplt.line(x, sin(x), color="red")
bplt.show()
Configuring embedded BokehJS mode.
from IPython.display import HTML
HTML('<iframe src=http://fiddle.jshell.net/bokeh/K8P4P/show/ width=600 height=700></iframe>')
import numpy as np
from IPython.display import Audio
framerate = 44100
t = np.linspace(0,5,framerate*5)
data = np.sin(2*np.pi*220*t**2)
Audio(data,rate=framerate)
from IPython.display import YouTubeVideo
YouTubeVideo('xe_ATRmw0KM')
%%latex \begin{align} \frac{\partial u}{\partial t} + \nabla \cdot \left( \boldsymbol{v} u - D\nabla u \right) = f \end{align}
from IPython.display import Latex
from IPython.display import Math
- %lsmagic
- %timeit/%%timeit
- %%file
- %%bash
- %%cythonmagic
%timeit np.linalg.eigvals(np.random.rand(100,100))
100 loops, best of 3: 11.2 ms per loop
%%timeit
a = np.random.rand(100, 100)
np.linalg.eigvals(a)
100 loops, best of 3: 11 ms per loop
%%bash
echo "HELLO WORLD!"
HELLO WORLD!
%%file zenofpython.py
'''this is a brand new file'''
import this
Overwriting zenofpython.py
import zenofpython
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
%load_ext cythonmagic
%%cython --annotate
def slow_f(n):
x = 100.
for i in range(n):
x+=n
return x
def fast_f(int n):
cdef double x=100.
cdef int i
for i in range(n):
x+=n
return x
Generated by Cython 0.20 on Sat Feb 8 09:19:19 2014
1: def slow_f(n):
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":1 * def slow_f(n): # <<<<<<<<<<<<<< * x = 100. * for i in range(n): */ /* Python wrapper */ static PyObject *__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_1slow_f(PyObject *__pyx_self, PyObject *__pyx_v_n); /*proto*/ static PyMethodDef __pyx_mdef_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_1slow_f = {__Pyx_NAMESTR("slow_f"), (PyCFunction)__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_1slow_f, METH_O, __Pyx_DOCSTR(0)}; static PyObject *__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_1slow_f(PyObject *__pyx_self, PyObject *__pyx_v_n) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("slow_f (wrapper)", 0); __pyx_r = __pyx_pf_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_slow_f(__pyx_self, ((PyObject *)__pyx_v_n)); /* function exit code */ __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_slow_f(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_n) { PyObject *__pyx_v_x = NULL; CYTHON_UNUSED PyObject *__pyx_v_i = NULL; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("slow_f", 0); /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":1 * def slow_f(n): # <<<<<<<<<<<<<< * x = 100. * for i in range(n): */ /* function exit code */ __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); __Pyx_XDECREF(__pyx_t_2); __Pyx_AddTraceback("_cython_magic_c1eb1f05956e38e29e7760862d7340e3.slow_f", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XDECREF(__pyx_v_x); __Pyx_XDECREF(__pyx_v_i); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":1 * def slow_f(n): # <<<<<<<<<<<<<< * x = 100. * for i in range(n): */ __pyx_tuple_ = PyTuple_Pack(3, __pyx_n_s_n, __pyx_n_s_x, __pyx_n_s_i); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_tuple_); __Pyx_GIVEREF(__pyx_tuple_); /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":1 * def slow_f(n): # <<<<<<<<<<<<<< * x = 100. * for i in range(n): */ __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_1slow_f, NULL, __pyx_n_s_cython_magic_c1eb1f05956e38e29e); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); if (PyDict_SetItem(__pyx_d, __pyx_n_s_slow_f, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_codeobj__2 = (PyObject*)__Pyx_PyCode_New(1, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple_, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_quasiben_ipython_cython, __pyx_n_s_slow_f, 1, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
2: x = 100.
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":2
* def slow_f(n):
* x = 100. # <<<<<<<<<<<<<<
* for i in range(n):
* x+=n
*/
__Pyx_INCREF(__pyx_float_100_);
__pyx_v_x = __pyx_float_100_;
3: for i in range(n):
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":3 * def slow_f(n): * x = 100. * for i in range(n): # <<<<<<<<<<<<<< * x+=n * return x */ __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); __Pyx_INCREF(__pyx_v_n); PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_n); __Pyx_GIVEREF(__pyx_v_n); __pyx_t_2 = PyObject_Call(__pyx_builtin_range, __pyx_t_1, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; if (PyList_CheckExact(__pyx_t_2) || PyTuple_CheckExact(__pyx_t_2)) { __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_3 = 0; __pyx_t_4 = NULL; } else { __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); __pyx_t_4 = Py_TYPE(__pyx_t_1)->tp_iternext; } __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; for (;;) { if (!__pyx_t_4 && PyList_CheckExact(__pyx_t_1)) { if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break; #if CYTHON_COMPILING_IN_CPYTHON __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} #else __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} #endif } else if (!__pyx_t_4 && PyTuple_CheckExact(__pyx_t_1)) { if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break; #if CYTHON_COMPILING_IN_CPYTHON __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} #else __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} #endif } else { __pyx_t_2 = __pyx_t_4(__pyx_t_1); if (unlikely(!__pyx_t_2)) { PyObject* exc_type = PyErr_Occurred(); if (exc_type) { if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear(); else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} } break; } __Pyx_GOTREF(__pyx_t_2); } __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_2); __pyx_t_2 = 0;
4: x+=n
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":4 * x = 100. * for i in range(n): * x+=n # <<<<<<<<<<<<<< * return x * */ __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_x, __pyx_v_n); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF_SET(__pyx_v_x, __pyx_t_2); __pyx_t_2 = 0; } __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
5: return x
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":5 * for i in range(n): * x+=n * return x # <<<<<<<<<<<<<< * * def fast_f(int n): */ __Pyx_XDECREF(__pyx_r); __Pyx_INCREF(__pyx_v_x); __pyx_r = __pyx_v_x; goto __pyx_L0;
6:
7: def fast_f(int n):
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":7 * return x * * def fast_f(int n): # <<<<<<<<<<<<<< * cdef double x=100. * cdef int i */ /* Python wrapper */ static PyObject *__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_3fast_f(PyObject *__pyx_self, PyObject *__pyx_arg_n); /*proto*/ static PyMethodDef __pyx_mdef_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_3fast_f = {__Pyx_NAMESTR("fast_f"), (PyCFunction)__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_3fast_f, METH_O, __Pyx_DOCSTR(0)}; static PyObject *__pyx_pw_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_3fast_f(PyObject *__pyx_self, PyObject *__pyx_arg_n) { int __pyx_v_n; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("fast_f (wrapper)", 0); assert(__pyx_arg_n); { __pyx_v_n = __Pyx_PyInt_As_int(__pyx_arg_n); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; __Pyx_AddTraceback("_cython_magic_c1eb1f05956e38e29e7760862d7340e3.fast_f", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; __pyx_r = __pyx_pf_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_2fast_f(__pyx_self, ((int)__pyx_v_n)); int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; /* function exit code */ __Pyx_RefNannyFinishContext(); return __pyx_r; } static PyObject *__pyx_pf_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_2fast_f(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_n) { double __pyx_v_x; CYTHON_UNUSED int __pyx_v_i; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("fast_f", 0); /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":7 * return x * * def fast_f(int n): # <<<<<<<<<<<<<< * cdef double x=100. * cdef int i */ /* function exit code */ __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_3); __Pyx_AddTraceback("_cython_magic_c1eb1f05956e38e29e7760862d7340e3.fast_f", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":7 * return x * * def fast_f(int n): # <<<<<<<<<<<<<< * cdef double x=100. * cdef int i */ __pyx_tuple__3 = PyTuple_Pack(4, __pyx_n_s_n, __pyx_n_s_n, __pyx_n_s_x, __pyx_n_s_i); if (unlikely(!__pyx_tuple__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_tuple__3); __Pyx_GIVEREF(__pyx_tuple__3); /* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":7 * return x * * def fast_f(int n): # <<<<<<<<<<<<<< * cdef double x=100. * cdef int i */ __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_c1eb1f05956e38e29e7760862d7340e3_3fast_f, NULL, __pyx_n_s_cython_magic_c1eb1f05956e38e29e); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); if (PyDict_SetItem(__pyx_d, __pyx_n_s_fast_f, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
8: cdef double x=100.
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":8 * * def fast_f(int n): * cdef double x=100. # <<<<<<<<<<<<<< * cdef int i * for i in range(n): */ __pyx_v_x = 100.;
9: cdef int i
10: for i in range(n):
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":10 * cdef double x=100. * cdef int i * for i in range(n): # <<<<<<<<<<<<<< * x+=n * return x */ __pyx_t_1 = __pyx_v_n; for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { __pyx_v_i = __pyx_t_2;
11: x+=n
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":11 * cdef int i * for i in range(n): * x+=n # <<<<<<<<<<<<<< * return x */ __pyx_v_x = (__pyx_v_x + __pyx_v_n); }
12: return x
/* "_cython_magic_c1eb1f05956e38e29e7760862d7340e3.pyx":12 * for i in range(n): * x+=n * return x # <<<<<<<<<<<<<< */ __Pyx_XDECREF(__pyx_r); __pyx_t_3 = PyFloat_FromDouble(__pyx_v_x); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); __pyx_r = __pyx_t_3; __pyx_t_3 = 0; goto __pyx_L0;
I've heard Bang or Shriek --> !
!ls
CGM_DEMO.ipynb LICENSE myfile.txt IPythonOverview.ipynb README.md zenofpython.py IPythonOverview.slides.html ca_website.png zenofpython.pyc
!cat CGM_DEMO.ipynb | sort | uniq -c | sort -r | head
41 { 41 "metadata": {}, 40 }, 21 ] 21 "source": [ 21 "cell_type": "markdown", 20 ], 20 "outputs": [] 20 "language": "python", 20 "input": [
%%ruby
puts "Hello from Ruby #{RUBY_VERSION}"
Hello from Ruby 1.8.7
%load_ext rmagic
##requires rpy2 and rtools and R
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
%%R -i X,Y -o XYcoef;
XYlm = lm(Y~X);
XYcoef = coef(XYlm);
#print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
HTML('<iframe src=http://nbviewer.ipython.org/github/creswick/ihaskell-notebook/blob/master/examples/iHaskell%20Examples.ipynb width=1024 height = 500></iframe>')
Not MapReduce (Data should remain static)
Starts With: $ipcluster start -n 4
from IPython.parallel import Client
c = Client()
c.ids
[0, 1, 2, 3]
c[:].apply_sync(lambda : "Hello, World")
['Hello, World', 'Hello, World', 'Hello, World', 'Hello, World']
IPython Team:
Damián Avila authored the Reveal.js nbconvert utility
$ipython nbconvert IPythonOverview.ipynb --to slides
$ipython nbconvert IPythonOverview.ipynb --to slides --post serve
Interactive viz & analysis on big data, little data, messy data:
from IPython.display import Image
Image(filename='ca_website.png',width=800,height=700)