Show More
@@ -1,573 +1,568 b'' | |||||
1 | .. _htmlnotebook: |
|
1 | .. _htmlnotebook: | |
2 |
|
2 | |||
3 | The IPython Notebook |
|
3 | The IPython Notebook | |
4 | ==================== |
|
4 | ==================== | |
5 |
|
5 | |||
6 | .. seealso:: |
|
6 | .. seealso:: | |
7 |
|
7 | |||
8 | :ref:`Installation requirements <installnotebook>` for the Notebook. |
|
8 | :ref:`Installation requirements <installnotebook>` for the Notebook. | |
9 |
|
9 | |||
10 | The IPython Notebook combines two components: |
|
10 | The IPython Notebook combines two components: | |
11 |
|
11 | |||
12 | * A web application, called the *IPython Notebook web app*, for interactive |
|
12 | * **The *IPython Notebook* web application**: | |
13 | authoring of literate computations, in which explanatory text, mathematics, |
|
|||
14 | computations and rich media output may be combined. Input and output are stored in persistent cells that may be edited in-place. |
|
|||
15 |
|
13 | |||
16 | * Plain text documents, called *notebook documents*, or *notebooks*, for recording and distributing the results of the rich computations. |
|
14 | The IPython Notebook web app is a browser-based tool for interactive authoring of literate computations, in which explanatory text, mathematics,computations and rich media output may be combined. Input and output are stored in persistent cells that may be edited in-place. | |
17 |
|
15 | |||
18 | In the documentation, the distinction between the *N*otebook app and *n*otebook documents is made by capitalization. |
|
16 | * **Notebook documents**: | |
19 |
|
17 | |||
20 | The Notebook app automatically saves, at certain intervals, the current state of the computational process occurring in the web browser to the corresponding notebook document. |
|
18 | *Notebook documents*, or *notebooks*, are plain text documents which record all inputs and outputs of the computations, interspersed with text, mathematics and HTML 5 representations of objects, in a literate style. | |
21 |
|
19 | |||
22 | Note that it is also common to refer to the current state of the computation, |
|
20 | Since the similarity in names can lead to some confusion, in the documentation, we will always use the typographical distinction between the *N*otebook app and *n*otebook documents via the respective capitalization. Here we are thinking of the Notebook app as being a proper noun. We will also always refer to the "Notebook app" when we are referring to the browser-based interface, to increase clarity. | |
23 | as represented by the sequence of input cells in the Notebook app, as a |
|
|||
24 | *notebook*. There is no problem with confounding these two concepts, since |
|
|||
25 | there is actually a one-to-one correspondence between what you see on the |
|
|||
26 | screen inside the app, and what is stored in the corresponding ``.ipynb`` |
|
|||
27 | notebook document. |
|
|||
28 |
|
21 | |||
|
22 | We refer to the current state of the computational process taking place in the Notebook app, i.e. the (numbered) sequence of input and output cells, as the | |||
|
23 | *notebook space*. Notebook documents provide an *exact*, *one-to-one* record of all the content in the notebook space, and the Notebook app automatically saves, at certain intervals, the contents of the notebook space to a notebook document stored on disk, with the same name as the title of the notebook space, and the file extension ".ipynb". For this reason, there is no confusion about using the same name "notebook" for both the notebook space and the corresonding notebook document. | |||
29 |
|
24 | |||
30 | Features of the IPython Notebook web app |
|
25 | Features of the IPython Notebook web app | |
31 | ---------------------------------------- |
|
26 | ---------------------------------------- | |
32 |
|
27 | |||
33 | Some of the main features of the IPython Notebook app include: |
|
28 | Some of the main features of the IPython Notebook app include: | |
34 |
|
29 | |||
35 | * In-browser editing, syntax highlighting, tab completion and autoindentation for code. |
|
30 | * In-browser editing, syntax highlighting, tab completion and autoindentation for code. | |
36 | * Mix code with rich text using the Markdown markup language. |
|
31 | * Mix code with rich text using the Markdown markup language. | |
37 | * Include mathematical equations using LaTeX notation, rendered directly in the browser by MathJax. |
|
32 | * Include mathematical equations using LaTeX notation, rendered directly in the browser by MathJax. | |
38 | * Display rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations. |
|
33 | * Display rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations. | |
39 | * Include inline figures rendered by the ``matplotlib`` library with publication quality, in a range of formats (SVG / PDF / PNG). |
|
34 | * Include inline figures rendered by the ``matplotlib`` library with publication quality, in a range of formats (SVG / PDF / PNG). | |
40 |
|
35 | |||
41 | If you have ever used the Mathematica or SAGE notebooks (the latter is also |
|
36 | If you have ever used the Mathematica or SAGE notebooks (the latter is also | |
42 | web-based__) you should feel right at home. If you have not, you will be |
|
37 | web-based__) you should feel right at home. If you have not, you will be | |
43 | able to learn how to use the IPython Notebook in just a few minutes. |
|
38 | able to learn how to use the IPython Notebook in just a few minutes. | |
44 |
|
39 | |||
45 | .. __: http://sagenb.org |
|
40 | .. __: http://sagenb.org | |
46 |
|
41 | |||
47 |
|
42 | |||
48 | Notebook documents |
|
43 | Notebook documents | |
49 | ------------------ |
|
44 | ------------------ | |
50 |
|
45 | |||
51 | Notebook document files are just standard text files with the extension |
|
46 | Notebook document files are just standard text files with the extension | |
52 | ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues. |
|
47 | ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues. | |
53 |
|
48 | |||
54 | Despite the fact that the notebook documents are plain text files, they use |
|
49 | Despite the fact that the notebook documents are plain text files, they use | |
55 | the JSON format in order to store a *complete*, *reproducible*, *one-to-one* copy of the state of the computational state as it is inside the Notebook app. |
|
50 | the JSON format in order to store a *complete*, *reproducible*, *one-to-one* copy of the state of the computational state as it is inside the Notebook app. | |
56 | All computations carried out, and the corresponding results obtained, are combined in a literate way, mixing them with descriptive text, mathematics, and HTML 5 representations of objects. |
|
51 | All computations carried out, and the corresponding results obtained, are combined in a literate way, mixing them with descriptive text, mathematics, and HTML 5 representations of objects. | |
57 |
|
52 | |||
58 | Notebooks may easily be exported to a range of static formats, including |
|
53 | Notebooks may easily be exported to a range of static formats, including | |
59 | HTML (for example, for blog posts), PDF and slide shows. |
|
54 | HTML (for example, for blog posts), PDF and slide shows. | |
60 | Furthermore, any publicly available notebook may be shared via the |
|
55 | Furthermore, any publicly available notebook may be shared via the | |
61 | `IPython Notebook Viewer <http://nbviewer.ipython.org>`_ service, which will |
|
56 | `IPython Notebook Viewer <http://nbviewer.ipython.org>`_ service, which will | |
62 | provide it as a static web page. The results may thus be shared without having to install anything. |
|
57 | provide it as a static web page. The results may thus be shared without having to install anything. | |
63 |
|
58 | |||
64 | See :ref:`our installation documentation <install_index>` for directions on |
|
59 | See :ref:`our installation documentation <install_index>` for directions on | |
65 | how to install the notebook and its dependencies. |
|
60 | how to install the notebook and its dependencies. | |
66 |
|
61 | |||
67 | .. note:: |
|
62 | .. note:: | |
68 |
|
63 | |||
69 | You can start more than one notebook server at the same time, if you want to |
|
64 | You can start more than one notebook server at the same time, if you want to | |
70 | work on notebooks in different directories. By default the first notebook |
|
65 | work on notebooks in different directories. By default the first notebook | |
71 | server starts on port 8888, and later notebook servers search for ports near |
|
66 | server starts on port 8888, and later notebook servers search for ports near | |
72 | that one. You can also manually specify the port with the ``--port`` |
|
67 | that one. You can also manually specify the port with the ``--port`` | |
73 | option. |
|
68 | option. | |
74 |
|
69 | |||
75 |
|
70 | |||
76 | Starting up the IPython Notebook web app |
|
71 | Starting up the IPython Notebook web app | |
77 | ---------------------------------------- |
|
72 | ---------------------------------------- | |
78 |
|
73 | |||
79 | The Notebook web app is started with the command:: |
|
74 | The Notebook web app is started with the command:: | |
80 |
|
75 | |||
81 | $ ipython notebook |
|
76 | $ ipython notebook | |
82 |
|
77 | |||
83 | The landing page of the notebook server application, the *dashboard*, shows the notebooks currently available in the *working directory* (the directory from which the notebook was started). |
|
78 | The landing page of the notebook server application, the *dashboard*, shows the notebooks currently available in the *working directory* (the directory from which the notebook was started). | |
84 | You can create new notebooks from the dashboard with the ``New Notebook`` |
|
79 | You can create new notebooks from the dashboard with the ``New Notebook`` | |
85 | button, or open existing ones by clicking on their name. |
|
80 | button, or open existing ones by clicking on their name. | |
86 | You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files into the notebook list area. |
|
81 | You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files into the notebook list area. | |
87 |
|
82 | |||
88 | ``.py`` files will be imported into the IPython Notebook as a notebook with the same name, but an ``.ipynb`` extension, located in the working directory. The notebook will consist of a single cell containing all the |
|
83 | ``.py`` files will be imported into the IPython Notebook as a notebook with the same name, but an ``.ipynb`` extension, located in the working directory. The notebook will consist of a single cell containing all the | |
89 | code in the ``.py`` file, which you can later manually partition into individual cells. |
|
84 | code in the ``.py`` file, which you can later manually partition into individual cells. | |
90 |
|
85 | |||
91 | .. Alternatively, prior to importing the ``.py``, you can manually add ``# <nbformat>2</nbformat>`` at the start of the file, and then add separators for text and code cells, to get a cleaner import with the file already broken into individual cells. |
|
86 | .. Alternatively, prior to importing the ``.py``, you can manually add ``# <nbformat>2</nbformat>`` at the start of the file, and then add separators for text and code cells, to get a cleaner import with the file already broken into individual cells. | |
92 |
|
87 | |||
93 |
|
88 | |||
94 | The IPython Notebook web app is based on a server-client structure. |
|
89 | The IPython Notebook web app is based on a server-client structure. | |
95 | This server uses a two-process kernel architecture based on ZeroMQ, as well as Tornado for serving HTTP requests. Other clients may connect to the same underlying IPython kernel. |
|
90 | This server uses a two-process kernel architecture based on ZeroMQ, as well as Tornado for serving HTTP requests. Other clients may connect to the same underlying IPython kernel. | |
96 |
|
91 | |||
97 |
|
92 | |||
98 | When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with "IPy". |
|
93 | When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with "IPy". | |
99 | The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython. |
|
94 | The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython. | |
100 |
|
95 | |||
101 |
|
96 | |||
102 | Notebook user interface |
|
97 | Notebook user interface | |
103 | ----------------------- |
|
98 | ----------------------- | |
104 |
|
99 | |||
105 | When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*. |
|
100 | When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*. | |
106 |
|
101 | |||
107 | Notebook title |
|
102 | Notebook title | |
108 | ~~~~~~~~~~~~~~ |
|
103 | ~~~~~~~~~~~~~~ | |
109 | The title of the notebook document that is currently being edited is displayed at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may be edited directly by clicking on it. The title is reflected in the name of the ``.ipynb`` notebook document file that is saved. |
|
104 | The title of the notebook document that is currently being edited is displayed at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may be edited directly by clicking on it. The title is reflected in the name of the ``.ipynb`` notebook document file that is saved. | |
110 |
|
105 | |||
111 | Menu bar |
|
106 | Menu bar | |
112 | ~~~~~~~~ |
|
107 | ~~~~~~~~ | |
113 | The menu bar presents different options that may be used to manipulate the way the Notebook functions. |
|
108 | The menu bar presents different options that may be used to manipulate the way the Notebook functions. | |
114 |
|
109 | |||
115 | Toolbar |
|
110 | Toolbar | |
116 | ~~~~~~~ |
|
111 | ~~~~~~~ | |
117 | The tool bar gives handy icons for the most-used operations within the Notebook. |
|
112 | The tool bar gives handy icons for the most-used operations within the Notebook. | |
118 |
|
113 | |||
119 |
|
114 | |||
120 | Input cells |
|
115 | Input cells | |
121 | ----------- |
|
116 | ----------- | |
122 | Input cells are the core of the functionality of the IPython Notebook. |
|
117 | Input cells are the core of the functionality of the IPython Notebook. | |
123 | They are regions in the document where you can enter different types of text and commands. These regions are then executed using :kbd:`Shift-Enter`, at which point the Notebook executes the current input cell, displays the resulting output beneath it, and adds a new input cell below. |
|
118 | They are regions in the document where you can enter different types of text and commands. These regions are then executed using :kbd:`Shift-Enter`, at which point the Notebook executes the current input cell, displays the resulting output beneath it, and adds a new input cell below. | |
124 |
|
119 | |||
125 | The notebook consists of a sequence of input cells, |
|
120 | The notebook consists of a sequence of input cells, | |
126 | providing the means to direct the computational process. |
|
121 | providing the means to direct the computational process. | |
127 |
|
122 | |||
128 |
|
123 | |||
129 | Basic workflow |
|
124 | Basic workflow | |
130 | -------------- |
|
125 | -------------- | |
131 | The normal workflow in a notebook is, then, quite similar to a standard IPython session, with the difference that you can edit cells in-place multiple |
|
126 | The normal workflow in a notebook is, then, quite similar to a standard IPython session, with the difference that you can edit cells in-place multiple | |
132 | times until you obtain the desired results, rather than having to |
|
127 | times until you obtain the desired results, rather than having to | |
133 | rerun separate scripts with the ``%run`` magic command. (Magic commands do, however, also work in the notebook; see below). Typically, you'll work on a problem in pieces, |
|
128 | rerun separate scripts with the ``%run`` magic command. (Magic commands do, however, also work in the notebook; see below). Typically, you'll work on a problem in pieces, | |
134 | organizing related pieces into cells and moving forward as previous |
|
129 | organizing related pieces into cells and moving forward as previous | |
135 | parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be |
|
130 | parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be | |
136 | executed together, especially if parts of them take a long time to run |
|
131 | executed together, especially if parts of them take a long time to run | |
137 |
|
132 | |||
138 | The only significant limitation that the notebook currently has, compared to the Qt console, is that it cannot run any code that |
|
133 | The only significant limitation that the notebook currently has, compared to the Qt console, is that it cannot run any code that | |
139 | expects input from the kernel (such as scripts that call |
|
134 | expects input from the kernel (such as scripts that call | |
140 | :func:`raw_input`). Very importantly, this means that the ``%debug`` |
|
135 | :func:`raw_input`). Very importantly, this means that the ``%debug`` | |
141 | magic does *not* currently work in the notebook! This limitation will |
|
136 | magic does *not* currently work in the notebook! This limitation will | |
142 | be overcome in the future, but in the meantime, there is a way to debug problems in the notebook: you can attach a Qt console to your existing notebook kernel, and run ``%debug`` from the Qt console. |
|
137 | be overcome in the future, but in the meantime, there is a way to debug problems in the notebook: you can attach a Qt console to your existing notebook kernel, and run ``%debug`` from the Qt console. | |
143 | If your notebook is running on a local |
|
138 | If your notebook is running on a local | |
144 | computer (i.e. if you are accessing it via your localhost address at ``127.0.0.1``), you can just type ``%qtconsole`` in the notebook and a Qt console will open up, connected to that same kernel. |
|
139 | computer (i.e. if you are accessing it via your localhost address at ``127.0.0.1``), you can just type ``%qtconsole`` in the notebook and a Qt console will open up, connected to that same kernel. | |
145 |
|
140 | |||
146 | At certain moments, it may be necessary to interrupt a particularly long calculation, or even to kill the entire computational process. This may be achieved by interrupting or restarting the kernel, respectively. |
|
141 | At certain moments, it may be necessary to interrupt a particularly long calculation, or even to kill the entire computational process. This may be achieved by interrupting or restarting the kernel, respectively. | |
147 | After a restart, all relevant cells must be re-evaluated |
|
142 | After a restart, all relevant cells must be re-evaluated | |
148 |
|
143 | |||
149 |
|
144 | |||
150 | A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the menu option ``File -> Download as`` |
|
145 | A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the menu option ``File -> Download as`` | |
151 | Choosing the ``.py`` option removes all output and saves the text cells |
|
146 | Choosing the ``.py`` option removes all output and saves the text cells | |
152 | in comment areas. See ref:`below <notebook_format>` for more details on the |
|
147 | in comment areas. See ref:`below <notebook_format>` for more details on the | |
153 | notebook format. |
|
148 | notebook format. | |
154 |
|
149 | |||
155 |
|
150 | |||
156 | .. warning:: |
|
151 | .. warning:: | |
157 |
|
152 | |||
158 | While in simple cases you can "roundtrip" a notebook to Python, edit the |
|
153 | While in simple cases you can "roundtrip" a notebook to Python, edit the | |
159 | Python file, and then import it back without loss of main content, this is in general *not guaranteed to work*. First, there is extra metadata |
|
154 | Python file, and then import it back without loss of main content, this is in general *not guaranteed to work*. First, there is extra metadata | |
160 | saved in the notebook that may not be saved to the ``.py`` format. And as |
|
155 | saved in the notebook that may not be saved to the ``.py`` format. And as | |
161 | the notebook format evolves in complexity, there will be attributes of the |
|
156 | the notebook format evolves in complexity, there will be attributes of the | |
162 | notebook that will not survive a roundtrip through the Python form. You |
|
157 | notebook that will not survive a roundtrip through the Python form. You | |
163 | should think of the Python format as a way to output a script version of a |
|
158 | should think of the Python format as a way to output a script version of a | |
164 | notebook and the import capabilities as a way to load existing code to get a |
|
159 | notebook and the import capabilities as a way to load existing code to get a | |
165 | notebook started. But the Python version is *not* an alternate notebook |
|
160 | notebook started. But the Python version is *not* an alternate notebook | |
166 | format. |
|
161 | format. | |
167 |
|
162 | |||
168 |
|
163 | |||
169 | Keyboard shortcuts |
|
164 | Keyboard shortcuts | |
170 | ------------------ |
|
165 | ------------------ | |
171 | All actions in the notebook can be achieved with the mouse, but we have also |
|
166 | All actions in the notebook can be achieved with the mouse, but we have also | |
172 | added keyboard shortcuts for the most common ones, so that productive use of |
|
167 | added keyboard shortcuts for the most common ones, so that productive use of | |
173 | the notebook can be achieved with minimal mouse intervention. The main |
|
168 | the notebook can be achieved with minimal mouse intervention. The main | |
174 | key bindings you need to remember are: |
|
169 | key bindings you need to remember are: | |
175 |
|
170 | |||
176 | * :kbd:`Shift-Enter`: |
|
171 | * :kbd:`Shift-Enter`: | |
177 | execute the current cell, show output (if any), and jump |
|
172 | execute the current cell, show output (if any), and jump | |
178 | to the next cell below. If :kbd:`Shift-Enter` |
|
173 | to the next cell below. If :kbd:`Shift-Enter` | |
179 | was invoked on the last input line, a new code cell will also be created. Note that in the notebook, simply using :kbd:`Enter` *never* forces execution, it simply inserts a new line in the current cell. Therefore, in the notebook you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run Selected`` button). |
|
174 | was invoked on the last input line, a new code cell will also be created. Note that in the notebook, simply using :kbd:`Enter` *never* forces execution, it simply inserts a new line in the current cell. Therefore, in the notebook you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run Selected`` button). | |
180 |
|
175 | |||
181 | * :kbd:`Alt-Enter`: |
|
176 | * :kbd:`Alt-Enter`: | |
182 | this combination is similar to the previous one, with the |
|
177 | this combination is similar to the previous one, with the | |
183 | exception that, if the next cell below is not empty, a new code cell will be |
|
178 | exception that, if the next cell below is not empty, a new code cell will be | |
184 | added to the notebook, even if the cell execution happens not in the last cell. :kbd:`Alt-Enter`: is a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`. |
|
179 | added to the notebook, even if the cell execution happens not in the last cell. :kbd:`Alt-Enter`: is a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`. | |
185 |
|
180 | |||
186 | * :kbd:`Ctrl-Enter`: |
|
181 | * :kbd:`Ctrl-Enter`: | |
187 | execute the current cell in "terminal mode", where any |
|
182 | execute the current cell in "terminal mode", where any | |
188 | output is shown, but the cursor remains in the current cell. This is convenient to do quick in-place experiments, or query things like filesystem content, without creating additional cells that you may not want saved in your notebook. |
|
183 | output is shown, but the cursor remains in the current cell. This is convenient to do quick in-place experiments, or query things like filesystem content, without creating additional cells that you may not want saved in your notebook. | |
189 |
|
184 | |||
190 | * :kbd:`Ctrl-m`: |
|
185 | * :kbd:`Ctrl-m`: | |
191 | this is the prefix for all other keybindings, which consist of an additional single letter or character. Type :kbd:`Ctrl-m h` (that is, the sole letter |
|
186 | this is the prefix for all other keybindings, which consist of an additional single letter or character. Type :kbd:`Ctrl-m h` (that is, the sole letter | |
192 | :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining available keybindings. |
|
187 | :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining available keybindings. | |
193 |
|
188 | |||
194 |
|
189 | |||
195 |
|
190 | |||
196 |
|
191 | |||
197 |
|
192 | |||
198 | Cell types |
|
193 | Cell types | |
199 | ---------- |
|
194 | ---------- | |
200 | Each IPython input cell has a *cell type*. |
|
195 | Each IPython input cell has a *cell type*. | |
201 | There is a restricted number of possible cell types, which may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts: |
|
196 | There is a restricted number of possible cell types, which may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts: | |
202 |
|
197 | |||
203 | * **code**: :kbd:`Ctrl-m y` |
|
198 | * **code**: :kbd:`Ctrl-m y` | |
204 | * **markdown**: :kbd:`Ctrl-m m` |
|
199 | * **markdown**: :kbd:`Ctrl-m m` | |
205 | * **raw**: :kbd:`Ctrl-m t` |
|
200 | * **raw**: :kbd:`Ctrl-m t` | |
206 | * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6` |
|
201 | * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6` | |
207 |
|
202 | |||
208 |
|
203 | |||
209 | Code cells |
|
204 | Code cells | |
210 | ~~~~~~~~~~ |
|
205 | ~~~~~~~~~~ | |
211 | Code cells contain code written in some computer language, which is Python by default. When the cell is executed with :kbd:`Shift-Enter`, this code is executed, and the result returned by Python (or the corresponding language) after running the code will be displayed as its output. |
|
206 | Code cells contain code written in some computer language, which is Python by default. When the cell is executed with :kbd:`Shift-Enter`, this code is executed, and the result returned by Python (or the corresponding language) after running the code will be displayed as its output. | |
212 |
|
207 | |||
213 | Code may be edited inline in the cell, with full syntax highlighting. |
|
208 | Code may be edited inline in the cell, with full syntax highlighting. | |
214 |
|
209 | |||
215 |
|
210 | |||
216 | Rich text using Markdown |
|
211 | Rich text using Markdown | |
217 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|
212 | ~~~~~~~~~~~~~~~~~~~~~~~~ | |
218 | The computational process may be documented in a literate way using rich text. |
|
213 | The computational process may be documented in a literate way using rich text. | |
219 | For this purpose, the Notebook provides markdown cells. Text is entered using Markdown_ syntax, allowing for italics, bold, ordered and unordered lists, etc. This is rendered using Markdown syntax to a rich HTML representation when the cell is executed. In this case, the output *replaces* the input cell. |
|
214 | For this purpose, the Notebook provides markdown cells. Text is entered using Markdown_ syntax, allowing for italics, bold, ordered and unordered lists, etc. This is rendered using Markdown syntax to a rich HTML representation when the cell is executed. In this case, the output *replaces* the input cell. | |
220 |
|
215 | |||
221 | Within markdown cells, mathematics can be included in a straightforward manner using LaTeX notation: ``$...$`` for inline math and ``$$...$$`` for displayed math. Standard LaTeX environments, such as ``\begin{equation}...\end{equation}``, also work. New commands may be defined using standard LaTeX commands, placed anywhere in a markdown cell. |
|
216 | Within markdown cells, mathematics can be included in a straightforward manner using LaTeX notation: ``$...$`` for inline math and ``$$...$$`` for displayed math. Standard LaTeX environments, such as ``\begin{equation}...\end{equation}``, also work. New commands may be defined using standard LaTeX commands, placed anywhere in a markdown cell. | |
222 |
|
217 | |||
223 | Raw cells |
|
218 | Raw cells | |
224 | ~~~~~~~~~ |
|
219 | ~~~~~~~~~ | |
225 | Raw cells provide a place to put additional information which is not evaluated by the Notebook. This can be used, for example, for extra information to be used when the notebook is exported to a certain format. |
|
220 | Raw cells provide a place to put additional information which is not evaluated by the Notebook. This can be used, for example, for extra information to be used when the notebook is exported to a certain format. | |
226 |
|
221 | |||
227 |
|
222 | |||
228 | Magic commands |
|
223 | Magic commands | |
229 | -------------- |
|
224 | -------------- | |
230 | Magic commands, or *magics*, are one-word commands beginning with the symbol ``%``, which send commands to IPython itself (as opposed to standard Python commands which are exported to be run in a Python interpreter). |
|
225 | Magic commands, or *magics*, are one-word commands beginning with the symbol ``%``, which send commands to IPython itself (as opposed to standard Python commands which are exported to be run in a Python interpreter). | |
231 |
|
226 | |||
232 | Magics control different elements of the way that the IPython notebook operates. They are entered into standard code cells and executed as usual with :kbd:`Shift-Enter`. |
|
227 | Magics control different elements of the way that the IPython notebook operates. They are entered into standard code cells and executed as usual with :kbd:`Shift-Enter`. | |
233 |
|
228 | |||
234 | There are two types of magics: *line magics*, which begin with a single ``%`` and operate on a single line of the code cell; and *cell magics*, which begin with ``%%`` and operate on the entire contents of the cell. |
|
229 | There are two types of magics: *line magics*, which begin with a single ``%`` and operate on a single line of the code cell; and *cell magics*, which begin with ``%%`` and operate on the entire contents of the cell. | |
235 |
|
230 | |||
236 | Line magics |
|
231 | Line magics | |
237 | ΛΛΛΛΛΛΛΛΛΛΛ |
|
232 | ΛΛΛΛΛΛΛΛΛΛΛ | |
238 | Some of the available line magics are the following: |
|
233 | Some of the available line magics are the following: | |
239 |
|
234 | |||
240 | * ``%load``: |
|
235 | * ``%load``: | |
241 | Loads a file and places its content into a new code cell. |
|
236 | Loads a file and places its content into a new code cell. | |
242 |
|
237 | |||
243 | * ``%timeit``: |
|
238 | * ``%timeit``: | |
244 | A simple way to time how long a single line of code takes to run |
|
239 | A simple way to time how long a single line of code takes to run | |
245 |
|
240 | |||
246 | * ``%config``: |
|
241 | * ``%config``: | |
247 | Configuration of the IPython Notebook |
|
242 | Configuration of the IPython Notebook | |
248 |
|
243 | |||
249 | * ``%lsmagic``: |
|
244 | * ``%lsmagic``: | |
250 | Provides a list of all available magic commands |
|
245 | Provides a list of all available magic commands | |
251 |
|
246 | |||
252 | Cell magics |
|
247 | Cell magics | |
253 | ΛΛΛΛΛΛΛΛΛΛΛ |
|
248 | ΛΛΛΛΛΛΛΛΛΛΛ | |
254 |
|
249 | |||
255 | * ``%%bash``: |
|
250 | * ``%%bash``: | |
256 | Send the contents of the code cell to be executed by ``bash`` |
|
251 | Send the contents of the code cell to be executed by ``bash`` | |
257 |
|
252 | |||
258 | * ``%%file``: |
|
253 | * ``%%file``: | |
259 | Writes a file with with contents of the cell. *Caution*: The file is ovewritten without asking. |
|
254 | Writes a file with with contents of the cell. *Caution*: The file is ovewritten without asking. | |
260 |
|
255 | |||
261 | * ``%%R``: |
|
256 | * ``%%R``: | |
262 | Execute the contents of the cell using the R language. |
|
257 | Execute the contents of the cell using the R language. | |
263 |
|
258 | |||
264 | * ``%%cython``: |
|
259 | * ``%%cython``: | |
265 | Execute the contents of the cell using ``Cython``. |
|
260 | Execute the contents of the cell using ``Cython``. | |
266 |
|
261 | |||
267 |
|
262 | |||
268 |
|
263 | |||
269 | Plotting |
|
264 | Plotting | |
270 | -------- |
|
265 | -------- | |
271 | One major feature of the Notebook is the ability to capture the result of plots as inline output. IPython is designed to work seamlessly together with |
|
266 | One major feature of the Notebook is the ability to capture the result of plots as inline output. IPython is designed to work seamlessly together with | |
272 | the ``%matplotlib`` plotting library. In order to set this up, the |
|
267 | the ``%matplotlib`` plotting library. In order to set this up, the | |
273 | ``%matplotlib`` magic command must be run before any plotting takes place. |
|
268 | ``%matplotlib`` magic command must be run before any plotting takes place. | |
274 |
|
269 | |||
275 | Note that ``%matplotlib`` only sets up IPython to work correctly with ``matplotlib``; it does not actually execute any ``import`` commands and does not add anything to the namespace. |
|
270 | Note that ``%matplotlib`` only sets up IPython to work correctly with ``matplotlib``; it does not actually execute any ``import`` commands and does not add anything to the namespace. | |
276 |
|
271 | |||
277 | There is an alternative magic, ``%pylab``, which, in addition, also executes a sequence of standard ``import`` statements required for working with the |
|
272 | There is an alternative magic, ``%pylab``, which, in addition, also executes a sequence of standard ``import`` statements required for working with the | |
278 | ``%matplotlib`` library. In particular, it automatically imports all names in the ``numpy`` and ``matplotlib`` packages to the namespace. A less invasive solution is ``%pylab --no-import-all``, which imports just the standard names |
|
273 | ``%matplotlib`` library. In particular, it automatically imports all names in the ``numpy`` and ``matplotlib`` packages to the namespace. A less invasive solution is ``%pylab --no-import-all``, which imports just the standard names | |
279 | ``np`` for the ``numpy`` module and ``plt`` for the ``matplotlib.pyplot`` module. |
|
274 | ``np`` for the ``numpy`` module and ``plt`` for the ``matplotlib.pyplot`` module. | |
280 |
|
275 | |||
281 | When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of a plotting command is captured in a *separate* window. An alternative is to use:: |
|
276 | When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of a plotting command is captured in a *separate* window. An alternative is to use:: | |
282 | ``%matplotlib inline`` |
|
277 | ``%matplotlib inline`` | |
283 | which captures the output inline within the notebook format. This has the benefit that the resulting plots will be stored in the notebook document. |
|
278 | which captures the output inline within the notebook format. This has the benefit that the resulting plots will be stored in the notebook document. | |
284 |
|
279 | |||
285 |
|
280 | |||
286 | Converting notebooks to other formats |
|
281 | Converting notebooks to other formats | |
287 | ------------------------------------- |
|
282 | ------------------------------------- | |
288 | Newly added in the 1.0 release of IPython is the ``nbconvert`` tool to convert a notebook document into another static format. This is a command line tool; at present, this functionality is not available to export directly from within the Notebook app. The syntax is:: |
|
283 | Newly added in the 1.0 release of IPython is the ``nbconvert`` tool to convert a notebook document into another static format. This is a command line tool; at present, this functionality is not available to export directly from within the Notebook app. The syntax is:: | |
289 |
|
284 | |||
290 | $ ipython nbconvert notebook.ipynb |
|
285 | $ ipython nbconvert notebook.ipynb | |
291 |
|
286 | |||
292 | for standard HTML output, or:: |
|
287 | for standard HTML output, or:: | |
293 |
|
288 | |||
294 | $ ipython nbconvert --format=FORMAT notebook.ipynb |
|
289 | $ ipython nbconvert --format=FORMAT notebook.ipynb | |
295 |
|
290 | |||
296 | where ``FORMAT`` is the desired export format. Options for this format include: |
|
291 | where ``FORMAT`` is the desired export format. Options for this format include: | |
297 |
|
292 | |||
298 | * ``full_html``: |
|
293 | * ``full_html``: | |
299 | Standard HTML |
|
294 | Standard HTML | |
300 |
|
295 | |||
301 | * ``simple_html``: |
|
296 | * ``simple_html``: | |
302 | A simplified version of HTML |
|
297 | A simplified version of HTML | |
303 |
|
298 | |||
304 | * ``reveal``: |
|
299 | * ``reveal``: | |
305 | A format to be used with the ``reveal.js`` package for slideshow presentations. |
|
300 | A format to be used with the ``reveal.js`` package for slideshow presentations. | |
306 |
|
301 | |||
307 | * ``sphinx_howto``: |
|
302 | * ``sphinx_howto``: | |
308 | A standard documentation format. |
|
303 | A standard documentation format. | |
309 |
|
304 | |||
310 | * ``latex``: |
|
305 | * ``latex``: | |
311 | Produces LaTeX output which may be compiled with ``pdflatex`` to PDF. |
|
306 | Produces LaTeX output which may be compiled with ``pdflatex`` to PDF. | |
312 |
|
307 | |||
313 |
|
308 | |||
314 | Configuration |
|
309 | Configuration | |
315 | ------------- |
|
310 | ------------- | |
316 | The IPython Notebook can be run with a variety of command line arguments. |
|
311 | The IPython Notebook can be run with a variety of command line arguments. | |
317 | To see a list of available options enter:: |
|
312 | To see a list of available options enter:: | |
318 |
|
313 | |||
319 | $ ipython notebook --help |
|
314 | $ ipython notebook --help | |
320 |
|
315 | |||
321 | Defaults for these options can also be set by creating a file named |
|
316 | Defaults for these options can also be set by creating a file named | |
322 | ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile folder is a subfolder of your IPython directory; ``ipython locate`` will show you where it is located. |
|
317 | ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile folder is a subfolder of your IPython directory; ``ipython locate`` will show you where it is located. | |
323 |
|
318 | |||
324 | To create a new set of default configuration files, with lots of information on available options, use:: |
|
319 | To create a new set of default configuration files, with lots of information on available options, use:: | |
325 |
|
320 | |||
326 | $ ipython profile create |
|
321 | $ ipython profile create | |
327 |
|
322 | |||
328 | .. seealso: |
|
323 | .. seealso: | |
329 |
|
324 | |||
330 | :ref:`config_overview`, in particular :ref:`Profiles`. |
|
325 | :ref:`config_overview`, in particular :ref:`Profiles`. | |
331 |
|
326 | |||
332 |
|
327 | |||
333 | Extracting standard Python files from notebooks |
|
328 | Extracting standard Python files from notebooks | |
334 | ----------------------------------------------- |
|
329 | ----------------------------------------------- | |
335 |
|
330 | |||
336 | The native format of the notebook, a file with a ``.ipynb`` `extension, is a |
|
331 | The native format of the notebook, a file with a ``.ipynb`` `extension, is a | |
337 | JSON container of all the input and output of the notebook, and therefore not |
|
332 | JSON container of all the input and output of the notebook, and therefore not | |
338 | valid Python by itself. This means that by default, you cannot directly |
|
333 | valid Python by itself. This means that by default, you cannot directly | |
339 | import a notebook from Python, nor execute it as a normal python script. |
|
334 | import a notebook from Python, nor execute it as a normal python script. | |
340 |
|
335 | |||
341 | But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with:: |
|
336 | But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with:: | |
342 |
|
337 | |||
343 | ipython notebook --script |
|
338 | ipython notebook --script | |
344 |
|
339 | |||
345 | or you can set this option permanently in your configuration file with:: |
|
340 | or you can set this option permanently in your configuration file with:: | |
346 |
|
341 | |||
347 | c.NotebookManager.save_script=True |
|
342 | c.NotebookManager.save_script=True | |
348 |
|
343 | |||
349 | This will instruct the notebook server to save the ``.py`` export of each |
|
344 | This will instruct the notebook server to save the ``.py`` export of each | |
350 | notebook, in addition to the ``.ipynb``, at every save. These are standard |
|
345 | notebook, in addition to the ``.ipynb``, at every save. These are standard | |
351 | ``.py`` files, and so they can be ``%run``, imported from regular IPython |
|
346 | ``.py`` files, and so they can be ``%run``, imported from regular IPython | |
352 | sessions or other notebooks, or executed at the command line. Since we export |
|
347 | sessions or other notebooks, or executed at the command line. Since we export | |
353 | the raw code you have typed, for these files to be importable from other code, |
|
348 | the raw code you have typed, for these files to be importable from other code, | |
354 | you will have to avoid using syntax such as ``%magic``s and other IPython-specific extensions to the language. |
|
349 | you will have to avoid using syntax such as ``%magic``s and other IPython-specific extensions to the language. | |
355 |
|
350 | |||
356 | In regular practice, the standard way to differentiate importable code from the |
|
351 | In regular practice, the standard way to differentiate importable code from the | |
357 | 'executable' part of a script is to put at the bottom:: |
|
352 | 'executable' part of a script is to put at the bottom:: | |
358 |
|
353 | |||
359 | if __name__ == '__main__': |
|
354 | if __name__ == '__main__': | |
360 | # rest of the code... |
|
355 | # rest of the code... | |
361 |
|
356 | |||
362 | Since all cells in the notebook are run as top-level code, you will need to |
|
357 | Since all cells in the notebook are run as top-level code, you will need to | |
363 | similarly protect *all* cells that you do not want executed when other scripts |
|
358 | similarly protect *all* cells that you do not want executed when other scripts | |
364 | try to import your notebook. A convenient shortand for this is to define early |
|
359 | try to import your notebook. A convenient shortand for this is to define early | |
365 | on:: |
|
360 | on:: | |
366 |
|
361 | |||
367 | script = __name__ == '__main__' |
|
362 | script = __name__ == '__main__' | |
368 |
|
363 | |||
369 | and then on any cell that you need to protect, use:: |
|
364 | and then on any cell that you need to protect, use:: | |
370 |
|
365 | |||
371 | if script: |
|
366 | if script: | |
372 | # rest of the cell... |
|
367 | # rest of the cell... | |
373 |
|
368 | |||
374 |
|
369 | |||
375 | .. _notebook_security: |
|
370 | .. _notebook_security: | |
376 |
|
371 | |||
377 | Security |
|
372 | Security | |
378 | -------- |
|
373 | -------- | |
379 |
|
374 | |||
380 | You can protect your Notebook server with a simple singlepassword by |
|
375 | You can protect your Notebook server with a simple singlepassword by | |
381 | setting the :attr:`NotebookApp.password` configurable. You can prepare a |
|
376 | setting the :attr:`NotebookApp.password` configurable. You can prepare a | |
382 | hashed password using the function :func:`IPython.lib.security.passwd`: |
|
377 | hashed password using the function :func:`IPython.lib.security.passwd`: | |
383 |
|
378 | |||
384 | .. sourcecode:: ipython |
|
379 | .. sourcecode:: ipython | |
385 |
|
380 | |||
386 | In [1]: from IPython.lib import passwd |
|
381 | In [1]: from IPython.lib import passwd | |
387 | In [2]: passwd() |
|
382 | In [2]: passwd() | |
388 | Enter password: |
|
383 | Enter password: | |
389 | Verify password: |
|
384 | Verify password: | |
390 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
385 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
391 |
|
386 | |||
392 | .. note:: |
|
387 | .. note:: | |
393 |
|
388 | |||
394 | :func:`~IPython.lib.security.passwd` can also take the password as a string |
|
389 | :func:`~IPython.lib.security.passwd` can also take the password as a string | |
395 | argument. **Do not** pass it as an argument inside an IPython session, as it |
|
390 | argument. **Do not** pass it as an argument inside an IPython session, as it | |
396 | will be saved in your input history. |
|
391 | will be saved in your input history. | |
397 |
|
392 | |||
398 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: |
|
393 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: | |
399 |
|
394 | |||
400 | # Password to use for web authentication |
|
395 | # Password to use for web authentication | |
401 | c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
396 | c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
402 |
|
397 | |||
403 | When using a password, it is a good idea to also use SSL, so that your password |
|
398 | When using a password, it is a good idea to also use SSL, so that your password | |
404 | is not sent unencrypted by your browser. You can start the notebook to |
|
399 | is not sent unencrypted by your browser. You can start the notebook to | |
405 | communicate via a secure protocol mode using a self-signed certificate with the command:: |
|
400 | communicate via a secure protocol mode using a self-signed certificate with the command:: | |
406 |
|
401 | |||
407 | $ ipython notebook --certfile=mycert.pem |
|
402 | $ ipython notebook --certfile=mycert.pem | |
408 |
|
403 | |||
409 | .. note:: |
|
404 | .. note:: | |
410 |
|
405 | |||
411 | A self-signed certificate can be generated with ``openssl``. For example, the following command will create a certificate valid for 365 days with both the key and certificate data written to the same file:: |
|
406 | A self-signed certificate can be generated with ``openssl``. For example, the following command will create a certificate valid for 365 days with both the key and certificate data written to the same file:: | |
412 |
|
407 | |||
413 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem |
|
408 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem | |
414 |
|
409 | |||
415 | Your browser will warn you of a dangerous certificate because it is |
|
410 | Your browser will warn you of a dangerous certificate because it is | |
416 | self-signed. If you want to have a fully compliant certificate that will not |
|
411 | self-signed. If you want to have a fully compliant certificate that will not | |
417 | raise warnings, it is possible (but rather involved) to obtain one for free, |
|
412 | raise warnings, it is possible (but rather involved) to obtain one for free, | |
418 | `as explained in detailed in this tutorial`__. |
|
413 | `as explained in detailed in this tutorial`__. | |
419 |
|
414 | |||
420 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars |
|
415 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars | |
421 |
|
416 | |||
422 | Keep in mind that when you enable SSL support, you'll need to access the |
|
417 | Keep in mind that when you enable SSL support, you'll need to access the | |
423 | notebook server over ``https://``, not over plain ``http://``. The startup |
|
418 | notebook server over ``https://``, not over plain ``http://``. The startup | |
424 | message from the server prints this, but it's easy to overlook and think the |
|
419 | message from the server prints this, but it's easy to overlook and think the | |
425 | server is for some reason non-responsive. |
|
420 | server is for some reason non-responsive. | |
426 |
|
421 | |||
427 |
|
422 | |||
428 | Connecting to an existing kernel |
|
423 | Connecting to an existing kernel | |
429 | --------------------------------- |
|
424 | --------------------------------- | |
430 |
|
425 | |||
431 | The notebook server always prints to the terminal the full details of |
|
426 | The notebook server always prints to the terminal the full details of | |
432 | how to connect to each kernel, with lines like:: |
|
427 | how to connect to each kernel, with lines like:: | |
433 |
|
428 | |||
434 | [IPKernelApp] To connect another client to this kernel, use: |
|
429 | [IPKernelApp] To connect another client to this kernel, use: | |
435 | [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json |
|
430 | [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |
436 |
|
431 | |||
437 | This is the name of a JSON file that contains all the port and |
|
432 | This is the name of a JSON file that contains all the port and | |
438 | validation information necessary to connect to the kernel. You can |
|
433 | validation information necessary to connect to the kernel. You can | |
439 | manually start a Qt console with:: |
|
434 | manually start a Qt console with:: | |
440 |
|
435 | |||
441 | ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json |
|
436 | ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json | |
442 |
|
437 | |||
443 | and if you only have a single kernel running, simply typing:: |
|
438 | and if you only have a single kernel running, simply typing:: | |
444 |
|
439 | |||
445 | ipython qtconsole --existing |
|
440 | ipython qtconsole --existing | |
446 |
|
441 | |||
447 | will automatically find it (it will always find the most recently |
|
442 | will automatically find it (it will always find the most recently | |
448 | started kernel if there is more than one). You can also request this |
|
443 | started kernel if there is more than one). You can also request this | |
449 | connection data by typing ``%connect_info``; this will print the same |
|
444 | connection data by typing ``%connect_info``; this will print the same | |
450 | file information as well as the content of the JSON data structure it contains. |
|
445 | file information as well as the content of the JSON data structure it contains. | |
451 |
|
446 | |||
452 |
|
447 | |||
453 | Running a public notebook server |
|
448 | Running a public notebook server | |
454 | -------------------------------- |
|
449 | -------------------------------- | |
455 |
|
450 | |||
456 | If you want to access your notebook server remotely with just a web browser, |
|
451 | If you want to access your notebook server remotely with just a web browser, | |
457 | here is a quick set of instructions. Start by creating a certificate file and |
|
452 | here is a quick set of instructions. Start by creating a certificate file and | |
458 | a hashed password as explained above. Then, create a custom profile for the |
|
453 | a hashed password as explained above. Then, create a custom profile for the | |
459 | notebook. At the command line, type:: |
|
454 | notebook. At the command line, type:: | |
460 |
|
455 | |||
461 | ipython profile create nbserver |
|
456 | ipython profile create nbserver | |
462 |
|
457 | |||
463 | In the profile directory, edit the file ``ipython_notebook_config.py``. By |
|
458 | In the profile directory, edit the file ``ipython_notebook_config.py``. By | |
464 | default the file has all fields commented, the minimum set you need to |
|
459 | default the file has all fields commented, the minimum set you need to | |
465 | uncomment and edit is here:: |
|
460 | uncomment and edit is here:: | |
466 |
|
461 | |||
467 | c = get_config() |
|
462 | c = get_config() | |
468 |
|
463 | |||
469 | # Kernel config |
|
464 | # Kernel config | |
470 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always |
|
465 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always | |
471 |
|
466 | |||
472 | # Notebook config |
|
467 | # Notebook config | |
473 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' |
|
468 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' | |
474 | c.NotebookApp.ip = '*' |
|
469 | c.NotebookApp.ip = '*' | |
475 | c.NotebookApp.open_browser = False |
|
470 | c.NotebookApp.open_browser = False | |
476 | c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here' |
|
471 | c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here' | |
477 | # It's a good idea to put it on a known, fixed port |
|
472 | # It's a good idea to put it on a known, fixed port | |
478 | c.NotebookApp.port = 9999 |
|
473 | c.NotebookApp.port = 9999 | |
479 |
|
474 | |||
480 | You can then start the notebook and access it later by pointing your browser to |
|
475 | You can then start the notebook and access it later by pointing your browser to | |
481 | ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``. |
|
476 | ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``. | |
482 |
|
477 | |||
483 | Running with a different URL prefix |
|
478 | Running with a different URL prefix | |
484 | ----------------------------------- |
|
479 | ----------------------------------- | |
485 |
|
480 | |||
486 | The notebook dashboard (i.e. the default landing page with an overview |
|
481 | The notebook dashboard (i.e. the default landing page with an overview | |
487 | of all your notebooks) typically lives at a URL path of |
|
482 | of all your notebooks) typically lives at a URL path of | |
488 | "http://localhost:8888/". If you want to have it, and the rest of the |
|
483 | "http://localhost:8888/". If you want to have it, and the rest of the | |
489 | notebook, live under a sub-directory, |
|
484 | notebook, live under a sub-directory, | |
490 | e.g. "http://localhost:8888/ipython/", you can do so with |
|
485 | e.g. "http://localhost:8888/ipython/", you can do so with | |
491 | configuration options like these (see above for instructions about |
|
486 | configuration options like these (see above for instructions about | |
492 | modifying ``ipython_notebook_config.py``):: |
|
487 | modifying ``ipython_notebook_config.py``):: | |
493 |
|
488 | |||
494 | c.NotebookApp.base_project_url = '/ipython/' |
|
489 | c.NotebookApp.base_project_url = '/ipython/' | |
495 | c.NotebookApp.base_kernel_url = '/ipython/' |
|
490 | c.NotebookApp.base_kernel_url = '/ipython/' | |
496 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} |
|
491 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} | |
497 |
|
492 | |||
498 | Using a different notebook store |
|
493 | Using a different notebook store | |
499 | -------------------------------- |
|
494 | -------------------------------- | |
500 |
|
495 | |||
501 | By default the notebook server stores notebooks as files in the working |
|
496 | By default the notebook server stores notebooks as files in the working | |
502 | directory of the notebook server, also known as the ``notebook_dir``. This |
|
497 | directory of the notebook server, also known as the ``notebook_dir``. This | |
503 | logic is implemented in the :class:`FileNotebookManager` class. However, the |
|
498 | logic is implemented in the :class:`FileNotebookManager` class. However, the | |
504 | server can be configured to use a different notebook manager class, which can |
|
499 | server can be configured to use a different notebook manager class, which can | |
505 | store the notebooks in a different format. Currently, we ship a |
|
500 | store the notebooks in a different format. Currently, we ship a | |
506 | :class:`AzureNotebookManager` class that stores notebooks in Azure blob |
|
501 | :class:`AzureNotebookManager` class that stores notebooks in Azure blob | |
507 | storage. This can be used by adding the following lines to your |
|
502 | storage. This can be used by adding the following lines to your | |
508 | ``ipython_notebook_config.py`` file:: |
|
503 | ``ipython_notebook_config.py`` file:: | |
509 |
|
504 | |||
510 | c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' |
|
505 | c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' | |
511 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' |
|
506 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' | |
512 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' |
|
507 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' | |
513 | c.AzureNotebookManager.container = u'notebooks' |
|
508 | c.AzureNotebookManager.container = u'notebooks' | |
514 |
|
509 | |||
515 | In addition to providing your Azure Blob Storage account name and key, you will |
|
510 | In addition to providing your Azure Blob Storage account name and key, you will | |
516 | have to provide a container name; you can use multiple containers to organize |
|
511 | have to provide a container name; you can use multiple containers to organize | |
517 | your Notebooks. |
|
512 | your Notebooks. | |
518 |
|
513 | |||
519 | .. _notebook_format: |
|
514 | .. _notebook_format: | |
520 |
|
515 | |||
521 | Notebook JSON format |
|
516 | Notebook JSON format | |
522 | ==================== |
|
517 | ==================== | |
523 |
|
518 | |||
524 | Notebooks are JSON files with an ``.ipynb`` extension, formatted |
|
519 | Notebooks are JSON files with an ``.ipynb`` extension, formatted | |
525 | as legibly as possible with minimal extra indentation and cell content broken |
|
520 | as legibly as possible with minimal extra indentation and cell content broken | |
526 | across lines to make them reasonably friendly to use in version-control |
|
521 | across lines to make them reasonably friendly to use in version-control | |
527 | workflows. You should be very careful if you ever manually edit this JSON |
|
522 | workflows. You should be very careful if you ever manually edit this JSON | |
528 | data, as it is extremely easy to corrupt its internal structure and make the |
|
523 | data, as it is extremely easy to corrupt its internal structure and make the | |
529 | file impossible to load. In general, you should consider the notebook as a |
|
524 | file impossible to load. In general, you should consider the notebook as a | |
530 | file meant only to be edited by the IPython Notebook app itself, not for hand-editing. |
|
525 | file meant only to be edited by the IPython Notebook app itself, not for hand-editing. | |
531 |
|
526 | |||
532 | .. note:: |
|
527 | .. note:: | |
533 |
|
528 | |||
534 | Binary data such as figures are directly saved in the JSON file. This |
|
529 | Binary data such as figures are directly saved in the JSON file. This | |
535 | provides convenient single-file portability, but means that the files can |
|
530 | provides convenient single-file portability, but means that the files can | |
536 | be large; ``diff``s of binary data also are not very meaningful. Since the |
|
531 | be large; ``diff``s of binary data also are not very meaningful. Since the | |
537 | binary blobs are encoded in a single line, they affect only one line of |
|
532 | binary blobs are encoded in a single line, they affect only one line of | |
538 | the ``diff`` output, but they are typically very long lines. You can use the ``Cell -> All Output -> Clear`` menu option to remove all output from a notebook prior to committing it to version control, if this is a concern. |
|
533 | the ``diff`` output, but they are typically very long lines. You can use the ``Cell -> All Output -> Clear`` menu option to remove all output from a notebook prior to committing it to version control, if this is a concern. | |
539 |
|
534 | |||
540 | The notebook server can also generate a pure Python version of your notebook, |
|
535 | The notebook server can also generate a pure Python version of your notebook, | |
541 | using the ``File -> Download as`` menu option. The resulting ``.py`` file will |
|
536 | using the ``File -> Download as`` menu option. The resulting ``.py`` file will | |
542 | contain all the code cells from your notebook verbatim, and all text cells |
|
537 | contain all the code cells from your notebook verbatim, and all text cells | |
543 | prepended with a comment marker. The separation between code and text |
|
538 | prepended with a comment marker. The separation between code and text | |
544 | cells is indicated with special comments and there is a header indicating the |
|
539 | cells is indicated with special comments and there is a header indicating the | |
545 | format version. All output is stripped out when exporting to Python. |
|
540 | format version. All output is stripped out when exporting to Python. | |
546 |
|
541 | |||
547 | Here is an example of the Python output from a simple notebook with one text cell and one code input cell:: |
|
542 | Here is an example of the Python output from a simple notebook with one text cell and one code input cell:: | |
548 |
|
543 | |||
549 | # <nbformat>2</nbformat> |
|
544 | # <nbformat>2</nbformat> | |
550 |
|
545 | |||
551 | # <markdowncell> |
|
546 | # <markdowncell> | |
552 |
|
547 | |||
553 | # A text cell |
|
548 | # A text cell | |
554 |
|
549 | |||
555 | # <codecell> |
|
550 | # <codecell> | |
556 |
|
551 | |||
557 | print "Hello, IPython!" |
|
552 | print "Hello, IPython!" | |
558 |
|
553 | |||
559 |
|
554 | |||
560 | Known issues |
|
555 | Known issues | |
561 | ============ |
|
556 | ============ | |
562 |
|
557 | |||
563 | When behind a proxy, especially if your system or browser is set to autodetect |
|
558 | When behind a proxy, especially if your system or browser is set to autodetect | |
564 | the proxy, the Notebook app might fail to connect to the server's websockets, |
|
559 | the proxy, the Notebook app might fail to connect to the server's websockets, | |
565 | and present you with a warning at startup. In this case, you need to configure |
|
560 | and present you with a warning at startup. In this case, you need to configure | |
566 | your system not to use the proxy for the server's address. |
|
561 | your system not to use the proxy for the server's address. | |
567 |
|
562 | |||
568 | In Firefox, for example, go to the Preferences panel, Advanced section, |
|
563 | In Firefox, for example, go to the Preferences panel, Advanced section, | |
569 | Network tab, click 'Settings...', and add the address of the notebook server |
|
564 | Network tab, click 'Settings...', and add the address of the notebook server | |
570 | to the 'No proxy for' field. |
|
565 | to the 'No proxy for' field. | |
571 |
|
566 | |||
572 |
|
567 | |||
573 | .. _Markdown: http://daringfireball.net/projects/markdown/basics |
|
568 | .. _Markdown: http://daringfireball.net/projects/markdown/basics |
General Comments 0
You need to be logged in to leave comments.
Login now