Show More
@@ -1,824 +1,871 | |||||
1 |
|
1 | |||
2 |
|
2 | |||
3 | %% This file was auto-generated by IPython. |
|
3 | %% This file was auto-generated by IPython. | |
4 | %% Conversion from the original notebook file: |
|
4 | %% Conversion from the original notebook file: | |
5 | %% tests/ipynbref/Gun_Data.orig.ipynb |
|
5 | %% tests/ipynbref/Gun_Data.orig.ipynb | |
6 | %% |
|
6 | %% | |
7 | \documentclass[11pt,english]{article} |
|
7 | \documentclass[11pt,english]{article} | |
8 |
|
8 | |||
9 | %% This is the automatic preamble used by IPython. Note that it does *not* |
|
9 | %% This is the automatic preamble used by IPython. Note that it does *not* | |
10 | %% include a documentclass declaration, that is added at runtime to the overall |
|
10 | %% include a documentclass declaration, that is added at runtime to the overall | |
11 | %% document. |
|
11 | %% document. | |
12 |
|
12 | |||
13 | \usepackage{amsmath} |
|
13 | \usepackage{amsmath} | |
14 | \usepackage{amssymb} |
|
14 | \usepackage{amssymb} | |
15 | \usepackage{graphicx} |
|
15 | \usepackage{graphicx} | |
16 | \usepackage{ucs} |
|
16 | \usepackage{ucs} | |
17 | \usepackage[utf8x]{inputenc} |
|
17 | \usepackage[utf8x]{inputenc} | |
18 |
|
18 | |||
19 | % needed for markdown enumerations to work |
|
19 | % needed for markdown enumerations to work | |
20 | \usepackage{enumerate} |
|
20 | \usepackage{enumerate} | |
21 |
|
21 | |||
22 | % Slightly bigger margins than the latex defaults |
|
22 | % Slightly bigger margins than the latex defaults | |
23 | \usepackage{geometry} |
|
23 | \usepackage{geometry} | |
24 | \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm} |
|
24 | \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm} | |
25 |
|
25 | |||
26 | % Define a few colors for use in code, links and cell shading |
|
26 | % Define a few colors for use in code, links and cell shading | |
27 | \usepackage{color} |
|
27 | \usepackage{color} | |
28 | \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
28 | \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} | |
29 | \definecolor{darkorange}{rgb}{.71,0.21,0.01} |
|
29 | \definecolor{darkorange}{rgb}{.71,0.21,0.01} | |
30 | \definecolor{darkgreen}{rgb}{.12,.54,.11} |
|
30 | \definecolor{darkgreen}{rgb}{.12,.54,.11} | |
31 | \definecolor{myteal}{rgb}{.26, .44, .56} |
|
31 | \definecolor{myteal}{rgb}{.26, .44, .56} | |
32 | \definecolor{gray}{gray}{0.45} |
|
32 | \definecolor{gray}{gray}{0.45} | |
33 | \definecolor{lightgray}{gray}{.95} |
|
33 | \definecolor{lightgray}{gray}{.95} | |
34 | \definecolor{mediumgray}{gray}{.8} |
|
34 | \definecolor{mediumgray}{gray}{.8} | |
35 | \definecolor{inputbackground}{rgb}{.95, .95, .85} |
|
35 | \definecolor{inputbackground}{rgb}{.95, .95, .85} | |
36 | \definecolor{outputbackground}{rgb}{.95, .95, .95} |
|
36 | \definecolor{outputbackground}{rgb}{.95, .95, .95} | |
37 | \definecolor{traceback}{rgb}{1, .95, .95} |
|
37 | \definecolor{traceback}{rgb}{1, .95, .95} | |
38 |
|
38 | |||
39 | % Framed environments for code cells (inputs, outputs, errors, ...). The |
|
39 | % Framed environments for code cells (inputs, outputs, errors, ...). The | |
40 | % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't |
|
40 | % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't | |
41 | % randomly change them unless you're sure of the effect it will have. |
|
41 | % randomly change them unless you're sure of the effect it will have. | |
42 | \usepackage{framed} |
|
42 | \usepackage{framed} | |
43 |
|
43 | |||
44 | % remove extraneous vertical space in boxes |
|
44 | % remove extraneous vertical space in boxes | |
45 | \setlength\fboxsep{0pt} |
|
45 | \setlength\fboxsep{0pt} | |
46 |
|
46 | |||
47 | % codecell is the whole input+output set of blocks that a Code cell can |
|
47 | % codecell is the whole input+output set of blocks that a Code cell can | |
48 | % generate. |
|
48 | % generate. | |
49 |
|
49 | |||
50 | % TODO: unfortunately, it seems that using a framed codecell environment breaks |
|
50 | % TODO: unfortunately, it seems that using a framed codecell environment breaks | |
51 | % the ability of the frames inside of it to be broken across pages. This |
|
51 | % the ability of the frames inside of it to be broken across pages. This | |
52 | % causes at least the problem of having lots of empty space at the bottom of |
|
52 | % causes at least the problem of having lots of empty space at the bottom of | |
53 | % pages as new frames are moved to the next page, and if a single frame is too |
|
53 | % pages as new frames are moved to the next page, and if a single frame is too | |
54 | % long to fit on a page, will completely stop latex from compiling the |
|
54 | % long to fit on a page, will completely stop latex from compiling the | |
55 | % document. So unless we figure out a solution to this, we'll have to instead |
|
55 | % document. So unless we figure out a solution to this, we'll have to instead | |
56 | % leave the codecell env. as empty. I'm keeping the original codecell |
|
56 | % leave the codecell env. as empty. I'm keeping the original codecell | |
57 | % definition here (a thin vertical bar) for reference, in case we find a |
|
57 | % definition here (a thin vertical bar) for reference, in case we find a | |
58 | % solution to the page break issue. |
|
58 | % solution to the page break issue. | |
59 |
|
59 | |||
60 | %% \newenvironment{codecell}{% |
|
60 | %% \newenvironment{codecell}{% | |
61 | %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}% |
|
61 | %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}% | |
62 | %% \MakeFramed{\vspace{-0.5em}}} |
|
62 | %% \MakeFramed{\vspace{-0.5em}}} | |
63 | %% {\unskip\endMakeFramed} |
|
63 | %% {\unskip\endMakeFramed} | |
64 |
|
64 | |||
65 | % For now, make this a no-op... |
|
65 | % For now, make this a no-op... | |
66 | \newenvironment{codecell}{} |
|
66 | \newenvironment{codecell}{} | |
67 |
|
67 | |||
68 | \newenvironment{codeinput}{% |
|
68 | \newenvironment{codeinput}{% | |
69 | \def\FrameCommand{\colorbox{inputbackground}}% |
|
69 | \def\FrameCommand{\colorbox{inputbackground}}% | |
70 | \MakeFramed{\advance\hsize-\width \FrameRestore}} |
|
70 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
71 | {\unskip\endMakeFramed} |
|
71 | {\unskip\endMakeFramed} | |
72 |
|
72 | |||
73 | \newenvironment{codeoutput}{% |
|
73 | \newenvironment{codeoutput}{% | |
74 | \def\FrameCommand{\colorbox{outputbackground}}% |
|
74 | \def\FrameCommand{\colorbox{outputbackground}}% | |
75 | \vspace{-1.4em} |
|
75 | \vspace{-1.4em} | |
76 | \MakeFramed{\advance\hsize-\width \FrameRestore}} |
|
76 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
77 | {\unskip\medskip\endMakeFramed} |
|
77 | {\unskip\medskip\endMakeFramed} | |
78 |
|
78 | |||
79 | \newenvironment{traceback}{% |
|
79 | \newenvironment{traceback}{% | |
80 | \def\FrameCommand{\colorbox{traceback}}% |
|
80 | \def\FrameCommand{\colorbox{traceback}}% | |
81 | \MakeFramed{\advance\hsize-\width \FrameRestore}} |
|
81 | \MakeFramed{\advance\hsize-\width \FrameRestore}} | |
82 | {\endMakeFramed} |
|
82 | {\endMakeFramed} | |
83 |
|
83 | |||
84 | % Use and configure listings package for nicely formatted code |
|
84 | % Use and configure listings package for nicely formatted code | |
85 | \usepackage{listingsutf8} |
|
85 | \usepackage{listingsutf8} | |
86 | \lstset{ |
|
86 | \lstset{ | |
87 | language=python, |
|
87 | language=python, | |
88 | inputencoding=utf8x, |
|
88 | inputencoding=utf8x, | |
89 | extendedchars=\true, |
|
89 | extendedchars=\true, | |
90 | aboveskip=\smallskipamount, |
|
90 | aboveskip=\smallskipamount, | |
91 | belowskip=\smallskipamount, |
|
91 | belowskip=\smallskipamount, | |
92 | xleftmargin=2mm, |
|
92 | xleftmargin=2mm, | |
93 | breaklines=true, |
|
93 | breaklines=true, | |
94 | basicstyle=\small \ttfamily, |
|
94 | basicstyle=\small \ttfamily, | |
95 | showstringspaces=false, |
|
95 | showstringspaces=false, | |
96 | keywordstyle=\color{blue}\bfseries, |
|
96 | keywordstyle=\color{blue}\bfseries, | |
97 | commentstyle=\color{myteal}, |
|
97 | commentstyle=\color{myteal}, | |
98 | stringstyle=\color{darkgreen}, |
|
98 | stringstyle=\color{darkgreen}, | |
99 | identifierstyle=\color{darkorange}, |
|
99 | identifierstyle=\color{darkorange}, | |
100 | columns=fullflexible, % tighter character kerning, like verb |
|
100 | columns=fullflexible, % tighter character kerning, like verb | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | % The hyperref package gives us a pdf with properly built |
|
103 | % The hyperref package gives us a pdf with properly built | |
104 | % internal navigation ('pdf bookmarks' for the table of contents, |
|
104 | % internal navigation ('pdf bookmarks' for the table of contents, | |
105 | % internal cross-reference links, web links for URLs, etc.) |
|
105 | % internal cross-reference links, web links for URLs, etc.) | |
106 | \usepackage{hyperref} |
|
106 | \usepackage{hyperref} | |
107 | \hypersetup{ |
|
107 | \hypersetup{ | |
108 | breaklinks=true, % so long urls are correctly broken across lines |
|
108 | breaklinks=true, % so long urls are correctly broken across lines | |
109 | colorlinks=true, |
|
109 | colorlinks=true, | |
110 | urlcolor=blue, |
|
110 | urlcolor=blue, | |
111 | linkcolor=darkorange, |
|
111 | linkcolor=darkorange, | |
112 | citecolor=darkgreen, |
|
112 | citecolor=darkgreen, | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 | % hardcode size of all verbatim environments to be a bit smaller |
|
115 | % hardcode size of all verbatim environments to be a bit smaller | |
116 | \makeatletter |
|
116 | \makeatletter | |
117 | \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt |
|
117 | \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt | |
118 | \makeatother |
|
118 | \makeatother | |
119 |
|
119 | |||
120 | % Prevent overflowing lines due to urls and other hard-to-break entities. |
|
120 | % Prevent overflowing lines due to urls and other hard-to-break entities. | |
121 | \sloppy |
|
121 | \sloppy | |
122 |
|
122 | |||
123 | \begin{document} |
|
123 | ||
124 | \section{A brief tour of the IPython notebook} |
|
124 | ||
|
125 | ||||
|
126 | \begin{document} | |||
|
127 | ||||
|
128 | ||||
|
129 | \section{A brief tour of the IPython notebook} | |||
125 |
|
130 | |||
126 | This document will give you a brief tour of the capabilities of the |
|
131 | This document will give you a brief tour of the capabilities of the | |
127 | IPython notebook.\\You can view its contents by scrolling around, or |
|
132 | IPython notebook.\\You can view its contents by scrolling around, or | |
128 | execute each cell by typing \texttt{Shift-Enter}. After you conclude |
|
133 | execute each cell by typing \texttt{Shift-Enter}. After you conclude | |
129 | this brief high-level tour, you should read the accompanying notebook |
|
134 | this brief high-level tour, you should read the accompanying notebook | |
130 | titled \texttt{01\_notebook\_introduction}, which takes a more |
|
135 | titled \texttt{01\_notebook\_introduction}, which takes a more | |
131 | step-by-step approach to the features of the system. |
|
136 | step-by-step approach to the features of the system. | |
132 |
|
137 | |||
133 | The rest of the notebooks in this directory illustrate various other |
|
138 | The rest of the notebooks in this directory illustrate various other | |
134 | aspects and capabilities of the IPython notebook; some of them may |
|
139 | aspects and capabilities of the IPython notebook; some of them may | |
135 | require additional libraries to be executed. |
|
140 | require additional libraries to be executed. | |
136 |
|
141 | |||
137 | \textbf{NOTE:} This notebook \emph{must} be run from its own directory, |
|
142 | \textbf{NOTE:} This notebook \emph{must} be run from its own directory, | |
138 | so you must \texttt{cd} to this directory and then start the notebook, |
|
143 | so you must \texttt{cd} to this directory and then start the notebook, | |
139 | but do \emph{not} use the \texttt{-{}-notebook-dir} option to run it |
|
144 | but do \emph{not} use the \texttt{-{}-notebook-dir} option to run it | |
140 | from another location. |
|
145 | from another location. | |
141 |
|
146 | |||
142 | The first thing you need to know is that you are still controlling the |
|
147 | The first thing you need to know is that you are still controlling the | |
143 | same old IPython you're used to, so things like shell aliases and magic |
|
148 | same old IPython you're used to, so things like shell aliases and magic | |
144 | commands still work: |
|
149 | commands still work: | |
145 |
|
150 | |||
146 | \begin{codecell} |
|
151 | \begin{codecell} | |
147 | \begin{codeinput} |
|
152 | \begin{codeinput} | |
148 | \begin{lstlisting} |
|
153 | \begin{lstlisting} | |
149 | pwd |
|
154 | pwd | |
150 | \end{lstlisting} |
|
155 | \end{lstlisting} | |
151 | \end{codeinput} |
|
156 | \end{codeinput} | |
152 | \begin{codeoutput} |
|
157 | \begin{codeoutput} | |
153 |
|
158 | |||
|
159 | ||||
|
160 | \begin{verbatim} | |||
154 | u'/Users/minrk/dev/ip/mine/docs/examples/notebooks' |
|
161 | u'/Users/minrk/dev/ip/mine/docs/examples/notebooks' | |
|
162 | \end{verbatim} | |||
|
163 | ||||
155 |
|
164 | |||
156 | \end{codeoutput} |
|
165 | \end{codeoutput} | |
157 | \end{codecell} |
|
166 | \end{codecell} | |
158 | \begin{codecell} |
|
167 | \begin{codecell} | |
159 | \begin{codeinput} |
|
168 | \begin{codeinput} | |
160 | \begin{lstlisting} |
|
169 | \begin{lstlisting} | |
161 | ls |
|
170 | ls | |
162 | \end{lstlisting} |
|
171 | \end{lstlisting} | |
163 | \end{codeinput} |
|
172 | \end{codeinput} | |
164 | \begin{codeoutput} |
|
173 | \begin{codeoutput} | |
165 |
|
174 | |||
166 | \begin{verbatim} |
|
175 | \begin{verbatim} | |
167 | 00_notebook_tour.ipynb callbacks.ipynb python-logo.svg |
|
176 | 00_notebook_tour.ipynb callbacks.ipynb python-logo.svg | |
168 | 01_notebook_introduction.ipynb cython_extension.ipynb rmagic_extension.ipynb |
|
177 | 01_notebook_introduction.ipynb cython_extension.ipynb rmagic_extension.ipynb | |
169 | Animations_and_Progress.ipynb display_protocol.ipynb sympy.ipynb |
|
178 | Animations_and_Progress.ipynb display_protocol.ipynb sympy.ipynb | |
170 | Capturing Output.ipynb formatting.ipynb sympy_quantum_computing.ipynb |
|
179 | Capturing Output.ipynb formatting.ipynb sympy_quantum_computing.ipynb | |
171 | Script Magics.ipynb octavemagic_extension.ipynb trapezoid_rule.ipynb |
|
180 | Script Magics.ipynb octavemagic_extension.ipynb trapezoid_rule.ipynb | |
172 | animation.m4v progbar.ipynb |
|
181 | animation.m4v progbar.ipynb | |
173 |
|
182 | |||
174 | \end{verbatim} |
|
183 | \end{verbatim} | |
175 |
|
184 | |||
176 | \end{codeoutput} |
|
185 | \end{codeoutput} | |
177 | \end{codecell} |
|
186 | \end{codecell} | |
178 | \begin{codecell} |
|
187 | \begin{codecell} | |
179 | \begin{codeinput} |
|
188 | \begin{codeinput} | |
180 | \begin{lstlisting} |
|
189 | \begin{lstlisting} | |
181 | message = 'The IPython notebook is great!' |
|
190 | message = 'The IPython notebook is great!' | |
182 | # note: the echo command does not run on Windows, it's a unix command. |
|
191 | # note: the echo command does not run on Windows, it's a unix command. | |
183 | !echo $message |
|
192 | !echo $message | |
184 | \end{lstlisting} |
|
193 | \end{lstlisting} | |
185 | \end{codeinput} |
|
194 | \end{codeinput} | |
186 | \begin{codeoutput} |
|
195 | \begin{codeoutput} | |
187 |
|
196 | |||
188 | \begin{verbatim} |
|
197 | \begin{verbatim} | |
189 | The IPython notebook is great! |
|
198 | The IPython notebook is great! | |
190 |
|
199 | |||
191 | \end{verbatim} |
|
200 | \end{verbatim} | |
192 |
|
201 | |||
193 | \end{codeoutput} |
|
202 | \end{codeoutput} | |
194 | \end{codecell} |
|
203 | \end{codecell} | |
195 |
|
204 | |||
196 | \section{Plots with matplotlib} |
|
205 | \section{Plots with matplotlib} | |
197 | IPython adds an `inline' matplotlib backend, which embeds any matplotlib |
|
206 | IPython adds an `inline' matplotlib backend, which embeds any matplotlib | |
198 | figures into the notebook. |
|
207 | figures into the notebook. | |
199 |
|
208 | |||
200 | \begin{codecell} |
|
209 | \begin{codecell} | |
201 | \begin{codeinput} |
|
210 | \begin{codeinput} | |
202 | \begin{lstlisting} |
|
211 | \begin{lstlisting} | |
203 | %pylab inline |
|
212 | %pylab inline | |
204 | \end{lstlisting} |
|
213 | \end{lstlisting} | |
205 | \end{codeinput} |
|
214 | \end{codeinput} | |
206 | \begin{codeoutput} |
|
215 | \begin{codeoutput} | |
207 |
|
216 | |||
208 | \begin{verbatim} |
|
217 | \begin{verbatim} | |
209 |
|
218 | |||
210 | Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. |
|
219 | Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. | |
211 | For more information, type 'help(pylab)'. |
|
220 | For more information, type 'help(pylab)'. | |
212 |
|
221 | |||
213 | \end{verbatim} |
|
222 | \end{verbatim} | |
214 |
|
223 | |||
215 | \end{codeoutput} |
|
224 | \end{codeoutput} | |
216 | \end{codecell} |
|
225 | \end{codecell} | |
217 | \begin{codecell} |
|
226 | \begin{codecell} | |
218 | \begin{codeinput} |
|
227 | \begin{codeinput} | |
219 | \begin{lstlisting} |
|
228 | \begin{lstlisting} | |
220 | x = linspace(0, 3*pi, 500) |
|
229 | x = linspace(0, 3*pi, 500) | |
221 | plot(x, sin(x**2)) |
|
230 | plot(x, sin(x**2)) | |
222 | title('A simple chirp'); |
|
231 | title('A simple chirp'); | |
223 | \end{lstlisting} |
|
232 | \end{lstlisting} | |
224 | \end{codeinput} |
|
233 | \end{codeinput} | |
225 | \begin{codeoutput} |
|
234 | \begin{codeoutput} | |
226 | \begin{center} |
|
235 | \begin{center} | |
227 | \includegraphics[width=0.7\textwidth]{_fig_07.png} |
|
236 | \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{_fig_07.png} | |
228 | \par |
|
237 | \par | |
229 | \end{center} |
|
238 | \end{center} | |
230 | \end{codeoutput} |
|
239 | \end{codeoutput} | |
231 | \end{codecell} |
|
240 | \end{codecell} | |
232 | You can paste blocks of input with prompt markers, such as those from |
|
241 | You can paste blocks of input with prompt markers, such as those from | |
233 | \href{http://docs.python.org/tutorial/interpreter.html\#interactive-mode}{the |
|
242 | \href{http://docs.python.org/tutorial/interpreter.html\#interactive-mode}{the | |
234 | official Python tutorial} |
|
243 | official Python tutorial} | |
235 |
|
244 | |||
236 | \begin{codecell} |
|
245 | \begin{codecell} | |
237 | \begin{codeinput} |
|
246 | \begin{codeinput} | |
238 | \begin{lstlisting} |
|
247 | \begin{lstlisting} | |
239 | >>> the_world_is_flat = 1 |
|
248 | >>> the_world_is_flat = 1 | |
240 | >>> if the_world_is_flat: |
|
249 | >>> if the_world_is_flat: | |
241 | ... print "Be careful not to fall off!" |
|
250 | ... print "Be careful not to fall off!" | |
242 | \end{lstlisting} |
|
251 | \end{lstlisting} | |
243 | \end{codeinput} |
|
252 | \end{codeinput} | |
244 | \begin{codeoutput} |
|
253 | \begin{codeoutput} | |
245 |
|
254 | |||
246 | \begin{verbatim} |
|
255 | \begin{verbatim} | |
247 | Be careful not to fall off! |
|
256 | Be careful not to fall off! | |
248 |
|
257 | |||
249 | \end{verbatim} |
|
258 | \end{verbatim} | |
250 |
|
259 | |||
251 | \end{codeoutput} |
|
260 | \end{codeoutput} | |
252 | \end{codecell} |
|
261 | \end{codecell} | |
253 | Errors are shown in informative ways: |
|
262 | Errors are shown in informative ways: | |
254 |
|
263 | |||
255 | \begin{codecell} |
|
264 | \begin{codecell} | |
256 | \begin{codeinput} |
|
265 | \begin{codeinput} | |
257 | \begin{lstlisting} |
|
266 | \begin{lstlisting} | |
258 | %run non_existent_file |
|
267 | %run non_existent_file | |
259 | \end{lstlisting} |
|
268 | \end{lstlisting} | |
260 | \end{codeinput} |
|
269 | \end{codeinput} | |
261 | \begin{codeoutput} |
|
270 | \begin{codeoutput} | |
262 |
|
271 | |||
263 | \begin{verbatim} |
|
272 | \begin{verbatim} | |
264 | ERROR: File `u'non_existent_file.py'` not found. |
|
273 | ERROR: File `u'non_existent_file.py'` not found. | |
265 | \end{verbatim} |
|
274 | \end{verbatim} | |
266 |
|
275 | |||
267 | \end{codeoutput} |
|
276 | \end{codeoutput} | |
268 | \end{codecell} |
|
277 | \end{codecell} | |
269 | \begin{codecell} |
|
278 | \begin{codecell} | |
270 | \begin{codeinput} |
|
279 | \begin{codeinput} | |
271 | \begin{lstlisting} |
|
280 | \begin{lstlisting} | |
272 | x = 1 |
|
281 | x = 1 | |
273 | y = 4 |
|
282 | y = 4 | |
274 | z = y/(1-x) |
|
283 | z = y/(1-x) | |
275 | \end{lstlisting} |
|
284 | \end{lstlisting} | |
276 | \end{codeinput} |
|
285 | \end{codeinput} | |
277 | \begin{codeoutput} |
|
286 | \begin{codeoutput} | |
278 |
|
287 | |||
279 | --------------------------------------------------------------------------- |
|
288 | --------------------------------------------------------------------------- | |
280 | ZeroDivisionError Traceback (most recent call last) |
|
289 | ZeroDivisionError Traceback (most recent call last) | |
281 | <ipython-input-8-dc39888fd1d2> in <module>() |
|
290 | <ipython-input-8-dc39888fd1d2> in <module>() | |
282 | 1 x = 1 |
|
291 | 1 x = 1 | |
283 | 2 y = 4 |
|
292 | 2 y = 4 | |
284 | ----> 3 z = y/(1-x) |
|
293 | ----> 3 z = y/(1-x) | |
285 |
|
294 | |||
286 | ZeroDivisionError: integer division or modulo by zero |
|
295 | ZeroDivisionError: integer division or modulo by zero | |
287 |
|
296 | |||
288 | \end{codeoutput} |
|
297 | \end{codeoutput} | |
289 | \end{codecell} |
|
298 | \end{codecell} | |
290 | When IPython needs to display additional information (such as providing |
|
299 | When IPython needs to display additional information (such as providing | |
291 | details on an object via \texttt{x?} it will automatically invoke a |
|
300 | details on an object via \texttt{x?} it will automatically invoke a | |
292 | pager at the bottom of the screen: |
|
301 | pager at the bottom of the screen: | |
293 |
|
302 | |||
294 | \begin{codecell} |
|
303 | \begin{codecell} | |
295 | \begin{codeinput} |
|
304 | \begin{codeinput} | |
296 | \begin{lstlisting} |
|
305 | \begin{lstlisting} | |
297 | magic |
|
306 | magic | |
298 | \end{lstlisting} |
|
307 | \end{lstlisting} | |
299 | \end{codeinput} |
|
308 | \end{codeinput} | |
300 |
|
309 | |||
301 | \end{codecell} |
|
310 | \end{codecell} | |
302 | \subsection{Non-blocking output of kernel} |
|
311 | \subsection{Non-blocking output of kernel} | |
303 |
|
312 | |||
304 | If you execute the next cell, you will see the output arriving as it is |
|
313 | If you execute the next cell, you will see the output arriving as it is | |
305 | generated, not all at the end. |
|
314 | generated, not all at the end. | |
306 |
|
315 | |||
307 | \begin{codecell} |
|
316 | \begin{codecell} | |
308 | \begin{codeinput} |
|
317 | \begin{codeinput} | |
309 | \begin{lstlisting} |
|
318 | \begin{lstlisting} | |
310 | import time, sys |
|
319 | import time, sys | |
311 | for i in range(8): |
|
320 | for i in range(8): | |
312 | print i, |
|
321 | print i, | |
313 | time.sleep(0.5) |
|
322 | time.sleep(0.5) | |
314 | \end{lstlisting} |
|
323 | \end{lstlisting} | |
315 | \end{codeinput} |
|
324 | \end{codeinput} | |
316 | \begin{codeoutput} |
|
325 | \begin{codeoutput} | |
317 |
|
326 | |||
318 | \begin{verbatim} |
|
327 | \begin{verbatim} | |
319 | 0 1 2 3 4 5 6 7 |
|
328 | 0 1 2 3 4 5 6 7 | |
|
329 | ||||
320 | \end{verbatim} |
|
330 | \end{verbatim} | |
321 |
|
331 | |||
322 | \end{codeoutput} |
|
332 | \end{codeoutput} | |
323 | \end{codecell} |
|
333 | \end{codecell} | |
324 | \subsection{Clean crash and restart} |
|
334 | \subsection{Clean crash and restart} | |
325 |
|
335 | |||
326 | We call the low-level system libc.time routine with the wrong argument |
|
336 | We call the low-level system libc.time routine with the wrong argument | |
327 | via ctypes to segfault the Python interpreter: |
|
337 | via ctypes to segfault the Python interpreter: | |
328 |
|
338 | |||
329 | \begin{codecell} |
|
339 | \begin{codecell} | |
330 | \begin{codeinput} |
|
340 | \begin{codeinput} | |
331 | \begin{lstlisting} |
|
341 | \begin{lstlisting} | |
332 | import sys |
|
342 | import sys | |
333 | from ctypes import CDLL |
|
343 | from ctypes import CDLL | |
334 | # This will crash a Linux or Mac system; equivalent calls can be made on Windows |
|
344 | # This will crash a Linux or Mac system; equivalent calls can be made on Windows | |
335 | dll = 'dylib' if sys.platform == 'darwin' else '.so.6' |
|
345 | dll = 'dylib' if sys.platform == 'darwin' else '.so.6' | |
336 | libc = CDLL("libc.%s" % dll) |
|
346 | libc = CDLL("libc.%s" % dll) | |
337 | libc.time(-1) # BOOM!! |
|
347 | libc.time(-1) # BOOM!! | |
338 | \end{lstlisting} |
|
348 | \end{lstlisting} | |
339 | \end{codeinput} |
|
349 | \end{codeinput} | |
340 |
|
350 | |||
341 | \end{codecell} |
|
351 | \end{codecell} | |
342 | \subsection{Markdown cells can contain formatted text and code} |
|
352 | \subsection{Markdown cells can contain formatted text and code} | |
343 |
|
353 | |||
344 | You can \emph{italicize}, \textbf{boldface} |
|
354 | You can \emph{italicize}, \textbf{boldface} | |
345 |
|
355 | |||
346 | \begin{itemize} |
|
356 | \begin{itemize} | |
347 | \item |
|
357 | \item | |
348 | build |
|
358 | build | |
349 | \item |
|
359 | \item | |
350 | lists |
|
360 | lists | |
351 | \end{itemize} |
|
361 | \end{itemize} | |
352 | and embed code meant for illustration instead of execution in Python: |
|
362 | and embed code meant for illustration instead of execution in Python: | |
353 |
|
363 | |||
354 | \begin{verbatim} |
|
364 | \begin{verbatim} | |
355 | def f(x): |
|
365 | def f(x): | |
356 | """a docstring""" |
|
366 | """a docstring""" | |
357 | return x**2 |
|
367 | return x**2 | |
358 | \end{verbatim} |
|
368 | \end{verbatim} | |
359 | or other languages: |
|
369 | or other languages: | |
360 |
|
370 | |||
361 | \begin{verbatim} |
|
371 | \begin{verbatim} | |
362 | if (i=0; i<n; i++) { |
|
372 | if (i=0; i<n; i++) { | |
363 | printf("hello %d\n", i); |
|
373 | printf("hello %d\n", i); | |
364 | x += 4; |
|
374 | x += 4; | |
365 | } |
|
375 | } | |
366 | \end{verbatim} |
|
376 | \end{verbatim} | |
367 |
|
377 | |||
368 |
|
378 | |||
369 | Courtesy of MathJax, you can include mathematical expressions both |
|
379 | Courtesy of MathJax, you can include mathematical expressions both | |
370 | inline: $e^{i\pi} + 1 = 0$ and displayed: |
|
380 | inline: $e^{i\pi} + 1 = 0$ and displayed: | |
371 |
|
381 | |||
372 |
\[ |
|
382 | \[^x=\sum_{i=0}^\infty \frac{1}{i!}x^i\] | |
373 |
|
383 | |||
374 | \subsection{Rich displays: include anyting a browser can show} |
|
384 | \subsection{Rich displays: include anyting a browser can show} | |
375 |
|
385 | |||
376 | Note that we have an actual protocol for this, see the |
|
386 | Note that we have an actual protocol for this, see the | |
377 | \texttt{display\_protocol} notebook for further details. |
|
387 | \texttt{display\_protocol} notebook for further details. | |
378 |
|
388 | |||
379 | \subsubsection{Images} |
|
389 | \subsubsection{Images} | |
380 |
|
390 | |||
381 |
|
391 | |||
382 | \begin{codecell} |
|
392 | \begin{codecell} | |
383 | \begin{codeinput} |
|
393 | \begin{codeinput} | |
384 | \begin{lstlisting} |
|
394 | \begin{lstlisting} | |
385 | from IPython.display import Image |
|
395 | from IPython.display import Image | |
386 | Image(filename='../../source/_static/logo.png') |
|
396 | Image(filename='../../source/_static/logo.png') | |
387 | \end{lstlisting} |
|
397 | \end{lstlisting} | |
388 | \end{codeinput} |
|
398 | \end{codeinput} | |
389 | \begin{codeoutput} |
|
399 | \begin{codeoutput} | |
390 |
|
400 | |||
391 | <IPython.core.display.Image at 0x10faeafd0> |
|
401 | \begin{center} | |
|
402 | \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{_fig_22.png} | |||
|
403 | \par | |||
|
404 | \end{center} | |||
392 |
|
405 | |||
393 | \end{codeoutput} |
|
406 | \end{codeoutput} | |
394 | \end{codecell} |
|
407 | \end{codecell} | |
395 | An image can also be displayed from raw data or a url |
|
408 | An image can also be displayed from raw data or a url | |
396 |
|
409 | |||
397 | \begin{codecell} |
|
410 | \begin{codecell} | |
398 | \begin{codeinput} |
|
411 | \begin{codeinput} | |
399 | \begin{lstlisting} |
|
412 | \begin{lstlisting} | |
400 | Image(url='http://python.org/images/python-logo.gif') |
|
413 | Image(url='http://python.org/images/python-logo.gif') | |
401 | \end{lstlisting} |
|
414 | \end{lstlisting} | |
402 | \end{codeinput} |
|
415 | \end{codeinput} | |
403 | \begin{codeoutput} |
|
416 | \begin{codeoutput} | |
404 |
|
417 | |||
|
418 | ||||
|
419 | \begin{verbatim} | |||
405 | <IPython.core.display.Image at 0x1060e7410> |
|
420 | <IPython.core.display.Image at 0x1060e7410> | |
|
421 | \end{verbatim} | |||
|
422 | ||||
406 |
|
423 | |||
407 | \end{codeoutput} |
|
424 | \end{codeoutput} | |
408 | \end{codecell} |
|
425 | \end{codecell} | |
409 | SVG images are also supported out of the box (since modern browsers do a |
|
426 | SVG images are also supported out of the box (since modern browsers do a | |
410 | good job of rendering them): |
|
427 | good job of rendering them): | |
411 |
|
428 | |||
412 | \begin{codecell} |
|
429 | \begin{codecell} | |
413 | \begin{codeinput} |
|
430 | \begin{codeinput} | |
414 | \begin{lstlisting} |
|
431 | \begin{lstlisting} | |
415 | from IPython.display import SVG |
|
432 | from IPython.display import SVG | |
416 | SVG(filename='python-logo.svg') |
|
433 | SVG(filename='python-logo.svg') | |
417 | \end{lstlisting} |
|
434 | \end{lstlisting} | |
418 | \end{codeinput} |
|
435 | \end{codeinput} | |
419 | \begin{codeoutput} |
|
436 | \begin{codeoutput} | |
420 |
|
437 | |||
421 | <IPython.core.display.SVG at 0x10fb998d0> |
|
438 | \begin{center} | |
|
439 | \includegraphics[width=0.7\textwidth]{_fig_26.pdf} | |||
|
440 | \par | |||
|
441 | \end{center} | |||
422 |
|
442 | |||
423 | \end{codeoutput} |
|
443 | \end{codeoutput} | |
424 | \end{codecell} |
|
444 | \end{codecell} | |
425 | \paragraph{Embedded vs Non-embedded Images} |
|
445 | \paragraph{Embedded vs Non-embedded Images} | |
426 |
|
446 | |||
427 |
|
447 | |||
428 | As of IPython 0.13, images are embedded by default for compatibility |
|
448 | As of IPython 0.13, images are embedded by default for compatibility | |
429 | with QtConsole, and the ability to still be displayed offline. |
|
449 | with QtConsole, and the ability to still be displayed offline. | |
430 |
|
450 | |||
431 | Let's look at the differences: |
|
451 | Let's look at the differences: | |
432 |
|
452 | |||
433 | \begin{codecell} |
|
453 | \begin{codecell} | |
434 | \begin{codeinput} |
|
454 | \begin{codeinput} | |
435 | \begin{lstlisting} |
|
455 | \begin{lstlisting} | |
436 | # by default Image data are embedded |
|
456 | # by default Image data are embedded | |
437 | Embed = Image( 'http://scienceview.berkeley.edu/view/images/newview.jpg') |
|
457 | Embed = Image( 'http://scienceview.berkeley.edu/view/images/newview.jpg') | |
438 |
|
458 | |||
439 | # if kwarg `url` is given, the embedding is assumed to be false |
|
459 | # if kwarg `url` is given, the embedding is assumed to be false | |
440 | SoftLinked = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg') |
|
460 | SoftLinked = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg') | |
441 |
|
461 | |||
442 | # In each case, embed can be specified explicitly with the `embed` kwarg |
|
462 | # In each case, embed can be specified explicitly with the `embed` kwarg | |
443 | # ForceEmbed = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg', embed=True) |
|
463 | # ForceEmbed = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg', embed=True) | |
444 | \end{lstlisting} |
|
464 | \end{lstlisting} | |
445 | \end{codeinput} |
|
465 | \end{codeinput} | |
446 |
|
466 | |||
447 | \end{codecell} |
|
467 | \end{codecell} | |
448 | Today's image from a webcam at Berkeley, (at the time I created this |
|
468 | Today's image from a webcam at Berkeley, (at the time I created this | |
449 | notebook). This should also work in the Qtconsole. Drawback is that the |
|
469 | notebook). This should also work in the Qtconsole. Drawback is that the | |
450 | saved notebook will be larger, but the image will still be present |
|
470 | saved notebook will be larger, but the image will still be present | |
451 | offline. |
|
471 | offline. | |
452 |
|
472 | |||
453 | \begin{codecell} |
|
473 | \begin{codecell} | |
454 | \begin{codeinput} |
|
474 | \begin{codeinput} | |
455 | \begin{lstlisting} |
|
475 | \begin{lstlisting} | |
456 | Embed |
|
476 | Embed | |
457 | \end{lstlisting} |
|
477 | \end{lstlisting} | |
458 | \end{codeinput} |
|
478 | \end{codeinput} | |
459 | \begin{codeoutput} |
|
479 | \begin{codeoutput} | |
460 |
|
480 | |||
461 | <IPython.core.display.Image at 0x10fb99b50> |
|
481 | ||
462 |
|
482 | |||
463 | \end{codeoutput} |
|
483 | \end{codeoutput} | |
464 | \end{codecell} |
|
484 | \end{codecell} | |
465 | Today's image from same webcam at Berkeley, (refreshed every minutes, if |
|
485 | Today's image from same webcam at Berkeley, (refreshed every minutes, if | |
466 | you reload the notebook), visible only with an active internet |
|
486 | you reload the notebook), visible only with an active internet | |
467 | connexion, that should be different from the previous one. This will not |
|
487 | connexion, that should be different from the previous one. This will not | |
468 | work on Qtconsole. Notebook saved with this kind of image will be |
|
488 | work on Qtconsole. Notebook saved with this kind of image will be | |
469 | lighter and always reflect the current version of the source, but the |
|
489 | lighter and always reflect the current version of the source, but the | |
470 | image won't display offline. |
|
490 | image won't display offline. | |
471 |
|
491 | |||
472 | \begin{codecell} |
|
492 | \begin{codecell} | |
473 | \begin{codeinput} |
|
493 | \begin{codeinput} | |
474 | \begin{lstlisting} |
|
494 | \begin{lstlisting} | |
475 | SoftLinked |
|
495 | SoftLinked | |
476 | \end{lstlisting} |
|
496 | \end{lstlisting} | |
477 | \end{codeinput} |
|
497 | \end{codeinput} | |
478 | \begin{codeoutput} |
|
498 | \begin{codeoutput} | |
479 |
|
499 | |||
|
500 | ||||
|
501 | \begin{verbatim} | |||
480 | <IPython.core.display.Image at 0x10fb99b10> |
|
502 | <IPython.core.display.Image at 0x10fb99b10> | |
|
503 | \end{verbatim} | |||
|
504 | ||||
481 |
|
505 | |||
482 | \end{codeoutput} |
|
506 | \end{codeoutput} | |
483 | \end{codecell} |
|
507 | \end{codecell} | |
484 | Of course, if you re-run the all notebook, the two images will be the |
|
508 | Of course, if you re-run the all notebook, the two images will be the | |
485 | same again. |
|
509 | same again. | |
486 |
|
510 | |||
487 | \subsubsection{Video} |
|
511 | \subsubsection{Video} | |
488 |
|
512 | |||
489 |
|
513 | |||
490 | And more exotic objects can also be displayed, as long as their |
|
514 | And more exotic objects can also be displayed, as long as their | |
491 | representation supports the IPython display protocol. |
|
515 | representation supports the IPython display protocol. | |
492 |
|
516 | |||
493 | For example, videos hosted externally on YouTube are easy to load (and |
|
517 | For example, videos hosted externally on YouTube are easy to load (and | |
494 | writing a similar wrapper for other hosted content is trivial): |
|
518 | writing a similar wrapper for other hosted content is trivial): | |
495 |
|
519 | |||
496 | \begin{codecell} |
|
520 | \begin{codecell} | |
497 | \begin{codeinput} |
|
521 | \begin{codeinput} | |
498 | \begin{lstlisting} |
|
522 | \begin{lstlisting} | |
499 | from IPython.display import YouTubeVideo |
|
523 | from IPython.display import YouTubeVideo | |
500 | # a talk about IPython at Sage Days at U. Washington, Seattle. |
|
524 | # a talk about IPython at Sage Days at U. Washington, Seattle. | |
501 | # Video credit: William Stein. |
|
525 | # Video credit: William Stein. | |
502 | YouTubeVideo('1j_HxD4iLn8') |
|
526 | YouTubeVideo('1j_HxD4iLn8') | |
503 | \end{lstlisting} |
|
527 | \end{lstlisting} | |
504 | \end{codeinput} |
|
528 | \end{codeinput} | |
505 | \begin{codeoutput} |
|
529 | \begin{codeoutput} | |
506 |
|
530 | |||
|
531 | ||||
|
532 | \begin{verbatim} | |||
507 | <IPython.lib.display.YouTubeVideo at 0x10fba2190> |
|
533 | <IPython.lib.display.YouTubeVideo at 0x10fba2190> | |
|
534 | \end{verbatim} | |||
|
535 | ||||
508 |
|
536 | |||
509 | \end{codeoutput} |
|
537 | \end{codeoutput} | |
510 | \end{codecell} |
|
538 | \end{codecell} | |
511 | Using the nascent video capabilities of modern browsers, you may also be |
|
539 | Using the nascent video capabilities of modern browsers, you may also be | |
512 | able to display local videos. At the moment this doesn't work very well |
|
540 | able to display local videos. At the moment this doesn't work very well | |
513 | in all browsers, so it may or may not work for you; we will continue |
|
541 | in all browsers, so it may or may not work for you; we will continue | |
514 | testing this and looking for ways to make it more robust. |
|
542 | testing this and looking for ways to make it more robust. | |
515 |
|
543 | |||
516 | The following cell loads a local file called \texttt{animation.m4v}, |
|
544 | The following cell loads a local file called \texttt{animation.m4v}, | |
517 | encodes the raw video as base64 for http transport, and uses the HTML5 |
|
545 | encodes the raw video as base64 for http transport, and uses the HTML5 | |
518 | video tag to load it. On Chrome 15 it works correctly, displaying a |
|
546 | video tag to load it. On Chrome 15 it works correctly, displaying a | |
519 | control bar at the bottom with a play/pause button and a location |
|
547 | control bar at the bottom with a play/pause button and a location | |
520 | slider. |
|
548 | slider. | |
521 |
|
549 | |||
522 | \begin{codecell} |
|
550 | \begin{codecell} | |
523 | \begin{codeinput} |
|
551 | \begin{codeinput} | |
524 | \begin{lstlisting} |
|
552 | \begin{lstlisting} | |
525 | from IPython.display import HTML |
|
553 | from IPython.display import HTML | |
526 | video = open("animation.m4v", "rb").read() |
|
554 | video = open("animation.m4v", "rb").read() | |
527 | video_encoded = video.encode("base64") |
|
555 | video_encoded = video.encode("base64") | |
528 | video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded) |
|
556 | video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded) | |
529 | HTML(data=video_tag) |
|
557 | HTML(data=video_tag) | |
530 | \end{lstlisting} |
|
558 | \end{lstlisting} | |
531 | \end{codeinput} |
|
559 | \end{codeinput} | |
532 | \begin{codeoutput} |
|
560 | \begin{codeoutput} | |
533 |
|
561 | |||
|
562 | ||||
|
563 | \begin{verbatim} | |||
534 | <IPython.core.display.HTML at 0x10fba28d0> |
|
564 | <IPython.core.display.HTML at 0x10fba28d0> | |
|
565 | \end{verbatim} | |||
|
566 | ||||
535 |
|
567 | |||
536 | \end{codeoutput} |
|
568 | \end{codeoutput} | |
537 | \end{codecell} |
|
569 | \end{codecell} | |
538 | \subsection{Local Files} |
|
570 | \subsection{Local Files} | |
539 |
|
571 | |||
540 | The above examples embed images and video from the notebook filesystem |
|
572 | The above examples embed images and video from the notebook filesystem | |
541 | in the output areas of code cells. It is also possible to request these |
|
573 | in the output areas of code cells. It is also possible to request these | |
542 | files directly in markdown cells if they reside in the notebook |
|
574 | files directly in markdown cells if they reside in the notebook | |
543 | directory via relative urls prefixed with \texttt{files/}: |
|
575 | directory via relative urls prefixed with \texttt{files/}: | |
544 |
|
576 | |||
545 | \begin{verbatim} |
|
577 | \begin{verbatim} | |
546 | files/[subdirectory/]<filename> |
|
578 | files/[subdirectory/]<filename> | |
547 | \end{verbatim} |
|
579 | \end{verbatim} | |
548 | For example, in the example notebook folder, we have the Python logo, |
|
580 | For example, in the example notebook folder, we have the Python logo, | |
549 | addressed as: |
|
581 | addressed as: | |
550 |
|
582 | |||
551 | \begin{verbatim} |
|
583 | \begin{verbatim} | |
552 | <img src="files/python-logo.svg" /> |
|
584 | <img src="files/python-logo.svg" /> | |
553 | \end{verbatim} |
|
585 | \end{verbatim} | |
554 | and a video with the HTML5 video tag: |
|
586 | and a video with the HTML5 video tag: | |
555 |
|
587 | |||
556 | \begin{verbatim} |
|
588 | \begin{verbatim} | |
557 | <video controls src="files/animation.m4v" /> |
|
589 | <video controls src="files/animation.m4v" /> | |
558 | \end{verbatim} |
|
590 | \end{verbatim} | |
559 | These do not embed the data into the notebook file, and require that the |
|
591 | These do not embed the data into the notebook file, and require that the | |
560 | files exist when you are viewing the notebook. |
|
592 | files exist when you are viewing the notebook. | |
561 |
|
593 | |||
562 | \subsubsection{Security of local files} |
|
594 | \subsubsection{Security of local files} | |
563 |
|
595 | |||
564 | Note that this means that the IPython notebook server also acts as a |
|
596 | Note that this means that the IPython notebook server also acts as a | |
565 | generic file server for files inside the same tree as your notebooks. |
|
597 | generic file server for files inside the same tree as your notebooks. | |
566 | Access is not granted outside the notebook folder so you have strict |
|
598 | Access is not granted outside the notebook folder so you have strict | |
567 | control over what files are visible, but for this reason it is highly |
|
599 | control over what files are visible, but for this reason it is highly | |
568 | recommended that you do not run the notebook server with a notebook |
|
600 | recommended that you do not run the notebook server with a notebook | |
569 | directory at a high level in your filesystem (e.g.~your home directory). |
|
601 | directory at a high level in your filesystem (e.g.~your home directory). | |
570 |
|
602 | |||
571 | When you run the notebook in a password-protected manner, local file |
|
603 | When you run the notebook in a password-protected manner, local file | |
572 | access is restricted to authenticated users unless read-only views are |
|
604 | access is restricted to authenticated users unless read-only views are | |
573 | active. |
|
605 | active. | |
574 |
|
606 | |||
575 | \subsection{Linking to files and directories for viewing in the browser} |
|
607 | \subsection{Linking to files and directories for viewing in the browser} | |
576 |
|
608 | |||
577 | It is also possible to link directly to files or directories so they can |
|
609 | It is also possible to link directly to files or directories so they can | |
578 | be opened in the browser. This is especially convenient if you're |
|
610 | be opened in the browser. This is especially convenient if you're | |
579 | interacting with a tool within IPython that generates HTML pages, and |
|
611 | interacting with a tool within IPython that generates HTML pages, and | |
580 | you'd like to easily be able to open those in a new browser window. |
|
612 | you'd like to easily be able to open those in a new browser window. | |
581 | Alternatively, if your IPython notebook server is on a remote system, |
|
613 | Alternatively, if your IPython notebook server is on a remote system, | |
582 | creating links provides an easy way to download any files that get |
|
614 | creating links provides an easy way to download any files that get | |
583 | generated. |
|
615 | generated. | |
584 |
|
616 | |||
585 | As we saw above, there are a bunch of \texttt{.ipynb} files in our |
|
617 | As we saw above, there are a bunch of \texttt{.ipynb} files in our | |
586 | current directory. |
|
618 | current directory. | |
587 |
|
619 | |||
588 | \begin{codecell} |
|
620 | \begin{codecell} | |
589 | \begin{codeinput} |
|
621 | \begin{codeinput} | |
590 | \begin{lstlisting} |
|
622 | \begin{lstlisting} | |
591 | ls |
|
623 | ls | |
592 | \end{lstlisting} |
|
624 | \end{lstlisting} | |
593 | \end{codeinput} |
|
625 | \end{codeinput} | |
594 | \begin{codeoutput} |
|
626 | \begin{codeoutput} | |
595 |
|
627 | |||
596 | \begin{verbatim} |
|
628 | \begin{verbatim} | |
597 | 00_notebook_tour.ipynb formatting.ipynb |
|
629 | 00_notebook_tour.ipynb formatting.ipynb | |
598 | 01_notebook_introduction.ipynb octavemagic_extension.ipynb |
|
630 | 01_notebook_introduction.ipynb octavemagic_extension.ipynb | |
599 | Animations_and_Progress.ipynb publish_data.ipynb |
|
631 | Animations_and_Progress.ipynb publish_data.ipynb | |
600 | Capturing Output.ipynb python-logo.svg |
|
632 | Capturing Output.ipynb python-logo.svg | |
601 | Script Magics.ipynb rmagic_extension.ipynb |
|
633 | Script Magics.ipynb rmagic_extension.ipynb | |
602 | animation.m4v sympy.ipynb |
|
634 | animation.m4v sympy.ipynb | |
603 | cython_extension.ipynb sympy_quantum_computing.ipynb |
|
635 | cython_extension.ipynb sympy_quantum_computing.ipynb | |
604 | display_protocol.ipynb trapezoid_rule.ipynb |
|
636 | display_protocol.ipynb trapezoid_rule.ipynb | |
605 |
|
637 | |||
606 | \end{verbatim} |
|
638 | \end{verbatim} | |
607 |
|
639 | |||
608 | \end{codeoutput} |
|
640 | \end{codeoutput} | |
609 | \end{codecell} |
|
641 | \end{codecell} | |
610 | If we want to create a link to one of them, we can call use the |
|
642 | If we want to create a link to one of them, we can call use the | |
611 | \texttt{FileLink} object. |
|
643 | \texttt{FileLink} object. | |
612 |
|
644 | |||
613 | \begin{codecell} |
|
645 | \begin{codecell} | |
614 | \begin{codeinput} |
|
646 | \begin{codeinput} | |
615 | \begin{lstlisting} |
|
647 | \begin{lstlisting} | |
616 | from IPython.display import FileLink |
|
648 | from IPython.display import FileLink | |
617 | FileLink('00_notebook_tour.ipynb') |
|
649 | FileLink('00_notebook_tour.ipynb') | |
618 | \end{lstlisting} |
|
650 | \end{lstlisting} | |
619 | \end{codeinput} |
|
651 | \end{codeinput} | |
620 | \begin{codeoutput} |
|
652 | \begin{codeoutput} | |
621 |
|
653 | |||
|
654 | ||||
|
655 | \begin{verbatim} | |||
622 | <IPython.lib.display.FileLink at 0x10f7ea3d0> |
|
656 | <IPython.lib.display.FileLink at 0x10f7ea3d0> | |
|
657 | \end{verbatim} | |||
|
658 | ||||
623 |
|
659 | |||
624 | \end{codeoutput} |
|
660 | \end{codeoutput} | |
625 | \end{codecell} |
|
661 | \end{codecell} | |
626 | Alternatively, if we want to link to all of them, we can use the |
|
662 | Alternatively, if we want to link to all of them, we can use the | |
627 | \texttt{FileLinks} object, passing \texttt{'.'} to indicate that we want |
|
663 | \texttt{FileLinks} object, passing \texttt{'.'} to indicate that we want | |
628 | links generated for the current working directory. Note that if there |
|
664 | links generated for the current working directory. Note that if there | |
629 | were other directories under the current directory, \texttt{FileLinks} |
|
665 | were other directories under the current directory, \texttt{FileLinks} | |
630 | would work in a recursive manner creating links to files in all |
|
666 | would work in a recursive manner creating links to files in all | |
631 | sub-directories as well. |
|
667 | sub-directories as well. | |
632 |
|
668 | |||
633 | \begin{codecell} |
|
669 | \begin{codecell} | |
634 | \begin{codeinput} |
|
670 | \begin{codeinput} | |
635 | \begin{lstlisting} |
|
671 | \begin{lstlisting} | |
636 | from IPython.display import FileLinks |
|
672 | from IPython.display import FileLinks | |
637 | FileLinks('.') |
|
673 | FileLinks('.') | |
638 | \end{lstlisting} |
|
674 | \end{lstlisting} | |
639 | \end{codeinput} |
|
675 | \end{codeinput} | |
640 | \begin{codeoutput} |
|
676 | \begin{codeoutput} | |
641 |
|
677 | |||
|
678 | ||||
|
679 | \begin{verbatim} | |||
642 | <IPython.lib.display.FileLinks at 0x10f7eaad0> |
|
680 | <IPython.lib.display.FileLinks at 0x10f7eaad0> | |
|
681 | \end{verbatim} | |||
|
682 | ||||
643 |
|
683 | |||
644 | \end{codeoutput} |
|
684 | \end{codeoutput} | |
645 | \end{codecell} |
|
685 | \end{codecell} | |
646 | \subsubsection{External sites} |
|
686 | \subsubsection{External sites} | |
647 |
|
687 | |||
648 | You can even embed an entire page from another site in an iframe; for |
|
688 | You can even embed an entire page from another site in an iframe; for | |
649 | example this is today's Wikipedia page for mobile users: |
|
689 | example this is today's Wikipedia page for mobile users: | |
650 |
|
690 | |||
651 | \begin{codecell} |
|
691 | \begin{codecell} | |
652 | \begin{codeinput} |
|
692 | \begin{codeinput} | |
653 | \begin{lstlisting} |
|
693 | \begin{lstlisting} | |
654 | HTML('<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350></iframe>') |
|
694 | HTML('<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350></iframe>') | |
655 | \end{lstlisting} |
|
695 | \end{lstlisting} | |
656 | \end{codeinput} |
|
696 | \end{codeinput} | |
657 | \begin{codeoutput} |
|
697 | \begin{codeoutput} | |
658 |
|
698 | |||
|
699 | ||||
|
700 | \begin{verbatim} | |||
659 | <IPython.core.display.HTML at 0x1094900d0> |
|
701 | <IPython.core.display.HTML at 0x1094900d0> | |
|
702 | \end{verbatim} | |||
|
703 | ||||
660 |
|
704 | |||
661 | \end{codeoutput} |
|
705 | \end{codeoutput} | |
662 | \end{codecell} |
|
706 | \end{codecell} | |
663 | \subsubsection{Mathematics} |
|
707 | \subsubsection{Mathematics} | |
664 |
|
708 | |||
665 | And we also support the display of mathematical expressions typeset in |
|
709 | And we also support the display of mathematical expressions typeset in | |
666 | LaTeX, which is rendered in the browser thanks to the |
|
710 | LaTeX, which is rendered in the browser thanks to the | |
667 | \href{http://mathjax.org}{MathJax library}. |
|
711 | \href{http://mathjax.org}{MathJax library}. | |
668 |
|
712 | |||
669 | Note that this is \emph{different} from the above examples. Above we |
|
713 | Note that this is \emph{different} from the above examples. Above we | |
670 | were typing mathematical expressions in Markdown cells (along with |
|
714 | were typing mathematical expressions in Markdown cells (along with | |
671 | normal text) and letting the browser render them; now we are displaying |
|
715 | normal text) and letting the browser render them; now we are displaying | |
672 | the output of a Python computation as a LaTeX expression wrapped by the |
|
716 | the output of a Python computation as a LaTeX expression wrapped by the | |
673 | \texttt{Math()} object so the browser renders it. The \texttt{Math} |
|
717 | \texttt{Math()} object so the browser renders it. The \texttt{Math} | |
674 |
object will add the needed LaTeX delimiters ( |
|
718 | object will add the needed LaTeX delimiters (`\$\$) if they are not | |
675 |
|
|
719 | provided: | |
676 |
|
720 | |||
677 | \begin{codecell} |
|
721 | \begin{codecell} | |
678 | \begin{codeinput} |
|
722 | \begin{codeinput} | |
679 | \begin{lstlisting} |
|
723 | \begin{lstlisting} | |
680 | from IPython.display import Math |
|
724 | from IPython.display import Math | |
681 | Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx') |
|
725 | Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx') | |
682 | \end{lstlisting} |
|
726 | \end{lstlisting} | |
683 | \end{codeinput} |
|
727 | \end{codeinput} | |
684 | \begin{codeoutput} |
|
728 | \begin{codeoutput} | |
685 |
|
729 | |||
686 | <IPython.core.display.Math at 0x10fba26d0> |
|
730 | ||
687 |
|
731 | |||
688 | \end{codeoutput} |
|
732 | \end{codeoutput} | |
689 | \end{codecell} |
|
733 | \end{codecell} | |
690 | With the \texttt{Latex} class, you have to include the delimiters |
|
734 | With the \texttt{Latex} class, you have to include the delimiters | |
691 | yourself. This allows you to use other LaTeX modes such as |
|
735 | yourself. This allows you to use other LaTeX modes such as | |
692 | \texttt{eqnarray}: |
|
736 | \texttt{eqnarray}: | |
693 |
|
737 | |||
694 | \begin{codecell} |
|
738 | \begin{codecell} | |
695 | \begin{codeinput} |
|
739 | \begin{codeinput} | |
696 | \begin{lstlisting} |
|
740 | \begin{lstlisting} | |
697 | from IPython.display import Latex |
|
741 | from IPython.display import Latex | |
698 | Latex(r"""\begin{eqnarray} |
|
742 | Latex(r"""\begin{eqnarray} | |
699 | \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ |
|
743 | \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ | |
700 | \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ |
|
744 | \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ | |
701 | \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ |
|
745 | \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ | |
702 | \nabla \cdot \vec{\mathbf{B}} & = 0 |
|
746 | \nabla \cdot \vec{\mathbf{B}} & = 0 | |
703 | \end{eqnarray}""") |
|
747 | \end{eqnarray}""") | |
704 | \end{lstlisting} |
|
748 | \end{lstlisting} | |
705 | \end{codeinput} |
|
749 | \end{codeinput} | |
706 | \begin{codeoutput} |
|
750 | \begin{codeoutput} | |
707 |
|
751 | |||
708 | <IPython.core.display.Latex at 0x10fba2c10> |
|
752 | ||
709 |
|
753 | |||
710 | \end{codeoutput} |
|
754 | \end{codeoutput} | |
711 | \end{codecell} |
|
755 | \end{codecell} | |
712 | Or you can enter latex directly with the \texttt{\%\%latex} cell magic: |
|
756 | Or you can enter latex directly with the \texttt{\%\%latex} cell magic: | |
713 |
|
757 | |||
714 | \begin{codecell} |
|
758 | \begin{codecell} | |
715 | \begin{codeinput} |
|
759 | \begin{codeinput} | |
716 | \begin{lstlisting} |
|
760 | \begin{lstlisting} | |
717 | %%latex |
|
761 | %%latex | |
718 | \begin{aligned} |
|
762 | \begin{aligned} | |
719 | \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ |
|
763 | \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ | |
720 | \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ |
|
764 | \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ | |
721 | \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ |
|
765 | \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ | |
722 | \nabla \cdot \vec{\mathbf{B}} & = 0 |
|
766 | \nabla \cdot \vec{\mathbf{B}} & = 0 | |
723 | \end{aligned} |
|
767 | \end{aligned} | |
724 | \end{lstlisting} |
|
768 | \end{lstlisting} | |
725 | \end{codeinput} |
|
769 | \end{codeinput} | |
726 | \begin{codeoutput} |
|
770 | \begin{codeoutput} | |
727 |
|
771 | |||
728 | \end{codeoutput} |
|
772 | \end{codeoutput} | |
729 | \end{codecell} |
|
773 | \end{codecell} | |
730 | There is also a \texttt{\%\%javascript} cell magic for running |
|
774 | There is also a \texttt{\%\%javascript} cell magic for running | |
731 | javascript directly, and \texttt{\%\%svg} for manually entering SVG |
|
775 | javascript directly, and \texttt{\%\%svg} for manually entering SVG | |
732 | content. |
|
776 | content. | |
733 |
|
777 | |||
734 | \section{Loading external codes} |
|
778 | \section{Loading external codes} | |
735 |
|
779 | |||
736 | \begin{itemize} |
|
780 | \begin{itemize} | |
737 | \item |
|
781 | \item | |
738 | Drag and drop a \texttt{.py} in the dashboard |
|
782 | Drag and drop a \texttt{.py} in the dashboard | |
739 | \item |
|
783 | \item | |
740 | Use \texttt{\%load} with any local or remote url: |
|
784 | Use \texttt{\%load} with any local or remote url: | |
741 | \href{http://matplotlib.sourceforge.net/gallery.html}{the Matplotlib |
|
785 | \href{http://matplotlib.sourceforge.net/gallery.html}{the Matplotlib | |
742 | Gallery!} |
|
786 | Gallery!} | |
743 | \end{itemize} |
|
787 | \end{itemize} | |
744 | In this notebook we've kept the output saved so you can see the result, |
|
788 | In this notebook we've kept the output saved so you can see the result, | |
745 | but you should run the next cell yourself (with an active internet |
|
789 | but you should run the next cell yourself (with an active internet | |
746 | connection). |
|
790 | connection). | |
747 |
|
791 | |||
748 | Let's make sure we have pylab again, in case we have restarted the |
|
792 | Let's make sure we have pylab again, in case we have restarted the | |
749 | kernel due to the crash demo above |
|
793 | kernel due to the crash demo above | |
750 |
|
794 | |||
751 | \begin{codecell} |
|
795 | \begin{codecell} | |
752 | \begin{codeinput} |
|
796 | \begin{codeinput} | |
753 | \begin{lstlisting} |
|
797 | \begin{lstlisting} | |
754 | %pylab inline |
|
798 | %pylab inline | |
755 | \end{lstlisting} |
|
799 | \end{lstlisting} | |
756 | \end{codeinput} |
|
800 | \end{codeinput} | |
757 | \begin{codeoutput} |
|
801 | \begin{codeoutput} | |
758 |
|
802 | |||
759 | \begin{verbatim} |
|
803 | \begin{verbatim} | |
760 |
|
804 | |||
761 | Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. |
|
805 | Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. | |
762 | For more information, type 'help(pylab)'. |
|
806 | For more information, type 'help(pylab)'. | |
763 |
|
807 | |||
764 | \end{verbatim} |
|
808 | \end{verbatim} | |
765 |
|
809 | |||
766 | \end{codeoutput} |
|
810 | \end{codeoutput} | |
767 | \end{codecell} |
|
811 | \end{codecell} | |
768 | \begin{codecell} |
|
812 | \begin{codecell} | |
769 | \begin{codeinput} |
|
813 | \begin{codeinput} | |
770 | \begin{lstlisting} |
|
814 | \begin{lstlisting} | |
771 | %load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py |
|
815 | %load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py | |
772 | \end{lstlisting} |
|
816 | \end{lstlisting} | |
773 | \end{codeinput} |
|
817 | \end{codeinput} | |
774 |
|
818 | |||
775 | \end{codecell} |
|
819 | \end{codecell} | |
776 | \begin{codecell} |
|
820 | \begin{codecell} | |
777 | \begin{codeinput} |
|
821 | \begin{codeinput} | |
778 | \begin{lstlisting} |
|
822 | \begin{lstlisting} | |
779 | #!/usr/bin/env python |
|
823 | #!/usr/bin/env python | |
780 |
|
824 | |||
781 | # implement the example graphs/integral from pyx |
|
825 | # implement the example graphs/integral from pyx | |
782 | from pylab import * |
|
826 | from pylab import * | |
783 | from matplotlib.patches import Polygon |
|
827 | from matplotlib.patches import Polygon | |
784 |
|
828 | |||
785 | def func(x): |
|
829 | def func(x): | |
786 | return (x-3)*(x-5)*(x-7)+85 |
|
830 | return (x-3)*(x-5)*(x-7)+85 | |
787 |
|
831 | |||
788 | ax = subplot(111) |
|
832 | ax = subplot(111) | |
789 |
|
833 | |||
790 | a, b = 2, 9 # integral area |
|
834 | a, b = 2, 9 # integral area | |
791 | x = arange(0, 10, 0.01) |
|
835 | x = arange(0, 10, 0.01) | |
792 | y = func(x) |
|
836 | y = func(x) | |
793 | plot(x, y, linewidth=1) |
|
837 | plot(x, y, linewidth=1) | |
794 |
|
838 | |||
795 | # make the shaded region |
|
839 | # make the shaded region | |
796 | ix = arange(a, b, 0.01) |
|
840 | ix = arange(a, b, 0.01) | |
797 | iy = func(ix) |
|
841 | iy = func(ix) | |
798 | verts = [(a,0)] + zip(ix,iy) + [(b,0)] |
|
842 | verts = [(a,0)] + zip(ix,iy) + [(b,0)] | |
799 | poly = Polygon(verts, facecolor='0.8', edgecolor='k') |
|
843 | poly = Polygon(verts, facecolor='0.8', edgecolor='k') | |
800 | ax.add_patch(poly) |
|
844 | ax.add_patch(poly) | |
801 |
|
845 | |||
802 | text(0.5 * (a + b), 30, |
|
846 | text(0.5 * (a + b), 30, | |
803 | r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center', |
|
847 | r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center', | |
804 | fontsize=20) |
|
848 | fontsize=20) | |
805 |
|
849 | |||
806 | axis([0,10, 0, 180]) |
|
850 | axis([0,10, 0, 180]) | |
807 | figtext(0.9, 0.05, 'x') |
|
851 | figtext(0.9, 0.05, 'x') | |
808 | figtext(0.1, 0.9, 'y') |
|
852 | figtext(0.1, 0.9, 'y') | |
809 | ax.set_xticks((a,b)) |
|
853 | ax.set_xticks((a,b)) | |
810 | ax.set_xticklabels(('a','b')) |
|
854 | ax.set_xticklabels(('a','b')) | |
811 | ax.set_yticks([]) |
|
855 | ax.set_yticks([]) | |
812 | show() |
|
856 | show() | |
813 |
|
857 | |||
814 | \end{lstlisting} |
|
858 | \end{lstlisting} | |
815 | \end{codeinput} |
|
859 | \end{codeinput} | |
816 | \begin{codeoutput} |
|
860 | \begin{codeoutput} | |
817 | \begin{center} |
|
861 | \begin{center} | |
818 | \includegraphics[width=0.7\textwidth]{_fig_60.png} |
|
862 | \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{_fig_60.png} | |
819 | \par |
|
863 | \par | |
820 | \end{center} |
|
864 | \end{center} | |
821 | \end{codeoutput} |
|
865 | \end{codeoutput} | |
822 | \end{codecell} |
|
866 | \end{codecell} | |
823 |
|
867 | |||
824 | \end{document} |
|
868 | ||
|
869 | ||||
|
870 | \end{document} | |||
|
871 |
General Comments 0
You need to be logged in to leave comments.
Login now