Show More
@@ -1,39 +1,203 | |||||
1 | .. _htmlnotebook: |
|
1 | .. _htmlnotebook: | |
2 |
|
2 | |||
3 | ========================= |
|
3 | ========================= | |
4 | An HTML Notebook IPython |
|
4 | An HTML Notebook IPython | |
5 | ========================= |
|
5 | ========================= | |
6 |
|
6 | |||
7 | The IPython Notebook consists of two related components: |
|
7 | The IPython Notebook consists of two related components: | |
8 |
|
8 | |||
9 | * An XML/JSON based Notebook document format for recording and distributing |
|
9 | * An XML/JSON based Notebook document format for recording and distributing | |
10 | Python code and rich text. |
|
10 | Python code and rich text. | |
11 | * A web-based user interface for authoring and running notebook documents. |
|
11 | * A web-based user interface for authoring and running notebook documents. | |
12 |
|
12 | |||
13 | The Notebook can be used by starting the Notebook server with the |
|
13 | The Notebook can be used by starting the Notebook server with the | |
14 | command:: |
|
14 | command:: | |
15 |
|
15 | |||
16 | $ ipython notebook |
|
16 | $ ipython notebook | |
17 |
|
17 | |||
|
18 | Note that by default, the notebook doesn't load pylab, it's just a normal | |||
|
19 | IPython session like any other. If you want pylab support, you must use:: | |||
|
20 | ||||
|
21 | $ ipython notebook --pylab | |||
|
22 | ||||
|
23 | which will behave similar to the terminal and Qt console versions, using your | |||
|
24 | default matplotlib backend and providing floating interactive plot windows. If | |||
|
25 | you want inline figures, you must manually select the ``inline`` backend:: | |||
|
26 | ||||
|
27 | $ ipython notebook --pylab inline | |||
|
28 | ||||
18 | This server uses the same ZeroMQ-based two process kernel architecture as |
|
29 | This server uses the same ZeroMQ-based two process kernel architecture as | |
19 | the QT Console as well Tornado for serving HTTP requests. Some of the main |
|
30 | the QT Console as well Tornado for serving HTTP requests. Some of the main | |
20 | features of the Notebook include: |
|
31 | features of the Notebook include: | |
21 |
|
32 | |||
22 | * Display rich data (png/html/latex/svg) in the browser as a result of |
|
33 | * Display rich data (png/html/latex/svg) in the browser as a result of | |
23 | computations. |
|
34 | computations. | |
24 | * Compose text cells using HTML and Markdown. |
|
35 | * Compose text cells using HTML and Markdown. | |
25 | * Import and export notebook documents in range of formats (.ipynb, .json, .py). |
|
36 | * Import and export notebook documents in range of formats (.ipynb, .json, .py). | |
26 | * In browser syntax highlighting, tab completion and autoindentation. |
|
37 | * In browser syntax highlighting, tab completion and autoindentation. | |
27 | * Inline matplotlib plots that can be stored in Notebook documents and opened |
|
38 | * Inline matplotlib plots that can be stored in Notebook documents and opened | |
28 | later. |
|
39 | later. | |
29 |
|
40 | |||
30 | See :ref:`our installation documentation <install_index>` for directions on |
|
41 | See :ref:`our installation documentation <install_index>` for directions on | |
31 | how to install the notebook and its dependencies. |
|
42 | how to install the notebook and its dependencies. | |
32 |
|
43 | |||
|
44 | .. note:: | |||
|
45 | ||||
|
46 | You can start more than one notebook server at the same time, if you want to | |||
|
47 | work on notebooks in different directories. By default the first notebook | |||
|
48 | server starts in port 8888, later notebooks search for random ports near | |||
|
49 | that one. You can also manually specify the port with the ``--port`` | |||
|
50 | option, if you want persistent URLs you can bookmark. | |||
|
51 | ||||
|
52 | ||||
33 | Basic Usage |
|
53 | Basic Usage | |
34 | =========== |
|
54 | =========== | |
35 |
|
55 | |||
|
56 | The landing page of the notebook server application, which we call the IPython | |||
|
57 | Notebook *dashboard*, shows the notebooks currently available in the directory | |||
|
58 | in which the application was started, and allows you to create new notebooks. | |||
|
59 | ||||
|
60 | A notebook is a combination of two things: | |||
|
61 | ||||
|
62 | 1. an interactive session connected to an IPython kernel, controlled by a web | |||
|
63 | application that can send input to the console and display many types of output | |||
|
64 | (text, graphics, mathematics and more). This is the same kernel used by the | |||
|
65 | :ref:`Qt console <qt_console>`, but in this case the web console sends input in | |||
|
66 | persistent cells that you can edit in-place instead of the vertically scrolling | |||
|
67 | terminal style used by the Qt console. | |||
|
68 | ||||
|
69 | 2. a document that can save the inputs and outputs of the session as well as | |||
|
70 | additional text that accompanies the code but is not meant for execution. In | |||
|
71 | this way, notebook files serve as a complete computational record of a session | |||
|
72 | including explanatory text and mathematics, code and resulting figures. These | |||
|
73 | documents are internally JSON files and are saved with the ``.ipynb`` | |||
|
74 | extension. | |||
|
75 | ||||
|
76 | If you have ever used the Mathematica or Sage notebooks (the latter is also | |||
|
77 | web-based__) you should feel right at home. If you have not, you should be | |||
|
78 | able to learn how to use it in just a few minutes. | |||
|
79 | ||||
|
80 | .. __: http://sagenb.org | |||
|
81 | ||||
|
82 | ||||
|
83 | Creating and editing notebooks | |||
|
84 | ------------------------------ | |||
|
85 | ||||
|
86 | You can create new notebooks from the dashboard with the ``New Notebook`` | |||
|
87 | button or open existing ones by clicking on their name. Once in a notebook, | |||
|
88 | your browser tab will reflect the name of that notebook (prefixed with "IPy:"). | |||
|
89 | The URL for that notebook is not meant to be human-readable, but it is | |||
|
90 | persistent across invocations of the notebook server *as long as you don't | |||
|
91 | rename the notebook*, so you can bookmark them for future use. | |||
|
92 | ||||
|
93 | You can also drag and dropp into the area listing files any python file: it | |||
|
94 | will be imported into a notebook with the same name (but ``.ipynb`` extension) | |||
|
95 | located in the directory where the notebook server was started. This notebook | |||
|
96 | will consist of a single cell with all the code in the file, which you can | |||
|
97 | later manually partition into individual cells for gradual execution, add text | |||
|
98 | and graphics, etc. | |||
|
99 | ||||
|
100 | Workflow and limitations | |||
|
101 | ------------------------ | |||
|
102 | ||||
|
103 | The normal workflow in a notebook is quite similar to a normal IPython session, | |||
|
104 | with the difference that you can edit a cell in-place multiple times until you | |||
|
105 | obtain the desired results rather than having to rerun separate scripts with | |||
|
106 | the ``%run`` magic (though magics also work in the notebook). Typically | |||
|
107 | you'll work on a problem in pieces, organizing related pieces into cells and | |||
|
108 | moving forward as previous parts work correctly. This is much more convenient | |||
|
109 | for interactive exploration than breaking up a computation into scripts that | |||
|
110 | must be executed together, especially if parts of them take a long time to run | |||
|
111 | (you can use tricks with namespaces and ``%run -i``, but we think the notebook | |||
|
112 | is a more natural solution for that kind of problem). | |||
|
113 | ||||
|
114 | The only significant limitation the notebook currently has, compared to the qt | |||
|
115 | console, is that it can not run any code that expects input from the kernel | |||
|
116 | (such as scripts that call :func:`raw_input`). Very importantly, this means | |||
|
117 | that the ``%debug`` magic does *not* work in the notebook! We intend to | |||
|
118 | correct this limitation, but in the meantime, there is a way to debug problems | |||
|
119 | in the notebook: you can attach a Qt console to your existing notebook kernel, | |||
|
120 | and run ``%debug`` from the Qt console. Simply look for the lines in the | |||
|
121 | terminal where you started the kernel that read something like:: | |||
|
122 | ||||
|
123 | [IPKernelApp] To connect another client to this kernel, use: | |||
|
124 | [IPKernelApp] --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543 | |||
|
125 | ||||
|
126 | and then start a qt console pointing to that kernel:: | |||
|
127 | ||||
|
128 | ipython qtconsole --existing --shell=53328 --iopub=53817 --stdin=34736 --hb=45543 | |||
|
129 | ||||
|
130 | ||||
|
131 | Text input | |||
|
132 | ---------- | |||
|
133 | ||||
|
134 | In addition to code cells and the output they procude (such as figures), you | |||
|
135 | can also type text not meant for execution. To type text, change the type of a | |||
|
136 | cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`C-m m` | |||
|
137 | keybinding (see below). You can then type any text in Markdown_ syntax, as | |||
|
138 | well as mathematical expressions if you use ``$...$`` for inline math or | |||
|
139 | ``$$...$$`` for displayed math. | |||
|
140 | ||||
|
141 | Exporting a notebook | |||
|
142 | -------------------- | |||
|
143 | ||||
|
144 | If you want to provide others with a static HTML or PDF view of your notebook, | |||
|
145 | use the ``Print`` button. This opens a static view of the document, which you | |||
|
146 | can print to PDF using your operating system's facilities, or save to a file | |||
|
147 | with your web browser's 'Save' option (note that typically, this will create | |||
|
148 | both an html file *and* a directory called `notebook_name_files` next to it | |||
|
149 | that contains all the necessary style information, so if you intend to share | |||
|
150 | this, you must send the directory along with the main html file). | |||
|
151 | ||||
|
152 | The `Download` button lets you save a notebook file to the Download area | |||
|
153 | configured by your web browser (particularly useful if you are running the | |||
|
154 | notebook server on a remote host and need a file locally). The notebook is | |||
|
155 | saved by default with the ``.ipynb`` extension and the files contain JSON data | |||
|
156 | that is not meant for human editing or consumption. But you can always export | |||
|
157 | the input part of a notebook to a plain python script by choosing Python format | |||
|
158 | in the `Download` drop list. This removes all output and saves the text cells | |||
|
159 | in comment areas. | |||
|
160 | ||||
|
161 | .. warning:: | |||
|
162 | ||||
|
163 | While in simple cases you can roundtrip a notebook to Python, edit the | |||
|
164 | python file and import it back without loss, this is in general *not | |||
|
165 | guaranteed to work at all*. As the notebook format evolves in complexity, | |||
|
166 | there will be attributes of the notebook that will not survive a roundtrip | |||
|
167 | through the Python form. You should think of the Python format as a way to | |||
|
168 | output a script version of a notebook and the import capabilities as a way | |||
|
169 | to load existing code to get a notebook started. But the Python version is | |||
|
170 | *not* an alternate Python format. | |||
|
171 | ||||
|
172 | ||||
|
173 | Keyboard use | |||
|
174 | ------------ | |||
|
175 | ||||
|
176 | All actions in the notebook can be achieved with the mouse, but we have also | |||
|
177 | added keyboard shortcuts for the most common ones, so that productive use of | |||
|
178 | the notebook can be achieved with minimal mouse intervention. The main | |||
|
179 | key bindings you need to remember are: | |||
|
180 | ||||
|
181 | * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console), | |||
|
182 | show output (if any) and create a new cell below. Note that in the notebook, | |||
|
183 | simply using :kbd:`Enter` *never* forces execution, it simply inserts a new | |||
|
184 | line in the current cell. Therefore, in the notebook you must always use | |||
|
185 | :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run | |||
|
186 | Selected`` button). | |||
|
187 | ||||
|
188 | * :kbd:`Control-Enter`: execute the current cell in "terminal mode", where any | |||
|
189 | output is shown but the cursor cursor stays in the current cell, whose input | |||
|
190 | area is flushed empty. This is convenient to do quick in-place experiments | |||
|
191 | or query things like filesystem content without creating additional cells you | |||
|
192 | may not want saved in your notebook. | |||
|
193 | ||||
|
194 | * :kbd:`Control-m`: this is the prefix for all other keybindings, which consist | |||
|
195 | of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole | |||
|
196 | letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining | |||
|
197 | available keybindings. | |||
|
198 | ||||
|
199 | ||||
36 | Notebook document format |
|
200 | Notebook document format | |
37 | ======================== |
|
201 | ======================== | |
38 |
|
202 | |||
39 |
|
203 |
General Comments 0
You need to be logged in to leave comments.
Login now