##// END OF EJS Templates
Merge pull request #1146 from minrk/savescript...
Fernando Perez -
r5711:1184eb4f merge
parent child Browse files
Show More
@@ -1,233 +1,255 b''
1 """A notebook manager that uses the local file system for storage.
1 """A notebook manager that uses the local file system for storage.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import datetime
19 import datetime
20 import os
20 import os
21 import uuid
21 import uuid
22 import glob
22 import glob
23
23
24 from tornado import web
24 from tornado import web
25
25
26 from IPython.config.configurable import LoggingConfigurable
26 from IPython.config.configurable import LoggingConfigurable
27 from IPython.nbformat import current
27 from IPython.nbformat import current
28 from IPython.utils.traitlets import Unicode, List, Dict
28 from IPython.utils.traitlets import Unicode, List, Dict, Bool
29
29
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Code
32 # Code
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34
34
35
35
36 class NotebookManager(LoggingConfigurable):
36 class NotebookManager(LoggingConfigurable):
37
37
38 notebook_dir = Unicode(os.getcwd(), config=True, help="""
38 notebook_dir = Unicode(os.getcwd(), config=True, help="""
39 The directory to use for notebooks.
39 The directory to use for notebooks.
40 """)
40 """)
41
42 save_script = Bool(False, config=True,
43 help="""Also save notebooks as a Python script.
44
45 For easier use of import/%loadpy across notebooks, a <notebook-name>.py
46 script will be created next to any <notebook-name>.ipynb on each save.
47 """
48 )
49
41 filename_ext = Unicode(u'.ipynb')
50 filename_ext = Unicode(u'.ipynb')
42 allowed_formats = List([u'json',u'py'])
51 allowed_formats = List([u'json',u'py'])
43
52
44 # Map notebook_ids to notebook names
53 # Map notebook_ids to notebook names
45 mapping = Dict()
54 mapping = Dict()
46 # Map notebook names to notebook_ids
55 # Map notebook names to notebook_ids
47 rev_mapping = Dict()
56 rev_mapping = Dict()
48
57
49 def list_notebooks(self):
58 def list_notebooks(self):
50 """List all notebooks in the notebook dir.
59 """List all notebooks in the notebook dir.
51
60
52 This returns a list of dicts of the form::
61 This returns a list of dicts of the form::
53
62
54 dict(notebook_id=notebook,name=name)
63 dict(notebook_id=notebook,name=name)
55 """
64 """
56 names = glob.glob(os.path.join(self.notebook_dir,
65 names = glob.glob(os.path.join(self.notebook_dir,
57 '*' + self.filename_ext))
66 '*' + self.filename_ext))
58 names = [os.path.splitext(os.path.basename(name))[0]
67 names = [os.path.splitext(os.path.basename(name))[0]
59 for name in names]
68 for name in names]
60
69
61 data = []
70 data = []
62 for name in names:
71 for name in names:
63 if name not in self.rev_mapping:
72 if name not in self.rev_mapping:
64 notebook_id = self.new_notebook_id(name)
73 notebook_id = self.new_notebook_id(name)
65 else:
74 else:
66 notebook_id = self.rev_mapping[name]
75 notebook_id = self.rev_mapping[name]
67 data.append(dict(notebook_id=notebook_id,name=name))
76 data.append(dict(notebook_id=notebook_id,name=name))
68 data = sorted(data, key=lambda item: item['name'])
77 data = sorted(data, key=lambda item: item['name'])
69 return data
78 return data
70
79
71 def new_notebook_id(self, name):
80 def new_notebook_id(self, name):
72 """Generate a new notebook_id for a name and store its mappings."""
81 """Generate a new notebook_id for a name and store its mappings."""
73 # TODO: the following will give stable urls for notebooks, but unless
82 # TODO: the following will give stable urls for notebooks, but unless
74 # the notebooks are immediately redirected to their new urls when their
83 # the notebooks are immediately redirected to their new urls when their
75 # filemname changes, nasty inconsistencies result. So for now it's
84 # filemname changes, nasty inconsistencies result. So for now it's
76 # disabled and instead we use a random uuid4() call. But we leave the
85 # disabled and instead we use a random uuid4() call. But we leave the
77 # logic here so that we can later reactivate it, whhen the necessary
86 # logic here so that we can later reactivate it, whhen the necessary
78 # url redirection code is written.
87 # url redirection code is written.
79 #notebook_id = unicode(uuid.uuid5(uuid.NAMESPACE_URL,
88 #notebook_id = unicode(uuid.uuid5(uuid.NAMESPACE_URL,
80 # 'file://'+self.get_path_by_name(name).encode('utf-8')))
89 # 'file://'+self.get_path_by_name(name).encode('utf-8')))
81
90
82 notebook_id = unicode(uuid.uuid4())
91 notebook_id = unicode(uuid.uuid4())
83
92
84 self.mapping[notebook_id] = name
93 self.mapping[notebook_id] = name
85 self.rev_mapping[name] = notebook_id
94 self.rev_mapping[name] = notebook_id
86 return notebook_id
95 return notebook_id
87
96
88 def delete_notebook_id(self, notebook_id):
97 def delete_notebook_id(self, notebook_id):
89 """Delete a notebook's id only. This doesn't delete the actual notebook."""
98 """Delete a notebook's id only. This doesn't delete the actual notebook."""
90 name = self.mapping[notebook_id]
99 name = self.mapping[notebook_id]
91 del self.mapping[notebook_id]
100 del self.mapping[notebook_id]
92 del self.rev_mapping[name]
101 del self.rev_mapping[name]
93
102
94 def notebook_exists(self, notebook_id):
103 def notebook_exists(self, notebook_id):
95 """Does a notebook exist?"""
104 """Does a notebook exist?"""
96 if notebook_id not in self.mapping:
105 if notebook_id not in self.mapping:
97 return False
106 return False
98 path = self.get_path_by_name(self.mapping[notebook_id])
107 path = self.get_path_by_name(self.mapping[notebook_id])
99 return os.path.isfile(path)
108 return os.path.isfile(path)
100
109
101 def find_path(self, notebook_id):
110 def find_path(self, notebook_id):
102 """Return a full path to a notebook given its notebook_id."""
111 """Return a full path to a notebook given its notebook_id."""
103 try:
112 try:
104 name = self.mapping[notebook_id]
113 name = self.mapping[notebook_id]
105 except KeyError:
114 except KeyError:
106 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
115 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
107 return self.get_path_by_name(name)
116 return self.get_path_by_name(name)
108
117
109 def get_path_by_name(self, name):
118 def get_path_by_name(self, name):
110 """Return a full path to a notebook given its name."""
119 """Return a full path to a notebook given its name."""
111 filename = name + self.filename_ext
120 filename = name + self.filename_ext
112 path = os.path.join(self.notebook_dir, filename)
121 path = os.path.join(self.notebook_dir, filename)
113 return path
122 return path
114
123
115 def get_notebook(self, notebook_id, format=u'json'):
124 def get_notebook(self, notebook_id, format=u'json'):
116 """Get the representation of a notebook in format by notebook_id."""
125 """Get the representation of a notebook in format by notebook_id."""
117 format = unicode(format)
126 format = unicode(format)
118 if format not in self.allowed_formats:
127 if format not in self.allowed_formats:
119 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
128 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
120 last_modified, nb = self.get_notebook_object(notebook_id)
129 last_modified, nb = self.get_notebook_object(notebook_id)
121 kwargs = {}
130 kwargs = {}
122 if format == 'json':
131 if format == 'json':
123 # don't split lines for sending over the wire, because it
132 # don't split lines for sending over the wire, because it
124 # should match the Python in-memory format.
133 # should match the Python in-memory format.
125 kwargs['split_lines'] = False
134 kwargs['split_lines'] = False
126 data = current.writes(nb, format, **kwargs)
135 data = current.writes(nb, format, **kwargs)
127 name = nb.get('name','notebook')
136 name = nb.get('name','notebook')
128 return last_modified, name, data
137 return last_modified, name, data
129
138
130 def get_notebook_object(self, notebook_id):
139 def get_notebook_object(self, notebook_id):
131 """Get the NotebookNode representation of a notebook by notebook_id."""
140 """Get the NotebookNode representation of a notebook by notebook_id."""
132 path = self.find_path(notebook_id)
141 path = self.find_path(notebook_id)
133 if not os.path.isfile(path):
142 if not os.path.isfile(path):
134 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
143 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
135 info = os.stat(path)
144 info = os.stat(path)
136 last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
145 last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
137 with open(path,'r') as f:
146 with open(path,'r') as f:
138 s = f.read()
147 s = f.read()
139 try:
148 try:
140 # v1 and v2 and json in the .ipynb files.
149 # v1 and v2 and json in the .ipynb files.
141 nb = current.reads(s, u'json')
150 nb = current.reads(s, u'json')
142 except:
151 except:
143 raise web.HTTPError(500, u'Unreadable JSON notebook.')
152 raise web.HTTPError(500, u'Unreadable JSON notebook.')
144 if 'name' not in nb:
153 if 'name' not in nb:
145 nb.name = os.path.split(path)[-1].split(u'.')[0]
154 nb.name = os.path.split(path)[-1].split(u'.')[0]
146 return last_modified, nb
155 return last_modified, nb
147
156
148 def save_new_notebook(self, data, name=None, format=u'json'):
157 def save_new_notebook(self, data, name=None, format=u'json'):
149 """Save a new notebook and return its notebook_id.
158 """Save a new notebook and return its notebook_id.
150
159
151 If a name is passed in, it overrides any values in the notebook data
160 If a name is passed in, it overrides any values in the notebook data
152 and the value in the data is updated to use that value.
161 and the value in the data is updated to use that value.
153 """
162 """
154 if format not in self.allowed_formats:
163 if format not in self.allowed_formats:
155 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
164 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
156
165
157 try:
166 try:
158 nb = current.reads(data.decode('utf-8'), format)
167 nb = current.reads(data.decode('utf-8'), format)
159 except:
168 except:
160 raise web.HTTPError(400, u'Invalid JSON data')
169 raise web.HTTPError(400, u'Invalid JSON data')
161
170
162 if name is None:
171 if name is None:
163 try:
172 try:
164 name = nb.metadata.name
173 name = nb.metadata.name
165 except AttributeError:
174 except AttributeError:
166 raise web.HTTPError(400, u'Missing notebook name')
175 raise web.HTTPError(400, u'Missing notebook name')
167 nb.metadata.name = name
176 nb.metadata.name = name
168
177
169 notebook_id = self.new_notebook_id(name)
178 notebook_id = self.new_notebook_id(name)
170 self.save_notebook_object(notebook_id, nb)
179 self.save_notebook_object(notebook_id, nb)
171 return notebook_id
180 return notebook_id
172
181
173 def save_notebook(self, notebook_id, data, name=None, format=u'json'):
182 def save_notebook(self, notebook_id, data, name=None, format=u'json'):
174 """Save an existing notebook by notebook_id."""
183 """Save an existing notebook by notebook_id."""
175 if format not in self.allowed_formats:
184 if format not in self.allowed_formats:
176 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
185 raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
177
186
178 try:
187 try:
179 nb = current.reads(data.decode('utf-8'), format)
188 nb = current.reads(data.decode('utf-8'), format)
180 except:
189 except:
181 raise web.HTTPError(400, u'Invalid JSON data')
190 raise web.HTTPError(400, u'Invalid JSON data')
182
191
183 if name is not None:
192 if name is not None:
184 nb.metadata.name = name
193 nb.metadata.name = name
185 self.save_notebook_object(notebook_id, nb)
194 self.save_notebook_object(notebook_id, nb)
186
195
187 def save_notebook_object(self, notebook_id, nb):
196 def save_notebook_object(self, notebook_id, nb):
188 """Save an existing notebook object by notebook_id."""
197 """Save an existing notebook object by notebook_id."""
189 if notebook_id not in self.mapping:
198 if notebook_id not in self.mapping:
190 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
199 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
191 old_name = self.mapping[notebook_id]
200 old_name = self.mapping[notebook_id]
192 try:
201 try:
193 new_name = nb.metadata.name
202 new_name = nb.metadata.name
194 except AttributeError:
203 except AttributeError:
195 raise web.HTTPError(400, u'Missing notebook name')
204 raise web.HTTPError(400, u'Missing notebook name')
196 path = self.get_path_by_name(new_name)
205 path = self.get_path_by_name(new_name)
197 try:
206 try:
198 with open(path,'w') as f:
207 with open(path,'w') as f:
199 current.write(nb, f, u'json')
208 current.write(nb, f, u'json')
200 except:
209 except Exception as e:
201 raise web.HTTPError(400, u'Unexpected error while saving notebook')
210 raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
211 # save .py script as well
212 if self.save_script:
213 pypath = os.path.splitext(path)[0] + '.py'
214 try:
215 with open(pypath,'w') as f:
216 current.write(nb, f, u'py')
217 except Exception as e:
218 raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e)
219
202 if old_name != new_name:
220 if old_name != new_name:
203 old_path = self.get_path_by_name(old_name)
221 old_path = self.get_path_by_name(old_name)
204 if os.path.isfile(old_path):
222 if os.path.isfile(old_path):
205 os.unlink(old_path)
223 os.unlink(old_path)
224 if self.save_script:
225 old_pypath = os.path.splitext(old_path)[0] + '.py'
226 if os.path.isfile(old_pypath):
227 os.unlink(old_pypath)
206 self.mapping[notebook_id] = new_name
228 self.mapping[notebook_id] = new_name
207 self.rev_mapping[new_name] = notebook_id
229 self.rev_mapping[new_name] = notebook_id
208
230
209 def delete_notebook(self, notebook_id):
231 def delete_notebook(self, notebook_id):
210 """Delete notebook by notebook_id."""
232 """Delete notebook by notebook_id."""
211 path = self.find_path(notebook_id)
233 path = self.find_path(notebook_id)
212 if not os.path.isfile(path):
234 if not os.path.isfile(path):
213 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
235 raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
214 os.unlink(path)
236 os.unlink(path)
215 self.delete_notebook_id(notebook_id)
237 self.delete_notebook_id(notebook_id)
216
238
217 def new_notebook(self):
239 def new_notebook(self):
218 """Create a new notebook and returns its notebook_id."""
240 """Create a new notebook and returns its notebook_id."""
219 i = 0
241 i = 0
220 while True:
242 while True:
221 name = u'Untitled%i' % i
243 name = u'Untitled%i' % i
222 path = self.get_path_by_name(name)
244 path = self.get_path_by_name(name)
223 if not os.path.isfile(path):
245 if not os.path.isfile(path):
224 break
246 break
225 else:
247 else:
226 i = i+1
248 i = i+1
227 notebook_id = self.new_notebook_id(name)
249 notebook_id = self.new_notebook_id(name)
228 metadata = current.new_metadata(name=name)
250 metadata = current.new_metadata(name=name)
229 nb = current.new_notebook(metadata=metadata)
251 nb = current.new_notebook(metadata=metadata)
230 with open(path,'w') as f:
252 with open(path,'w') as f:
231 current.write(nb, f, u'json')
253 current.write(nb, f, u'json')
232 return notebook_id
254 return notebook_id
233
255
@@ -1,375 +1,383 b''
1 .. _htmlnotebook:
1 .. _htmlnotebook:
2
2
3 =========================
3 =========================
4 An HTML Notebook IPython
4 An HTML Notebook IPython
5 =========================
5 =========================
6
6
7 .. seealso::
7 .. seealso::
8
8
9 :ref:`Installation requirements <installnotebook>` for the Notebook.
9 :ref:`Installation requirements <installnotebook>` for the Notebook.
10
10
11 The IPython Notebook consists of two related components:
11 The IPython Notebook consists of two related components:
12
12
13 * An JSON based Notebook document format for recording and distributing
13 * An JSON based Notebook document format for recording and distributing
14 Python code and rich text.
14 Python code and rich text.
15 * A web-based user interface for authoring and running notebook documents.
15 * A web-based user interface for authoring and running notebook documents.
16
16
17 The Notebook can be used by starting the Notebook server with the
17 The Notebook can be used by starting the Notebook server with the
18 command::
18 command::
19
19
20 $ ipython notebook
20 $ ipython notebook
21
21
22 Note that by default, the notebook doesn't load pylab, it's just a normal
22 Note that by default, the notebook doesn't load pylab, it's just a normal
23 IPython session like any other. If you want pylab support, you must use::
23 IPython session like any other. If you want pylab support, you must use::
24
24
25 $ ipython notebook --pylab
25 $ ipython notebook --pylab
26
26
27 which will behave similar to the terminal and Qt console versions, using your
27 which will behave similar to the terminal and Qt console versions, using your
28 default matplotlib backend and providing floating interactive plot windows. If
28 default matplotlib backend and providing floating interactive plot windows. If
29 you want inline figures, you must manually select the ``inline`` backend::
29 you want inline figures, you must manually select the ``inline`` backend::
30
30
31 $ ipython notebook --pylab inline
31 $ ipython notebook --pylab inline
32
32
33 This server uses the same ZeroMQ-based two process kernel architecture as
33 This server uses the same ZeroMQ-based two process kernel architecture as
34 the QT Console as well Tornado for serving HTTP/S requests. Some of the main
34 the QT Console as well Tornado for serving HTTP/S requests. Some of the main
35 features of the Notebook include:
35 features of the Notebook include:
36
36
37 * Display rich data (png/html/latex/svg) in the browser as a result of
37 * Display rich data (png/html/latex/svg) in the browser as a result of
38 computations.
38 computations.
39 * Compose text cells using HTML and Markdown.
39 * Compose text cells using HTML and Markdown.
40 * Import and export notebook documents in range of formats (.ipynb, .py).
40 * Import and export notebook documents in range of formats (.ipynb, .py).
41 * In browser syntax highlighting, tab completion and autoindentation.
41 * In browser syntax highlighting, tab completion and autoindentation.
42 * Inline matplotlib plots that can be stored in Notebook documents and opened
42 * Inline matplotlib plots that can be stored in Notebook documents and opened
43 later.
43 later.
44
44
45 See :ref:`our installation documentation <install_index>` for directions on
45 See :ref:`our installation documentation <install_index>` for directions on
46 how to install the notebook and its dependencies.
46 how to install the notebook and its dependencies.
47
47
48 .. note::
48 .. note::
49
49
50 You can start more than one notebook server at the same time, if you want to
50 You can start more than one notebook server at the same time, if you want to
51 work on notebooks in different directories. By default the first notebook
51 work on notebooks in different directories. By default the first notebook
52 server starts in port 8888, later notebooks search for random ports near
52 server starts in port 8888, later notebooks search for random ports near
53 that one. You can also manually specify the port with the ``--port``
53 that one. You can also manually specify the port with the ``--port``
54 option.
54 option.
55
55
56
56
57 Basic Usage
57 Basic Usage
58 ===========
58 ===========
59
59
60 The landing page of the notebook server application, which we call the IPython
60 The landing page of the notebook server application, which we call the IPython
61 Notebook *dashboard*, shows the notebooks currently available in the directory
61 Notebook *dashboard*, shows the notebooks currently available in the directory
62 in which the application was started, and allows you to create new notebooks.
62 in which the application was started, and allows you to create new notebooks.
63
63
64 A notebook is a combination of two things:
64 A notebook is a combination of two things:
65
65
66 1. An interactive session connected to an IPython kernel, controlled by a web
66 1. An interactive session connected to an IPython kernel, controlled by a web
67 application that can send input to the console and display many types of
67 application that can send input to the console and display many types of
68 output (text, graphics, mathematics and more). This is the same kernel used
68 output (text, graphics, mathematics and more). This is the same kernel used
69 by the :ref:`Qt console <qtconsole>`, but in this case the web console sends
69 by the :ref:`Qt console <qtconsole>`, but in this case the web console sends
70 input in persistent cells that you can edit in-place instead of the
70 input in persistent cells that you can edit in-place instead of the
71 vertically scrolling terminal style used by the Qt console.
71 vertically scrolling terminal style used by the Qt console.
72
72
73 2. A document that can save the inputs and outputs of the session as well as
73 2. A document that can save the inputs and outputs of the session as well as
74 additional text that accompanies the code but is not meant for execution.
74 additional text that accompanies the code but is not meant for execution.
75 In this way, notebook files serve as a complete computational record of a
75 In this way, notebook files serve as a complete computational record of a
76 session including explanatory text and mathematics, code and resulting
76 session including explanatory text and mathematics, code and resulting
77 figures. These documents are internally JSON files and are saved with the
77 figures. These documents are internally JSON files and are saved with the
78 ``.ipynb`` extension.
78 ``.ipynb`` extension.
79
79
80 If you have ever used the Mathematica or Sage notebooks (the latter is also
80 If you have ever used the Mathematica or Sage notebooks (the latter is also
81 web-based__) you should feel right at home. If you have not, you should be
81 web-based__) you should feel right at home. If you have not, you should be
82 able to learn how to use it in just a few minutes.
82 able to learn how to use it in just a few minutes.
83
83
84 .. __: http://sagenb.org
84 .. __: http://sagenb.org
85
85
86
86
87 Creating and editing notebooks
87 Creating and editing notebooks
88 ------------------------------
88 ------------------------------
89
89
90 You can create new notebooks from the dashboard with the ``New Notebook``
90 You can create new notebooks from the dashboard with the ``New Notebook``
91 button or open existing ones by clicking on their name. Once in a notebook,
91 button or open existing ones by clicking on their name. Once in a notebook,
92 your browser tab will reflect the name of that notebook (prefixed with "IPy:").
92 your browser tab will reflect the name of that notebook (prefixed with "IPy:").
93 The URL for that notebook is not meant to be human-readable and is *not*
93 The URL for that notebook is not meant to be human-readable and is *not*
94 persistent across invocations of the notebook server.
94 persistent across invocations of the notebook server.
95
95
96 You can also drag and drop into the area listing files any python file: it
96 You can also drag and drop into the area listing files any python file: it
97 will be imported into a notebook with the same name (but ``.ipynb`` extension)
97 will be imported into a notebook with the same name (but ``.ipynb`` extension)
98 located in the directory where the notebook server was started. This notebook
98 located in the directory where the notebook server was started. This notebook
99 will consist of a single cell with all the code in the file, which you can
99 will consist of a single cell with all the code in the file, which you can
100 later manually partition into individual cells for gradual execution, add text
100 later manually partition into individual cells for gradual execution, add text
101 and graphics, etc.
101 and graphics, etc.
102
102
103
103
104 Workflow and limitations
104 Workflow and limitations
105 ------------------------
105 ------------------------
106
106
107 The normal workflow in a notebook is quite similar to a normal IPython session,
107 The normal workflow in a notebook is quite similar to a normal IPython session,
108 with the difference that you can edit a cell in-place multiple times until you
108 with the difference that you can edit a cell in-place multiple times until you
109 obtain the desired results rather than having to rerun separate scripts with
109 obtain the desired results rather than having to rerun separate scripts with
110 the ``%run`` magic (though magics also work in the notebook). Typically
110 the ``%run`` magic (though magics also work in the notebook). Typically
111 you'll work on a problem in pieces, organizing related pieces into cells and
111 you'll work on a problem in pieces, organizing related pieces into cells and
112 moving forward as previous parts work correctly. This is much more convenient
112 moving forward as previous parts work correctly. This is much more convenient
113 for interactive exploration than breaking up a computation into scripts that
113 for interactive exploration than breaking up a computation into scripts that
114 must be executed together, especially if parts of them take a long time to run
114 must be executed together, especially if parts of them take a long time to run
115 (In the traditional terminal-based IPython, you can use tricks with namespaces
115 (In the traditional terminal-based IPython, you can use tricks with namespaces
116 and ``%run -i`` to achieve this capability, but we think the notebook is a more
116 and ``%run -i`` to achieve this capability, but we think the notebook is a more
117 natural solution for that kind of problem).
117 natural solution for that kind of problem).
118
118
119 The only significant limitation the notebook currently has, compared to the qt
119 The only significant limitation the notebook currently has, compared to the qt
120 console, is that it can not run any code that expects input from the kernel
120 console, is that it can not run any code that expects input from the kernel
121 (such as scripts that call :func:`raw_input`). Very importantly, this means
121 (such as scripts that call :func:`raw_input`). Very importantly, this means
122 that the ``%debug`` magic does *not* work in the notebook! We intend to
122 that the ``%debug`` magic does *not* work in the notebook! We intend to
123 correct this limitation, but in the meantime, there is a way to debug problems
123 correct this limitation, but in the meantime, there is a way to debug problems
124 in the notebook: you can attach a Qt console to your existing notebook kernel,
124 in the notebook: you can attach a Qt console to your existing notebook kernel,
125 and run ``%debug`` from the Qt console. If your notebook is running on a local
125 and run ``%debug`` from the Qt console. If your notebook is running on a local
126 computer (i.e. if you are accessing it via your localhost address at
126 computer (i.e. if you are accessing it via your localhost address at
127 127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console
127 127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console
128 will open up connected to that same kernel.
128 will open up connected to that same kernel.
129
129
130 In general, the notebook server prints the full details of how to connect to
130 In general, the notebook server prints the full details of how to connect to
131 each kernel at the terminal, with lines like::
131 each kernel at the terminal, with lines like::
132
132
133 [IPKernelApp] To connect another client to this kernel, use:
133 [IPKernelApp] To connect another client to this kernel, use:
134 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
134 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
135
135
136 This is the name of a JSON file that contains all the port and validation
136 This is the name of a JSON file that contains all the port and validation
137 information necessary to connect to the kernel. You can manually start a
137 information necessary to connect to the kernel. You can manually start a
138 qt console with::
138 qt console with::
139
139
140 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
140 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
141
141
142 and if you only have a single kernel running, simply typing::
142 and if you only have a single kernel running, simply typing::
143
143
144 ipython qtconsole --existing
144 ipython qtconsole --existing
145
145
146 will automatically find it (it will always find the most recently started
146 will automatically find it (it will always find the most recently started
147 kernel if there is more than one). You can also request this connection data
147 kernel if there is more than one). You can also request this connection data
148 by typing ``%connect_info``; this will print the same file information as well
148 by typing ``%connect_info``; this will print the same file information as well
149 as the content of the JSON data structure it contains.
149 as the content of the JSON data structure it contains.
150
150
151
151
152 Text input
152 Text input
153 ----------
153 ----------
154
154
155 In addition to code cells and the output they produce (such as figures), you
155 In addition to code cells and the output they produce (such as figures), you
156 can also type text not meant for execution. To type text, change the type of a
156 can also type text not meant for execution. To type text, change the type of a
157 cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
157 cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
158 keybinding (see below). You can then type any text in Markdown_ syntax, as
158 keybinding (see below). You can then type any text in Markdown_ syntax, as
159 well as mathematical expressions if you use ``$...$`` for inline math or
159 well as mathematical expressions if you use ``$...$`` for inline math or
160 ``$$...$$`` for displayed math.
160 ``$$...$$`` for displayed math.
161
161
162
162
163 Exporting a notebook and importing existing scripts
163 Exporting a notebook and importing existing scripts
164 ---------------------------------------------------
164 ---------------------------------------------------
165
165
166 If you want to provide others with a static HTML or PDF view of your notebook,
166 If you want to provide others with a static HTML or PDF view of your notebook,
167 use the ``Print`` button. This opens a static view of the document, which you
167 use the ``Print`` button. This opens a static view of the document, which you
168 can print to PDF using your operating system's facilities, or save to a file
168 can print to PDF using your operating system's facilities, or save to a file
169 with your web browser's 'Save' option (note that typically, this will create
169 with your web browser's 'Save' option (note that typically, this will create
170 both an html file *and* a directory called `notebook_name_files` next to it
170 both an html file *and* a directory called `notebook_name_files` next to it
171 that contains all the necessary style information, so if you intend to share
171 that contains all the necessary style information, so if you intend to share
172 this, you must send the directory along with the main html file).
172 this, you must send the directory along with the main html file).
173
173
174 The `Download` button lets you save a notebook file to the Download area
174 The `Download` button lets you save a notebook file to the Download area
175 configured by your web browser (particularly useful if you are running the
175 configured by your web browser (particularly useful if you are running the
176 notebook server on a remote host and need a file locally). The notebook is
176 notebook server on a remote host and need a file locally). The notebook is
177 saved by default with the ``.ipynb`` extension and the files contain JSON data
177 saved by default with the ``.ipynb`` extension and the files contain JSON data
178 that is not meant for human editing or consumption. But you can always export
178 that is not meant for human editing or consumption. But you can always export
179 the input part of a notebook to a plain python script by choosing Python format
179 the input part of a notebook to a plain python script by choosing Python format
180 in the `Download` drop list. This removes all output and saves the text cells
180 in the `Download` drop list. This removes all output and saves the text cells
181 in comment areas. See ref:`below <notebook_format>` for more details on the
181 in comment areas. See ref:`below <notebook_format>` for more details on the
182 notebook format.
182 notebook format.
183
183
184 The notebook can also *import* ``.py`` files as notebooks, by dragging and
184 The notebook can also *import* ``.py`` files as notebooks, by dragging and
185 dropping the file into the notebook dashboard file list area. By default, the
185 dropping the file into the notebook dashboard file list area. By default, the
186 entire contents of the file will be loaded into a single code cell. But if
186 entire contents of the file will be loaded into a single code cell. But if
187 prior to import, you manually add the ``# <nbformat>2</nbformat>`` marker at
187 prior to import, you manually add the ``# <nbformat>2</nbformat>`` marker at
188 the start and then add separators for text/code cells, you can get a cleaner
188 the start and then add separators for text/code cells, you can get a cleaner
189 import with the file broken into individual cells.
189 import with the file broken into individual cells.
190
190
191 If you want use notebooks as scripts a lot, then you can set::
192
193 c.NotebookManager.save_script=True
194
195 which will instruct the notebook server to save the ``.py`` export of each
196 notebook adjacent to the ``.ipynb`` at every save. Then these can be ``%run``
197 or imported from regular IPython sessions or other notebooks.
198
191 .. warning::
199 .. warning::
192
200
193 While in simple cases you can roundtrip a notebook to Python, edit the
201 While in simple cases you can roundtrip a notebook to Python, edit the
194 python file and import it back without loss of main content, this is in
202 python file and import it back without loss of main content, this is in
195 general *not guaranteed to work at all*. First, there is extra metadata
203 general *not guaranteed to work at all*. First, there is extra metadata
196 saved in the notebook that may not be saved to the ``.py`` format. And as
204 saved in the notebook that may not be saved to the ``.py`` format. And as
197 the notebook format evolves in complexity, there will be attributes of the
205 the notebook format evolves in complexity, there will be attributes of the
198 notebook that will not survive a roundtrip through the Python form. You
206 notebook that will not survive a roundtrip through the Python form. You
199 should think of the Python format as a way to output a script version of a
207 should think of the Python format as a way to output a script version of a
200 notebook and the import capabilities as a way to load existing code to get a
208 notebook and the import capabilities as a way to load existing code to get a
201 notebook started. But the Python version is *not* an alternate notebook
209 notebook started. But the Python version is *not* an alternate notebook
202 format.
210 format.
203
211
204
212
205 Keyboard use
213 Keyboard use
206 ------------
214 ------------
207
215
208 All actions in the notebook can be achieved with the mouse, but we have also
216 All actions in the notebook can be achieved with the mouse, but we have also
209 added keyboard shortcuts for the most common ones, so that productive use of
217 added keyboard shortcuts for the most common ones, so that productive use of
210 the notebook can be achieved with minimal mouse intervention. The main
218 the notebook can be achieved with minimal mouse intervention. The main
211 key bindings you need to remember are:
219 key bindings you need to remember are:
212
220
213 * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console),
221 * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console),
214 show output (if any) and create a new cell below. Note that in the notebook,
222 show output (if any) and create a new cell below. Note that in the notebook,
215 simply using :kbd:`Enter` *never* forces execution, it simply inserts a new
223 simply using :kbd:`Enter` *never* forces execution, it simply inserts a new
216 line in the current cell. Therefore, in the notebook you must always use
224 line in the current cell. Therefore, in the notebook you must always use
217 :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run
225 :kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run
218 Selected`` button).
226 Selected`` button).
219
227
220 * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
228 * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
221 output is shown but the cursor stays in the current cell, whose input
229 output is shown but the cursor stays in the current cell, whose input
222 area is flushed empty. This is convenient to do quick in-place experiments
230 area is flushed empty. This is convenient to do quick in-place experiments
223 or query things like filesystem content without creating additional cells you
231 or query things like filesystem content without creating additional cells you
224 may not want saved in your notebook.
232 may not want saved in your notebook.
225
233
226 * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
234 * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
227 of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
235 of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
228 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
236 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
229 available keybindings.
237 available keybindings.
230
238
231
239
232 .. _notebook_security:
240 .. _notebook_security:
233
241
234 Security
242 Security
235 ========
243 ========
236
244
237 You can protect your notebook server with a simple single-password by
245 You can protect your notebook server with a simple single-password by
238 setting the :attr:`NotebookApp.password` configurable. You can prepare a
246 setting the :attr:`NotebookApp.password` configurable. You can prepare a
239 hashed password using the function :func:`IPython.lib.security.passwd`:
247 hashed password using the function :func:`IPython.lib.security.passwd`:
240
248
241 .. sourcecode:: ipython
249 .. sourcecode:: ipython
242
250
243 In [1]: from IPython.lib import passwd
251 In [1]: from IPython.lib import passwd
244 In [2]: passwd()
252 In [2]: passwd()
245 Enter password:
253 Enter password:
246 Verify password:
254 Verify password:
247 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
255 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
248
256
249 .. note::
257 .. note::
250
258
251 :func:`~IPython.lib.security.passwd` can also take the password as a string
259 :func:`~IPython.lib.security.passwd` can also take the password as a string
252 argument. **Do not** pass it as an argument inside an IPython session, as it
260 argument. **Do not** pass it as an argument inside an IPython session, as it
253 will be saved in your input history.
261 will be saved in your input history.
254
262
255 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
263 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
256
264
257 # Password to use for web authentication
265 # Password to use for web authentication
258 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
266 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
259
267
260 When using a password, it is a good idea to also use SSL, so that your password
268 When using a password, it is a good idea to also use SSL, so that your password
261 is not sent unencrypted by your browser. You can start the notebook to
269 is not sent unencrypted by your browser. You can start the notebook to
262 communicate via a secure protocol mode using a self-signed certificate by
270 communicate via a secure protocol mode using a self-signed certificate by
263 typing::
271 typing::
264
272
265 $ ipython notebook --certfile=mycert.pem
273 $ ipython notebook --certfile=mycert.pem
266
274
267 .. note::
275 .. note::
268
276
269 A self-signed certificate can be generated with openssl. For example, the
277 A self-signed certificate can be generated with openssl. For example, the
270 following command will create a certificate valid for 365 days with both
278 following command will create a certificate valid for 365 days with both
271 the key and certificate data written to the same file::
279 the key and certificate data written to the same file::
272
280
273 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
281 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
274
282
275 Your browser will warn you of a dangerous certificate because it is
283 Your browser will warn you of a dangerous certificate because it is
276 self-signed. If you want to have a fully compliant certificate that will not
284 self-signed. If you want to have a fully compliant certificate that will not
277 raise warnings, it is possible (but rather involved) to obtain one for free,
285 raise warnings, it is possible (but rather involved) to obtain one for free,
278 `as explained in detailed in this tutorial`__.
286 `as explained in detailed in this tutorial`__.
279
287
280 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
288 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
281
289
282 Keep in mind that when you enable SSL support, you'll need to access the
290 Keep in mind that when you enable SSL support, you'll need to access the
283 notebook server over ``https://``, not over plain ``http://``. The startup
291 notebook server over ``https://``, not over plain ``http://``. The startup
284 message from the server prints this, but it's easy to overlook and think the
292 message from the server prints this, but it's easy to overlook and think the
285 server is for some reason non-responsive.
293 server is for some reason non-responsive.
286
294
287
295
288 Quick Howto: running a public notebook server
296 Quick Howto: running a public notebook server
289 =============================================
297 =============================================
290
298
291 If you want to access your notebook server remotely with just a web browser,
299 If you want to access your notebook server remotely with just a web browser,
292 here is a quick set of instructions. Start by creating a certificate file and
300 here is a quick set of instructions. Start by creating a certificate file and
293 a hashed password as explained above. Then, create a custom profile for the
301 a hashed password as explained above. Then, create a custom profile for the
294 notebook. At the command line, type::
302 notebook. At the command line, type::
295
303
296 ipython profile create nbserver
304 ipython profile create nbserver
297
305
298 In the profile directory, edit the file ``ipython_notebook_config.py``. By
306 In the profile directory, edit the file ``ipython_notebook_config.py``. By
299 default the file has all fields commented, the minimum set you need to
307 default the file has all fields commented, the minimum set you need to
300 uncomment and edit is here::
308 uncomment and edit is here::
301
309
302 c = get_config()
310 c = get_config()
303
311
304 # Kernel config
312 # Kernel config
305 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
313 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
306
314
307 # Notebook config
315 # Notebook config
308 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
316 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
309 c.NotebookApp.ip = '*'
317 c.NotebookApp.ip = '*'
310 c.NotebookApp.open_browser = False
318 c.NotebookApp.open_browser = False
311 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
319 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
312 # It's a good idea to put it on a known, fixed port
320 # It's a good idea to put it on a known, fixed port
313 c.NotebookApp.port = 9999
321 c.NotebookApp.port = 9999
314
322
315 You can then start the notebook and access it later by pointing your browser to
323 You can then start the notebook and access it later by pointing your browser to
316 ``https://your.host.com:9999``.
324 ``https://your.host.com:9999``.
317
325
318 .. _notebook_format:
326 .. _notebook_format:
319
327
320 The notebook format
328 The notebook format
321 ===================
329 ===================
322
330
323 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
331 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
324 as legibly as possible with minimal extra indentation and cell content broken
332 as legibly as possible with minimal extra indentation and cell content broken
325 across lines to make them reasonably friendly to use in version-control
333 across lines to make them reasonably friendly to use in version-control
326 workflows. You should be very careful if you ever edit manually this JSON
334 workflows. You should be very careful if you ever edit manually this JSON
327 data, as it is extremely easy to corrupt its internal structure and make the
335 data, as it is extremely easy to corrupt its internal structure and make the
328 file impossible to load. In general, you should consider the notebook as a
336 file impossible to load. In general, you should consider the notebook as a
329 file meant only to be edited by IPython itself, not for hand-editing.
337 file meant only to be edited by IPython itself, not for hand-editing.
330
338
331 .. note::
339 .. note::
332
340
333 Binary data such as figures are directly saved in the JSON file. This
341 Binary data such as figures are directly saved in the JSON file. This
334 provides convenient single-file portability but means the files can be
342 provides convenient single-file portability but means the files can be
335 large and diffs of binary data aren't very meaningful. Since the binary
343 large and diffs of binary data aren't very meaningful. Since the binary
336 blobs are encoded in a single line they only affect one line of the diff
344 blobs are encoded in a single line they only affect one line of the diff
337 output, but they are typically very long lines. You can use the
345 output, but they are typically very long lines. You can use the
338 'ClearAll' button to remove all output from a notebook prior to
346 'ClearAll' button to remove all output from a notebook prior to
339 committing it to version control, if this is a concern.
347 committing it to version control, if this is a concern.
340
348
341 The notebook server can also generate a pure-python version of your notebook,
349 The notebook server can also generate a pure-python version of your notebook,
342 by clicking on the 'Download' button and selecting ``py`` as the format. This
350 by clicking on the 'Download' button and selecting ``py`` as the format. This
343 file will contain all the code cells from your notebook verbatim, and all text
351 file will contain all the code cells from your notebook verbatim, and all text
344 cells prepended with a comment marker. The separation between code and text
352 cells prepended with a comment marker. The separation between code and text
345 cells is indicated with special comments and there is a header indicating the
353 cells is indicated with special comments and there is a header indicating the
346 format version. All output is stripped out when exporting to python.
354 format version. All output is stripped out when exporting to python.
347
355
348 Here is an example of a simple notebook with one text cell and one code input
356 Here is an example of a simple notebook with one text cell and one code input
349 cell, when exported to python format::
357 cell, when exported to python format::
350
358
351 # <nbformat>2</nbformat>
359 # <nbformat>2</nbformat>
352
360
353 # <markdowncell>
361 # <markdowncell>
354
362
355 # A text cell
363 # A text cell
356
364
357 # <codecell>
365 # <codecell>
358
366
359 print "hello IPython"
367 print "hello IPython"
360
368
361
369
362 Known Issues
370 Known Issues
363 ============
371 ============
364
372
365 When behind a proxy, especially if your system or browser is set to autodetect
373 When behind a proxy, especially if your system or browser is set to autodetect
366 the proxy, the html notebook might fail to connect to the server's websockets,
374 the proxy, the html notebook might fail to connect to the server's websockets,
367 and present you with a warning at startup. In this case, you need to configure
375 and present you with a warning at startup. In this case, you need to configure
368 your system not to use the proxy for the server's address.
376 your system not to use the proxy for the server's address.
369
377
370 In Firefox, for example, go to the Preferences panel, Advanced section,
378 In Firefox, for example, go to the Preferences panel, Advanced section,
371 Network tab, click 'Settings...', and add the address of the notebook server
379 Network tab, click 'Settings...', and add the address of the notebook server
372 to the 'No proxy for' field.
380 to the 'No proxy for' field.
373
381
374
382
375 .. _Markdown: http://daringfireball.net/projects/markdown/basics
383 .. _Markdown: http://daringfireball.net/projects/markdown/basics
General Comments 0
You need to be logged in to leave comments. Login now