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