Skip to content

Commit d8d5d2c

Browse files
committed
Add an IPython notebook tour
1 parent 8f8ccc3 commit d8d5d2c

File tree

5 files changed

+359
-0
lines changed

5 files changed

+359
-0
lines changed
Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"cells": [
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# A quick tour of IPython Notebook"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"This tour will work a little better in interactive mode, so it'll be better if you get IPython notebook installed and running. You can start it from a terminal by running\n",
22+
"\n",
23+
"`ipython notebook --pylab inline`\n",
24+
"\n",
25+
"The `--pylab inline` is for plotting graphs"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"First, we need to explain how to run cells. Try to run the cell below!"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"collapsed": false,
38+
"input": [
39+
"import pandas as pd\n",
40+
"\n",
41+
"print \"Hi! This is a cell. Press the \u25b6 button above to run it\""
42+
],
43+
"language": "python",
44+
"metadata": {},
45+
"outputs": []
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that."
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"One of the most useful things about IPython notebook is its tab completion. \n",
59+
"\n",
60+
"Try this: click just after read_csv( in the cell below and press tab 4 times, slowly"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"collapsed": false,
66+
"input": [
67+
"pd.read_csv("
68+
],
69+
"language": "python",
70+
"metadata": {},
71+
"outputs": []
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"After the first time, you should see this:\n",
78+
"<div align=\"center\">\n",
79+
"<img src=\"files/images/tab-once.png\" width=\"75%\">\n",
80+
"</div>\n",
81+
"\n",
82+
"After the second time:\n",
83+
"<div align=\"center\">\n",
84+
"<img src=\"files/images/tab-twice.png\" width=\"75%\">\n",
85+
"</div>\n",
86+
"\n",
87+
"After the fourth time, a big help box should pop up at the bottom of the screen, with the full documentation for the `read_csv` function:\n",
88+
"<div align=\"center\">\n",
89+
"<img src=\"files/images/tab-4-times.png\" width=\"90%\">\n",
90+
"</div>"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"I find this amazingly useful. I think of this as \"the more confused I am, the more times I should press tab\". Nothing bad will happen if you press tab 12 times.\n",
98+
"\n",
99+
"Okay, let's try tab completion for function names!"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"collapsed": false,
105+
"input": [
106+
"pd.r"
107+
],
108+
"language": "python",
109+
"metadata": {},
110+
"outputs": []
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"You should see this:\n",
117+
"\n",
118+
"<div align=\"center\">\n",
119+
"<img src=\"files/images/function-completion.png\" width=\"30%\">\n",
120+
"</div>"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"# Writing code"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"Writing code in the notebook is pretty normal."
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"collapsed": false,
140+
"input": [
141+
"def print_10_nums():\n",
142+
" for i in range(10):\n",
143+
" print i,"
144+
],
145+
"language": "python",
146+
"metadata": {},
147+
"outputs": [],
148+
"prompt_number": 1
149+
},
150+
{
151+
"cell_type": "code",
152+
"collapsed": false,
153+
"input": [
154+
"print_10_nums()"
155+
],
156+
"language": "python",
157+
"metadata": {},
158+
"outputs": [
159+
{
160+
"output_type": "stream",
161+
"stream": "stdout",
162+
"text": [
163+
"0 1 2 3 4 5 6 7 8 9\n"
164+
]
165+
}
166+
],
167+
"prompt_number": 2
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"# Saving"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"As of the latest stable version, the notebook autosaves. You should use the latest stable version. Really."
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"# Magic functions"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"IPython has all kinds of magic functions. Here's an example of comparing `sum()` with a list comprehension to a generator comprehension using the `%time` magic."
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"collapsed": false,
200+
"input": [
201+
"%time sum([x for x in range(100000)])"
202+
],
203+
"language": "python",
204+
"metadata": {},
205+
"outputs": [
206+
{
207+
"output_type": "stream",
208+
"stream": "stdout",
209+
"text": [
210+
"CPU times: user 24 ms, sys: 4 ms, total: 28 ms\n",
211+
"Wall time: 27.4 ms\n"
212+
]
213+
},
214+
{
215+
"metadata": {},
216+
"output_type": "pyout",
217+
"prompt_number": 3,
218+
"text": [
219+
"4999950000"
220+
]
221+
}
222+
],
223+
"prompt_number": 3
224+
},
225+
{
226+
"cell_type": "code",
227+
"collapsed": false,
228+
"input": [
229+
"%time sum(x for x in range(100000))"
230+
],
231+
"language": "python",
232+
"metadata": {},
233+
"outputs": [
234+
{
235+
"output_type": "stream",
236+
"stream": "stdout",
237+
"text": [
238+
"CPU times: user 8 ms, sys: 0 ns, total: 8 ms\n",
239+
"Wall time: 8.11 ms\n"
240+
]
241+
},
242+
{
243+
"metadata": {},
244+
"output_type": "pyout",
245+
"prompt_number": 4,
246+
"text": [
247+
"4999950000"
248+
]
249+
}
250+
],
251+
"prompt_number": 4
252+
},
253+
{
254+
"cell_type": "markdown",
255+
"metadata": {},
256+
"source": [
257+
"The magics I use most are `%time` and `%prun` for profiling. You can run `%magic` to get a list of all of them, and `%quickref` for a reference sheet."
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"collapsed": false,
263+
"input": [
264+
"%quickref"
265+
],
266+
"language": "python",
267+
"metadata": {},
268+
"outputs": [],
269+
"prompt_number": 5
270+
},
271+
{
272+
"cell_type": "markdown",
273+
"metadata": {},
274+
"source": [
275+
"You can also do nutty things like run Perl code in the notebook with cell magics. This is especially cool for things like Cython code, where you can try out Cython really fast with the `%%cython` magic (you'll need to install it)."
276+
]
277+
},
278+
{
279+
"cell_type": "code",
280+
"collapsed": false,
281+
"input": [
282+
"%%perl\n",
283+
"\n",
284+
"$_ = \"whoa, python!\";\n",
285+
"s/python/perl/;\n",
286+
"print"
287+
],
288+
"language": "python",
289+
"metadata": {},
290+
"outputs": [
291+
{
292+
"output_type": "stream",
293+
"stream": "stdout",
294+
"text": [
295+
"whoa, perl!"
296+
]
297+
}
298+
],
299+
"prompt_number": 6
300+
},
301+
{
302+
"cell_type": "markdown",
303+
"metadata": {},
304+
"source": [
305+
"That's it for now!"
306+
]
307+
},
308+
{
309+
"cell_type": "markdown",
310+
"metadata": {},
311+
"source": [
312+
"<style>\n",
313+
" @font-face {\n",
314+
" font-family: \"Computer Modern\";\n",
315+
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
316+
" }\n",
317+
" div.cell{\n",
318+
" width:800px;\n",
319+
" margin-left:16% !important;\n",
320+
" margin-right:auto;\n",
321+
" }\n",
322+
" h1 {\n",
323+
" font-family: Helvetica, serif;\n",
324+
" }\n",
325+
" h4{\n",
326+
" margin-top:12px;\n",
327+
" margin-bottom: 3px;\n",
328+
" }\n",
329+
" div.text_cell_render{\n",
330+
" font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
331+
" line-height: 145%;\n",
332+
" font-size: 130%;\n",
333+
" width:800px;\n",
334+
" margin-left:auto;\n",
335+
" margin-right:auto;\n",
336+
" }\n",
337+
" .CodeMirror{\n",
338+
" font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n",
339+
" }\n",
340+
" .text_cell_render h5 {\n",
341+
" font-weight: 300;\n",
342+
" font-size: 22pt;\n",
343+
" color: #4057A1;\n",
344+
" font-style: italic;\n",
345+
" margin-bottom: .5em;\n",
346+
" margin-top: 0.5em;\n",
347+
" display: block;\n",
348+
" }\n",
349+
" \n",
350+
" .warning{\n",
351+
" color: rgb( 240, 20, 20 )\n",
352+
" } "
353+
]
354+
}
355+
],
356+
"metadata": {}
357+
}
358+
]
359+
}
11.1 KB
Loading

cookbook/images/tab-4-times.png

40.8 KB
Loading

cookbook/images/tab-once.png

16 KB
Loading

cookbook/images/tab-twice.png

34 KB
Loading

0 commit comments

Comments
 (0)