/
intro-jupyter.jupyter
379 lines (345 loc) · 11.5 KB
/
intro-jupyter.jupyter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
nbformat 4
nbformat_minor 0
markdown
# A Short Introduction to Jupyter notebooks (using an IPython kernel)
[back to main page](index.ipynb)
This very page is a Jupyter notebook, so if you are reading this on a static web page, you should download the notebook file (which has the file extension `.ipynb`) and open it with Jupyter/IPython (see below for how that's done).
markdown
## IPython console, Qt console, Jupyter notebook
The IPython (I guess that's short for *interactive* Python) kernel can be used in 3 quite different ways:
* The **classic IPython console** runs in a terminal window (or on a real terminal, if those still exist) and is somewhat similar to the native interactive Python interpreter.
However, it has many significant improvements like tab-completion, color output, "magic" functions, the ability to run shell commands, ...
It can be started with
ipython3
Just give it a try!
If you are really into text-mode applications, you might also want to check out
[bpython](http://bpython-interpreter.org/) and [ptpython](https://github.com/jonathanslenders/ptpython).
---
* The **Qt console** uses the Qt GUI library but still keeps a terminal-like appearance.
The main difference to the classic console is the support for inline graphics/plots and multi-line editing.
It can be started with
ipython3 qtconsole
What are you waiting for? Go ahead and start it!
---
* The **Jupyter notebook** (formerly known as *IPython notebook*) brings your interactive Python session into the browser and opens up a cornucopia of multi-medial greatness (to say the least).
Depending on your installation, the command for opening a notebook (like, for example, this very page) might be one of those:
ipython3 notebook
jupyter3
jupyter3 notebook
jupyter3-notebook
The commands on your system may have the digit `3` in it (to distinguish between Python 3.x and 2.x) or not (if you have only Python 3 installed, anyway).
If you don't know what's the deal with Python 2 vs. 3, just use Python 3!
The rest of this page describes the Jupyter notebook, but some of the commands are also available for the two console applications.
markdown
## Markdown cells, Code cells
Text can be written in [Markdown](http://daringfireball.net/projects/markdown/).
Support for $\LaTeX$ equations: $a^2+b^2=c^2$ (using [MathJax](http://www.mathjax.org/)).
code
metadata
{
"collapsed": true
}
markdown
## Getting Help
code 1
?
metadata
{
"collapsed": false
}
markdown
## "Magic" Functions
code 1
%quickref
metadata
{
"collapsed": false
}
markdown
`qtconsole` can also be started from a running notebook session:
code 2
%qtconsole
metadata
{
"collapsed": false
}
code 5
range?
metadata
{
"collapsed": false
}
code 11
%whos?
metadata
{
"collapsed": true
}
code 9
%whos??
metadata
{
"collapsed": false
}
code 5
%pylab inline
stream stdout
Populating the interactive namespace from numpy and matplotlib
metadata
{
"collapsed": false
}
code 12
# TODO: TikZ extension?
metadata
{
"collapsed": false
}
code 7
%timeit?
# or:
#%run -t
metadata
{
"collapsed": false
}
code 8
%debug?
# or:
#%run -d
metadata
{
"collapsed": true
}
code 9
%who?
metadata
{
"collapsed": false
}
markdown
You can also do crazy things like running shell commands. You can even pass Python names and use the shell output again in Python:
code 10
extension = 'ipynb'
a =! ls *.$extension
a
execute_result
- text/plain
['intro-ipython.ipynb',
'intro-numpy.ipynb',
'intro-python.ipynb',
'simple-signals.ipynb']
metadata
{
"collapsed": false
}
code 11
d =! ddate
print(d.n)
stream stdout
Today is Prickle-Prickle, the 11th day of Discord in the YOLD 3180
metadata
{
"collapsed": false
}
code 12
%%bash
tmp=$(tempfile).txt
echo some text > $tmp
echo more text >> $tmp
sed -i s/text/nonsense/ $tmp
cat $tmp
rm $tmp
stream stdout
some nonsense
more nonsense
metadata
{
"collapsed": false
}
markdown
... but it doesn't stop there, you can also run Ruby, Octave or R code!
This is of course beyond the scope of this little tutorial, if you are interested, have a look at these commands:
%%ruby
%load_ext octavemagic
%%octave
%load_ext rmagic
%%R
See also [Running Scripts from IPython](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Script%20Magics.ipynb)
[Example notebook using Octave](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Octave%20Magic.ipynb)
[Example notebook using R](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/R%20Magics.ipynb)
[Example notebook using Julia](http://nbviewer.ipython.org/github/JuliaLang/IJulia.jl/blob/master/python/doc/JuliaMagic.ipynb)
markdown
Show all available "magic" functions:
code 13
%lsmagic
execute_result
- application/json
{
"cell": {
"!": "OSMagics",
"HTML": "Other",
"SVG": "Other",
"bash": "Other",
"capture": "ExecutionMagics",
"debug": "ExecutionMagics",
"file": "Other",
"html": "DisplayMagics",
"javascript": "DisplayMagics",
"latex": "DisplayMagics",
"perl": "Other",
"prun": "ExecutionMagics",
"pypy": "Other",
"python": "Other",
"python3": "Other",
"ruby": "Other",
"script": "ScriptMagics",
"sh": "Other",
"svg": "DisplayMagics",
"sx": "OSMagics",
"system": "OSMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"writefile": "OSMagics"
},
"line": {
"alias": "OSMagics",
"alias_magic": "BasicMagics",
"autocall": "AutoMagics",
"automagic": "AutoMagics",
"autosave": "KernelMagics",
"bookmark": "OSMagics",
"cd": "OSMagics",
"clear": "KernelMagics",
"colors": "BasicMagics",
"config": "ConfigMagics",
"connect_info": "KernelMagics",
"debug": "ExecutionMagics",
"dhist": "OSMagics",
"dirs": "OSMagics",
"doctest_mode": "KernelMagics",
"ed": "Other",
"edit": "KernelMagics",
"env": "OSMagics",
"gui": "BasicMagics",
"guiref": "Other",
"hist": "Other",
"history": "HistoryMagics",
"install_default_config": "DeprecatedMagics",
"install_ext": "ExtensionMagics",
"install_profiles": "DeprecatedMagics",
"killbgscripts": "ScriptMagics",
"less": "KernelMagics",
"load": "CodeMagics",
"load_ext": "ExtensionMagics",
"loadpy": "CodeMagics",
"logoff": "LoggingMagics",
"logon": "LoggingMagics",
"logstart": "LoggingMagics",
"logstate": "LoggingMagics",
"logstop": "LoggingMagics",
"lsmagic": "BasicMagics",
"macro": "ExecutionMagics",
"magic": "BasicMagics",
"man": "KernelMagics",
"matplotlib": "PylabMagics",
"more": "KernelMagics",
"notebook": "BasicMagics",
"page": "BasicMagics",
"pastebin": "CodeMagics",
"pdb": "ExecutionMagics",
"pdef": "NamespaceMagics",
"pdoc": "NamespaceMagics",
"pfile": "NamespaceMagics",
"pinfo": "NamespaceMagics",
"pinfo2": "NamespaceMagics",
"popd": "OSMagics",
"pprint": "BasicMagics",
"precision": "BasicMagics",
"profile": "BasicMagics",
"prun": "ExecutionMagics",
"psearch": "NamespaceMagics",
"psource": "NamespaceMagics",
"pushd": "OSMagics",
"pwd": "OSMagics",
"pycat": "OSMagics",
"pylab": "PylabMagics",
"qtconsole": "KernelMagics",
"quickref": "BasicMagics",
"recall": "HistoryMagics",
"rehashx": "OSMagics",
"reload_ext": "ExtensionMagics",
"rep": "Other",
"rerun": "HistoryMagics",
"reset": "NamespaceMagics",
"reset_selective": "NamespaceMagics",
"run": "ExecutionMagics",
"save": "CodeMagics",
"sc": "OSMagics",
"store": "StoreMagics",
"sx": "OSMagics",
"system": "OSMagics",
"tb": "ExecutionMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"unalias": "OSMagics",
"unload_ext": "ExtensionMagics",
"who": "NamespaceMagics",
"who_ls": "NamespaceMagics",
"whos": "NamespaceMagics",
"xdel": "NamespaceMagics",
"xmode": "BasicMagics"
}
}
- text/plain
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %colors %config %connect_info %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %guiref %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %man %matplotlib %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
metadata
{
"collapsed": false
}
markdown
IPython can display [all kinds of media](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb) and it can even handle the [HTML5 `<audio>` tag](http://nbviewer.ipython.org/github/Carreau/posts/blob/master/07-the-sound-of-hydrogen.ipynb).
markdown
http://nbviewer.ipython.org/
http://nbdiff.org/
TODO: binder
markdown
## Links
http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/awesome_things_ipynb.ipynb
http://nbgrader.readthedocs.org/
markdown
<p xmlns:dct="http://purl.org/dc/terms/">
<a rel="license"
href="http://creativecommons.org/publicdomain/zero/1.0/">
<img src="http://i.creativecommons.org/p/zero/1.0/88x31.png" style="border-style: none;" alt="CC0" />
</a>
<br />
To the extent possible under law,
<span rel="dct:publisher" resource="[_:publisher]">the person who associated CC0</span>
with this work has waived all copyright and related or neighboring
rights to this work.
</p>
metadata
{
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1+"
}
}