2012-02-03 06:41:20 +08:00
|
|
|
# coding: utf-8
|
2014-04-12 02:24:30 +08:00
|
|
|
"""A tornado based IPython notebook server."""
|
2011-08-18 04:21:19 +08:00
|
|
|
|
2014-04-12 02:24:30 +08:00
|
|
|
# Copyright (c) IPython Development Team.
|
|
|
|
# Distributed under the terms of the Modified BSD License.
|
2011-08-18 04:21:19 +08:00
|
|
|
|
2013-10-24 05:58:57 +08:00
|
|
|
from __future__ import print_function
|
2014-04-18 03:25:07 +08:00
|
|
|
|
2014-07-04 00:18:11 +08:00
|
|
|
import base64
|
2011-08-15 12:41:01 +08:00
|
|
|
import errno
|
2014-01-09 05:02:36 +08:00
|
|
|
import io
|
|
|
|
import json
|
2011-03-24 01:00:52 +08:00
|
|
|
import logging
|
2011-03-18 12:09:04 +08:00
|
|
|
import os
|
2012-06-01 06:06:33 +08:00
|
|
|
import random
|
2014-06-28 08:47:56 +08:00
|
|
|
import re
|
2012-04-16 12:48:37 +08:00
|
|
|
import select
|
2011-07-13 05:32:03 +08:00
|
|
|
import signal
|
2011-08-15 12:41:01 +08:00
|
|
|
import socket
|
2011-07-13 05:32:03 +08:00
|
|
|
import sys
|
2011-10-28 08:53:27 +08:00
|
|
|
import threading
|
2012-04-17 02:59:52 +08:00
|
|
|
import time
|
2011-10-18 05:31:26 +08:00
|
|
|
import webbrowser
|
2011-03-24 01:00:52 +08:00
|
|
|
|
2013-03-29 01:20:44 +08:00
|
|
|
|
2013-04-13 12:33:49 +08:00
|
|
|
# check for pyzmq 2.1.11
|
|
|
|
from IPython.utils.zmqrelated import check_for_zmq
|
2013-06-20 22:54:02 +08:00
|
|
|
check_for_zmq('2.1.11', 'IPython.html')
|
2013-03-29 01:20:44 +08:00
|
|
|
|
2012-12-07 05:35:28 +08:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2011-03-18 12:09:04 +08:00
|
|
|
|
2011-03-24 01:00:52 +08:00
|
|
|
# Install the pyzmq ioloop. This has to be done before anything else from
|
|
|
|
# tornado is imported.
|
|
|
|
from zmq.eventloop import ioloop
|
2012-04-24 07:59:00 +08:00
|
|
|
ioloop.install()
|
2011-03-24 01:00:52 +08:00
|
|
|
|
2013-10-24 03:09:42 +08:00
|
|
|
# check for tornado 3.1.0
|
|
|
|
msg = "The IPython Notebook requires tornado >= 3.1.0"
|
2013-03-29 01:20:44 +08:00
|
|
|
try:
|
|
|
|
import tornado
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError(msg)
|
|
|
|
try:
|
|
|
|
version_info = tornado.version_info
|
|
|
|
except AttributeError:
|
|
|
|
raise ImportError(msg + ", but you have < 1.1.0")
|
2013-10-24 03:09:42 +08:00
|
|
|
if version_info < (3,1,0):
|
2013-03-29 01:20:44 +08:00
|
|
|
raise ImportError(msg + ", but you have %s" % tornado.version)
|
|
|
|
|
2011-03-24 01:00:52 +08:00
|
|
|
from tornado import httpserver
|
|
|
|
from tornado import web
|
2014-04-19 03:50:59 +08:00
|
|
|
from tornado.log import LogFormatter
|
2011-03-24 01:00:52 +08:00
|
|
|
|
2013-06-20 22:54:02 +08:00
|
|
|
from IPython.html import DEFAULT_STATIC_FILES_PATH
|
2013-12-23 02:15:38 +08:00
|
|
|
from .base.handlers import Template404
|
2014-01-22 02:43:28 +08:00
|
|
|
from .log import log_request
|
2013-05-17 07:08:20 +08:00
|
|
|
from .services.kernels.kernelmanager import MappingKernelManager
|
2014-05-31 04:01:21 +08:00
|
|
|
from .services.contents.manager import ContentsManager
|
|
|
|
from .services.contents.filemanager import FileContentsManager
|
2013-05-17 07:08:20 +08:00
|
|
|
from .services.clusters.clustermanager import ClusterManager
|
2013-07-04 01:31:49 +08:00
|
|
|
from .services.sessions.sessionmanager import SessionManager
|
2013-05-03 03:19:21 +08:00
|
|
|
|
2013-05-16 03:55:40 +08:00
|
|
|
from .base.handlers import AuthenticatedFileHandler, FileFindHandler
|
2011-03-18 12:09:04 +08:00
|
|
|
|
2014-03-29 02:51:21 +08:00
|
|
|
from IPython.config import Config
|
2011-12-18 10:12:41 +08:00
|
|
|
from IPython.config.application import catch_config_error, boolean_flag
|
2014-04-18 03:25:07 +08:00
|
|
|
from IPython.core.application import (
|
|
|
|
BaseIPythonApplication, base_flags, base_aliases,
|
2011-07-13 05:32:03 +08:00
|
|
|
)
|
2014-04-18 03:25:07 +08:00
|
|
|
from IPython.core.profiledir import ProfileDir
|
|
|
|
from IPython.kernel import KernelManager
|
2014-05-09 07:18:23 +08:00
|
|
|
from IPython.kernel.kernelspec import KernelSpecManager
|
2014-04-18 03:25:07 +08:00
|
|
|
from IPython.kernel.zmq.session import default_secure, Session
|
2014-03-11 06:21:11 +08:00
|
|
|
from IPython.nbformat.sign import NotebookNotary
|
2012-06-27 07:15:42 +08:00
|
|
|
from IPython.utils.importstring import import_item
|
2013-05-04 06:44:05 +08:00
|
|
|
from IPython.utils import submodule
|
2014-07-09 13:03:16 +08:00
|
|
|
from IPython.utils.process import check_pid
|
2012-06-27 07:15:42 +08:00
|
|
|
from IPython.utils.traitlets import (
|
2014-05-09 07:18:23 +08:00
|
|
|
Dict, Unicode, Integer, List, Bool, Bytes, Instance,
|
2014-02-13 15:03:43 +08:00
|
|
|
DottedObjectName, TraitError,
|
2012-06-27 07:15:42 +08:00
|
|
|
)
|
2012-02-03 06:41:20 +08:00
|
|
|
from IPython.utils import py3compat
|
2013-09-26 07:24:40 +08:00
|
|
|
from IPython.utils.path import filefind, get_ipython_dir
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2013-05-03 03:19:21 +08:00
|
|
|
from .utils import url_path_join
|
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Module globals
|
|
|
|
#-----------------------------------------------------------------------------
|
2011-03-18 12:09:04 +08:00
|
|
|
|
2011-08-11 00:28:47 +08:00
|
|
|
_examples = """
|
|
|
|
ipython notebook # start the notebook
|
|
|
|
ipython notebook --profile=sympy # use the sympy profile
|
|
|
|
ipython notebook --certfile=mycert.pem # use SSL/TLS certificate
|
|
|
|
"""
|
|
|
|
|
2012-01-27 01:41:41 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Helper functions
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-01 06:06:33 +08:00
|
|
|
def random_ports(port, n):
|
|
|
|
"""Generate a list of n random ports near the given port.
|
|
|
|
|
|
|
|
The first 5 ports will be sequential, and the remaining n-5 will be
|
|
|
|
randomly selected in the range [port-2*n, port+2*n].
|
|
|
|
"""
|
|
|
|
for i in range(min(5, n)):
|
|
|
|
yield port + i
|
|
|
|
for i in range(n-5):
|
2013-10-01 02:14:34 +08:00
|
|
|
yield max(1, port + random.randint(-2*n, 2*n))
|
2012-06-01 06:06:33 +08:00
|
|
|
|
2013-05-16 01:55:43 +08:00
|
|
|
def load_handlers(name):
|
|
|
|
"""Load the (URL pattern, handler) tuples for each component."""
|
2013-06-20 22:54:02 +08:00
|
|
|
name = 'IPython.html.' + name
|
2013-05-16 01:55:43 +08:00
|
|
|
mod = __import__(name, fromlist=['default_handlers'])
|
|
|
|
return mod.default_handlers
|
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# The Tornado web application
|
|
|
|
#-----------------------------------------------------------------------------
|
2011-03-26 00:12:01 +08:00
|
|
|
|
2011-07-02 07:05:26 +08:00
|
|
|
class NotebookWebApplication(web.Application):
|
2011-03-24 01:00:52 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def __init__(self, ipython_app, kernel_manager, contents_manager,
|
2014-05-09 07:18:23 +08:00
|
|
|
cluster_manager, session_manager, kernel_spec_manager, log,
|
2014-09-05 08:19:31 +08:00
|
|
|
base_url, default_url, settings_overrides, jinja_env_options):
|
2013-05-16 01:55:43 +08:00
|
|
|
|
2013-05-16 05:51:20 +08:00
|
|
|
settings = self.init_settings(
|
2014-05-31 04:01:21 +08:00
|
|
|
ipython_app, kernel_manager, contents_manager, cluster_manager,
|
2014-09-05 08:19:31 +08:00
|
|
|
session_manager, kernel_spec_manager, log, base_url, default_url,
|
2014-05-09 07:18:23 +08:00
|
|
|
settings_overrides, jinja_env_options)
|
2013-05-16 05:51:20 +08:00
|
|
|
handlers = self.init_handlers(settings)
|
|
|
|
|
|
|
|
super(NotebookWebApplication, self).__init__(handlers, **settings)
|
2011-12-10 23:16:34 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
def init_settings(self, ipython_app, kernel_manager, contents_manager,
|
2014-05-09 07:18:23 +08:00
|
|
|
cluster_manager, session_manager, kernel_spec_manager,
|
2014-09-05 08:19:31 +08:00
|
|
|
log, base_url, default_url, settings_overrides,
|
2014-05-09 07:18:23 +08:00
|
|
|
jinja_env_options=None):
|
2012-02-03 06:41:20 +08:00
|
|
|
# Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and
|
2014-02-06 08:16:32 +08:00
|
|
|
# base_url will always be unicode, which will in turn
|
2012-02-03 06:41:20 +08:00
|
|
|
# make the patterns unicode, and ultimately result in unicode
|
|
|
|
# keys in kwargs to handler._execute(**kwargs) in tornado.
|
2014-02-06 08:16:32 +08:00
|
|
|
# This enforces that base_url be ascii in that situation.
|
2012-02-03 06:41:20 +08:00
|
|
|
#
|
|
|
|
# Note that the URLs these patterns check against are escaped,
|
|
|
|
# and thus guaranteed to be ASCII: 'héllo' is really 'h%C3%A9llo'.
|
2014-02-06 08:16:32 +08:00
|
|
|
base_url = py3compat.unicode_to_str(base_url, 'ascii')
|
2013-08-16 07:28:19 +08:00
|
|
|
template_path = settings_overrides.get("template_path", os.path.join(os.path.dirname(__file__), "templates"))
|
2014-02-23 02:12:23 +08:00
|
|
|
jenv_opt = jinja_env_options if jinja_env_options else {}
|
|
|
|
env = Environment(loader=FileSystemLoader(template_path),**jenv_opt )
|
2012-07-01 18:04:10 +08:00
|
|
|
settings = dict(
|
2013-03-27 03:05:21 +08:00
|
|
|
# basics
|
2014-01-22 02:43:28 +08:00
|
|
|
log_function=log_request,
|
2014-02-06 08:16:32 +08:00
|
|
|
base_url=base_url,
|
2014-09-05 08:19:31 +08:00
|
|
|
default_url=default_url,
|
2013-03-27 03:05:21 +08:00
|
|
|
template_path=template_path,
|
2012-07-21 01:14:09 +08:00
|
|
|
static_path=ipython_app.static_file_path,
|
2012-07-21 01:01:49 +08:00
|
|
|
static_handler_class = FileFindHandler,
|
2014-02-06 08:16:32 +08:00
|
|
|
static_url_prefix = url_path_join(base_url,'/static/'),
|
2013-03-27 03:05:21 +08:00
|
|
|
|
|
|
|
# authentication
|
2013-05-29 04:34:08 +08:00
|
|
|
cookie_secret=ipython_app.cookie_secret,
|
2014-02-06 08:16:32 +08:00
|
|
|
login_url=url_path_join(base_url,'/login'),
|
2013-03-27 03:05:21 +08:00
|
|
|
password=ipython_app.password,
|
|
|
|
|
|
|
|
# managers
|
|
|
|
kernel_manager=kernel_manager,
|
2014-05-31 04:01:21 +08:00
|
|
|
contents_manager=contents_manager,
|
2013-03-27 03:05:21 +08:00
|
|
|
cluster_manager=cluster_manager,
|
2013-07-04 01:31:49 +08:00
|
|
|
session_manager=session_manager,
|
2014-05-09 07:18:23 +08:00
|
|
|
kernel_spec_manager=kernel_spec_manager,
|
2013-07-04 01:31:49 +08:00
|
|
|
|
2013-03-27 03:05:21 +08:00
|
|
|
# IPython stuff
|
2013-09-27 06:25:30 +08:00
|
|
|
nbextensions_path = ipython_app.nbextensions_path,
|
2014-06-24 06:27:59 +08:00
|
|
|
websocket_url=ipython_app.websocket_url,
|
2013-04-28 00:29:17 +08:00
|
|
|
mathjax_url=ipython_app.mathjax_url,
|
2013-03-27 03:05:21 +08:00
|
|
|
config=ipython_app.config,
|
2014-02-23 02:12:23 +08:00
|
|
|
jinja2_env=env,
|
2012-07-01 18:04:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# allow custom overrides for the tornado web app.
|
|
|
|
settings.update(settings_overrides)
|
2013-05-16 05:51:20 +08:00
|
|
|
return settings
|
2012-07-01 18:04:10 +08:00
|
|
|
|
2013-05-16 05:51:20 +08:00
|
|
|
def init_handlers(self, settings):
|
|
|
|
# Load the (URL pattern, handler) tuples for each component.
|
|
|
|
handlers = []
|
|
|
|
handlers.extend(load_handlers('base.handlers'))
|
|
|
|
handlers.extend(load_handlers('tree.handlers'))
|
|
|
|
handlers.extend(load_handlers('auth.login'))
|
|
|
|
handlers.extend(load_handlers('auth.logout'))
|
2013-05-17 06:52:27 +08:00
|
|
|
handlers.extend(load_handlers('notebook.handlers'))
|
2013-12-06 09:38:30 +08:00
|
|
|
handlers.extend(load_handlers('nbconvert.handlers'))
|
2014-05-11 03:22:20 +08:00
|
|
|
handlers.extend(load_handlers('kernelspecs.handlers'))
|
2013-05-17 06:52:27 +08:00
|
|
|
handlers.extend(load_handlers('services.kernels.handlers'))
|
2014-05-31 04:01:21 +08:00
|
|
|
handlers.extend(load_handlers('services.contents.handlers'))
|
2013-05-17 06:52:27 +08:00
|
|
|
handlers.extend(load_handlers('services.clusters.handlers'))
|
2013-07-04 01:31:49 +08:00
|
|
|
handlers.extend(load_handlers('services.sessions.handlers'))
|
2013-12-12 07:28:22 +08:00
|
|
|
handlers.extend(load_handlers('services.nbconvert.handlers'))
|
2014-05-09 07:18:23 +08:00
|
|
|
handlers.extend(load_handlers('services.kernelspecs.handlers'))
|
2014-02-13 15:03:43 +08:00
|
|
|
# FIXME: /files/ should be handled by the Contents service when it exists
|
2014-05-31 04:01:21 +08:00
|
|
|
cm = settings['contents_manager']
|
|
|
|
if hasattr(cm, 'root_dir'):
|
|
|
|
handlers.append(
|
|
|
|
(r"/files/(.*)", AuthenticatedFileHandler, {'path' : cm.root_dir}),
|
|
|
|
)
|
|
|
|
handlers.append(
|
2013-09-27 06:25:30 +08:00
|
|
|
(r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}),
|
2014-05-31 04:01:21 +08:00
|
|
|
)
|
2014-09-05 08:19:31 +08:00
|
|
|
# set the URL that will be redirected from `/`
|
|
|
|
handlers.append(
|
|
|
|
(r'/?', web.RedirectHandler, {
|
|
|
|
'url' : url_path_join(settings['base_url'], settings['default_url']),
|
|
|
|
'permanent': False, # want 302, not 301
|
|
|
|
})
|
|
|
|
)
|
2014-02-06 08:16:32 +08:00
|
|
|
# prepend base_url onto the patterns that we match
|
2012-01-27 01:41:41 +08:00
|
|
|
new_handlers = []
|
|
|
|
for handler in handlers:
|
2014-02-06 08:16:32 +08:00
|
|
|
pattern = url_path_join(settings['base_url'], handler[0])
|
2013-03-27 03:05:21 +08:00
|
|
|
new_handler = tuple([pattern] + list(handler[1:]))
|
|
|
|
new_handlers.append(new_handler)
|
2013-12-23 02:15:38 +08:00
|
|
|
# add 404 on the end, which will catch everything that falls through
|
|
|
|
new_handlers.append((r'(.*)', Template404))
|
2013-05-16 05:51:20 +08:00
|
|
|
return new_handlers
|
2011-04-27 01:52:14 +08:00
|
|
|
|
2011-03-18 12:09:04 +08:00
|
|
|
|
2014-01-14 08:22:43 +08:00
|
|
|
class NbserverListApp(BaseIPythonApplication):
|
|
|
|
|
|
|
|
description="List currently running notebook servers in this profile."
|
|
|
|
|
|
|
|
flags = dict(
|
|
|
|
json=({'NbserverListApp': {'json': True}},
|
|
|
|
"Produce machine-readable JSON output."),
|
|
|
|
)
|
|
|
|
|
|
|
|
json = Bool(False, config=True,
|
|
|
|
help="If True, each line of output will be a JSON object with the "
|
|
|
|
"details from the server info file.")
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if not self.json:
|
|
|
|
print("Currently running servers:")
|
2014-01-14 08:25:39 +08:00
|
|
|
for serverinfo in list_running_servers(self.profile):
|
2014-01-14 08:22:43 +08:00
|
|
|
if self.json:
|
|
|
|
print(json.dumps(serverinfo))
|
|
|
|
else:
|
2014-01-14 08:25:39 +08:00
|
|
|
print(serverinfo['url'], "::", serverinfo['notebook_dir'])
|
2011-07-14 02:33:50 +08:00
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Aliases and Flags
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-18 03:25:07 +08:00
|
|
|
flags = dict(base_flags)
|
2011-10-18 05:31:26 +08:00
|
|
|
flags['no-browser']=(
|
2011-10-19 13:12:09 +08:00
|
|
|
{'NotebookApp' : {'open_browser' : False}},
|
2011-10-18 05:31:26 +08:00
|
|
|
"Don't open the notebook in a browser after startup."
|
|
|
|
)
|
2014-04-18 03:25:07 +08:00
|
|
|
flags['pylab']=(
|
|
|
|
{'NotebookApp' : {'pylab' : 'warn'}},
|
|
|
|
"DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib."
|
|
|
|
)
|
2011-12-01 12:11:37 +08:00
|
|
|
flags['no-mathjax']=(
|
|
|
|
{'NotebookApp' : {'enable_mathjax' : False}},
|
|
|
|
"""Disable MathJax
|
|
|
|
|
|
|
|
MathJax is the javascript library IPython uses to render math/LaTeX. It is
|
|
|
|
very large, so you may want to disable it if you have a slow internet
|
|
|
|
connection, or for offline use of the notebook.
|
|
|
|
|
|
|
|
When disabled, equations etc. will appear as their untransformed TeX source.
|
|
|
|
"""
|
|
|
|
)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2014-07-30 05:01:27 +08:00
|
|
|
# Add notebook manager flags
|
|
|
|
flags.update(boolean_flag('script', 'FileContentsManager.save_script',
|
|
|
|
'DEPRECATED, IGNORED',
|
|
|
|
'DEPRECATED, IGNORED'))
|
|
|
|
|
2014-04-18 03:25:07 +08:00
|
|
|
aliases = dict(base_aliases)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2011-08-11 00:28:47 +08:00
|
|
|
aliases.update({
|
2011-09-07 07:49:46 +08:00
|
|
|
'ip': 'NotebookApp.ip',
|
|
|
|
'port': 'NotebookApp.port',
|
2012-06-01 06:06:33 +08:00
|
|
|
'port-retries': 'NotebookApp.port_retries',
|
2013-01-19 13:21:17 +08:00
|
|
|
'transport': 'KernelManager.transport',
|
2011-09-07 07:49:46 +08:00
|
|
|
'keyfile': 'NotebookApp.keyfile',
|
|
|
|
'certfile': 'NotebookApp.certfile',
|
2014-02-13 15:03:43 +08:00
|
|
|
'notebook-dir': 'NotebookApp.notebook_dir',
|
2012-02-14 11:15:32 +08:00
|
|
|
'browser': 'NotebookApp.browser',
|
2014-04-18 03:25:07 +08:00
|
|
|
'pylab': 'NotebookApp.pylab',
|
2011-08-11 00:28:47 +08:00
|
|
|
})
|
2011-07-13 03:10:34 +08:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2011-09-07 07:49:46 +08:00
|
|
|
# NotebookApp
|
2011-07-13 03:10:34 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2011-09-07 07:49:46 +08:00
|
|
|
class NotebookApp(BaseIPythonApplication):
|
2011-08-11 00:28:47 +08:00
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
name = 'ipython-notebook'
|
|
|
|
|
|
|
|
description = """
|
|
|
|
The IPython HTML Notebook.
|
|
|
|
|
|
|
|
This launches a Tornado based HTML Notebook Server that serves up an
|
|
|
|
HTML5/Javascript Notebook client.
|
|
|
|
"""
|
2011-08-11 00:28:47 +08:00
|
|
|
examples = _examples
|
2014-04-18 03:25:07 +08:00
|
|
|
aliases = aliases
|
|
|
|
flags = flags
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2014-04-18 03:25:07 +08:00
|
|
|
classes = [
|
|
|
|
KernelManager, ProfileDir, Session, MappingKernelManager,
|
2014-05-31 04:01:21 +08:00
|
|
|
ContentsManager, FileContentsManager, NotebookNotary,
|
2014-04-18 03:25:07 +08:00
|
|
|
]
|
2011-07-13 03:10:34 +08:00
|
|
|
flags = Dict(flags)
|
|
|
|
aliases = Dict(aliases)
|
2014-01-14 08:22:43 +08:00
|
|
|
|
|
|
|
subcommands = dict(
|
|
|
|
list=(NbserverListApp, NbserverListApp.description.splitlines()[0]),
|
|
|
|
)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
|
|
|
kernel_argv = List(Unicode)
|
2014-04-19 03:50:59 +08:00
|
|
|
|
|
|
|
_log_formatter_cls = LogFormatter
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2013-03-27 03:05:21 +08:00
|
|
|
def _log_level_default(self):
|
|
|
|
return logging.INFO
|
2011-07-13 05:32:03 +08:00
|
|
|
|
2014-04-19 03:50:59 +08:00
|
|
|
def _log_datefmt_default(self):
|
|
|
|
"""Exclude date from default date format"""
|
|
|
|
return "%H:%M:%S"
|
|
|
|
|
2013-04-26 02:58:58 +08:00
|
|
|
def _log_format_default(self):
|
|
|
|
"""override default log format to include time"""
|
2014-04-19 03:50:59 +08:00
|
|
|
return u"%(color)s[%(levelname)1.1s %(asctime)s.%(msecs).03d %(name)s]%(end_color)s %(message)s"
|
2013-04-26 02:58:58 +08:00
|
|
|
|
2012-01-11 08:52:07 +08:00
|
|
|
# create requested profiles by default, if they don't exist:
|
|
|
|
auto_create = Bool(True)
|
|
|
|
|
2012-05-02 22:20:07 +08:00
|
|
|
# file to be opened in the notebook server
|
2014-02-25 07:41:04 +08:00
|
|
|
file_to_run = Unicode('', config=True)
|
2014-02-25 07:28:14 +08:00
|
|
|
def _file_to_run_changed(self, name, old, new):
|
|
|
|
path, base = os.path.split(new)
|
|
|
|
if path:
|
|
|
|
self.file_to_run = base
|
2014-02-25 07:41:04 +08:00
|
|
|
self.notebook_dir = path
|
2012-05-02 22:20:07 +08:00
|
|
|
|
2014-06-28 08:47:56 +08:00
|
|
|
# Network related information
|
|
|
|
|
2014-07-01 01:40:31 +08:00
|
|
|
allow_origin = Unicode('', config=True,
|
2014-06-28 08:47:56 +08:00
|
|
|
help="""Set the Access-Control-Allow-Origin header
|
|
|
|
|
|
|
|
Use '*' to allow any origin to access your server.
|
|
|
|
|
2014-07-01 01:40:31 +08:00
|
|
|
Takes precedence over allow_origin_pat.
|
2014-06-28 08:47:56 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2014-07-01 01:40:31 +08:00
|
|
|
allow_origin_pat = Unicode('', config=True,
|
2014-06-28 08:47:56 +08:00
|
|
|
help="""Use a regular expression for the Access-Control-Allow-Origin header
|
|
|
|
|
|
|
|
Requests from an origin matching the expression will get replies with:
|
|
|
|
|
|
|
|
Access-Control-Allow-Origin: origin
|
|
|
|
|
|
|
|
where `origin` is the origin of the request.
|
|
|
|
|
2014-07-01 01:40:31 +08:00
|
|
|
Ignored if allow_origin is set.
|
2014-06-28 08:47:56 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2014-07-01 01:40:31 +08:00
|
|
|
allow_credentials = Bool(False, config=True,
|
2014-06-28 08:47:56 +08:00
|
|
|
help="Set the Access-Control-Allow-Credentials: true header"
|
|
|
|
)
|
|
|
|
|
2014-09-05 08:19:31 +08:00
|
|
|
default_url = Unicode('/tree', config=True,
|
|
|
|
help="The default URL to redirect to from `/`"
|
|
|
|
)
|
|
|
|
|
2014-04-18 03:01:55 +08:00
|
|
|
ip = Unicode('localhost', config=True,
|
2011-07-13 03:10:34 +08:00
|
|
|
help="The IP address the notebook server will listen on."
|
|
|
|
)
|
|
|
|
|
2011-08-11 00:28:47 +08:00
|
|
|
def _ip_changed(self, name, old, new):
|
|
|
|
if new == u'*': self.ip = u''
|
|
|
|
|
2011-11-02 06:15:33 +08:00
|
|
|
port = Integer(8888, config=True,
|
2011-07-13 03:10:34 +08:00
|
|
|
help="The port the notebook server will listen on."
|
|
|
|
)
|
2012-06-01 06:06:33 +08:00
|
|
|
port_retries = Integer(50, config=True,
|
|
|
|
help="The number of additional ports to try if the specified port is not available."
|
|
|
|
)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2011-08-11 00:28:47 +08:00
|
|
|
certfile = Unicode(u'', config=True,
|
|
|
|
help="""The full path to an SSL/TLS certificate file."""
|
|
|
|
)
|
|
|
|
|
|
|
|
keyfile = Unicode(u'', config=True,
|
|
|
|
help="""The full path to a private key file for usage with SSL/TLS."""
|
|
|
|
)
|
2013-05-29 04:34:08 +08:00
|
|
|
|
2014-07-04 00:18:11 +08:00
|
|
|
cookie_secret_file = Unicode(config=True,
|
|
|
|
help="""The file where the cookie secret is stored."""
|
|
|
|
)
|
|
|
|
def _cookie_secret_file_default(self):
|
|
|
|
if self.profile_dir is None:
|
|
|
|
return ''
|
|
|
|
return os.path.join(self.profile_dir.security_dir, 'notebook_cookie_secret')
|
|
|
|
|
2013-05-29 04:34:08 +08:00
|
|
|
cookie_secret = Bytes(b'', config=True,
|
|
|
|
help="""The random bytes used to secure cookies.
|
|
|
|
By default this is a new random number every time you start the Notebook.
|
|
|
|
Set it to a value in a config file to enable logins to persist across server sessions.
|
2013-06-05 05:37:19 +08:00
|
|
|
|
|
|
|
Note: Cookie secrets should be kept private, do not share config files with
|
|
|
|
cookie_secret stored in plaintext (you can read the value from a file).
|
2013-05-29 04:34:08 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
def _cookie_secret_default(self):
|
2014-07-04 00:18:11 +08:00
|
|
|
if os.path.exists(self.cookie_secret_file):
|
|
|
|
with io.open(self.cookie_secret_file, 'rb') as f:
|
|
|
|
return f.read()
|
|
|
|
else:
|
|
|
|
secret = base64.encodestring(os.urandom(1024))
|
|
|
|
self._write_cookie_secret_file(secret)
|
|
|
|
return secret
|
|
|
|
|
|
|
|
def _write_cookie_secret_file(self, secret):
|
|
|
|
"""write my secret to my secret_file"""
|
|
|
|
self.log.info("Writing notebook server cookie secret to %s", self.cookie_secret_file)
|
|
|
|
with io.open(self.cookie_secret_file, 'wb') as f:
|
|
|
|
f.write(secret)
|
|
|
|
try:
|
|
|
|
os.chmod(self.cookie_secret_file, 0o600)
|
|
|
|
except OSError:
|
|
|
|
self.log.warn(
|
|
|
|
"Could not set permissions on %s",
|
|
|
|
self.cookie_secret_file
|
|
|
|
)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2011-08-31 02:24:52 +08:00
|
|
|
password = Unicode(u'', config=True,
|
2011-11-19 02:53:09 +08:00
|
|
|
help="""Hashed password to use for web authentication.
|
|
|
|
|
2011-11-19 12:20:57 +08:00
|
|
|
To generate, type in a python/IPython shell:
|
2011-11-19 02:53:09 +08:00
|
|
|
|
2011-11-19 12:20:57 +08:00
|
|
|
from IPython.lib import passwd; passwd()
|
2011-11-19 02:53:09 +08:00
|
|
|
|
|
|
|
The string should be of the form type:salt:hashed-password.
|
|
|
|
"""
|
2011-08-27 13:09:57 +08:00
|
|
|
)
|
2012-02-14 11:15:32 +08:00
|
|
|
|
2011-10-18 05:31:26 +08:00
|
|
|
open_browser = Bool(True, config=True,
|
2012-02-14 10:11:54 +08:00
|
|
|
help="""Whether to open in a browser after starting.
|
|
|
|
The specific browser used is platform dependent and
|
|
|
|
determined by the python standard library `webbrowser`
|
2012-02-14 11:15:32 +08:00
|
|
|
module, unless it is overridden using the --browser
|
|
|
|
(NotebookApp.browser) configuration option.
|
2012-02-14 10:11:54 +08:00
|
|
|
""")
|
2012-02-14 11:15:32 +08:00
|
|
|
|
|
|
|
browser = Unicode(u'', config=True,
|
2012-02-15 06:26:31 +08:00
|
|
|
help="""Specify what command to use to invoke a web
|
|
|
|
browser when opening the notebook. If not specified, the
|
|
|
|
default browser will be determined by the `webbrowser`
|
|
|
|
standard library module, which allows setting of the
|
|
|
|
BROWSER environment variable to override it.
|
2012-02-14 11:15:32 +08:00
|
|
|
""")
|
2011-10-25 12:33:05 +08:00
|
|
|
|
2011-12-11 01:49:41 +08:00
|
|
|
webapp_settings = Dict(config=True,
|
|
|
|
help="Supply overrides for the tornado.web.Application that the "
|
|
|
|
"IPython notebook uses.")
|
2014-02-23 02:12:23 +08:00
|
|
|
|
|
|
|
jinja_environment_options = Dict(config=True,
|
|
|
|
help="Supply extra arguments that will be passed to Jinja environment.")
|
|
|
|
|
2011-12-11 01:49:41 +08:00
|
|
|
|
2011-12-01 12:11:37 +08:00
|
|
|
enable_mathjax = Bool(True, config=True,
|
|
|
|
help="""Whether to enable MathJax for typesetting math/TeX
|
|
|
|
|
|
|
|
MathJax is the javascript library IPython uses to render math/LaTeX. It is
|
|
|
|
very large, so you may want to disable it if you have a slow internet
|
|
|
|
connection, or for offline use of the notebook.
|
|
|
|
|
|
|
|
When disabled, equations etc. will appear as their untransformed TeX source.
|
|
|
|
"""
|
|
|
|
)
|
2011-12-02 07:59:02 +08:00
|
|
|
def _enable_mathjax_changed(self, name, old, new):
|
|
|
|
"""set mathjax url to empty if mathjax is disabled"""
|
|
|
|
if not new:
|
|
|
|
self.mathjax_url = u''
|
2012-01-28 20:24:52 +08:00
|
|
|
|
2014-02-06 08:16:32 +08:00
|
|
|
base_url = Unicode('/', config=True,
|
2012-12-27 05:51:22 +08:00
|
|
|
help='''The base URL for the notebook server.
|
|
|
|
|
|
|
|
Leading and trailing slashes can be omitted,
|
|
|
|
and will automatically be added.
|
|
|
|
''')
|
2014-02-06 08:16:32 +08:00
|
|
|
def _base_url_changed(self, name, old, new):
|
2012-12-27 05:51:22 +08:00
|
|
|
if not new.startswith('/'):
|
2014-02-06 08:16:32 +08:00
|
|
|
self.base_url = '/'+new
|
2012-12-27 05:51:22 +08:00
|
|
|
elif not new.endswith('/'):
|
2014-02-06 08:16:32 +08:00
|
|
|
self.base_url = new+'/'
|
|
|
|
|
|
|
|
base_project_url = Unicode('/', config=True, help="""DEPRECATED use base_url""")
|
|
|
|
def _base_project_url_changed(self, name, old, new):
|
|
|
|
self.log.warn("base_project_url is deprecated, use base_url")
|
|
|
|
self.base_url = new
|
2012-12-27 05:51:22 +08:00
|
|
|
|
2012-07-21 01:01:49 +08:00
|
|
|
extra_static_paths = List(Unicode, config=True,
|
|
|
|
help="""Extra paths to search for serving static files.
|
|
|
|
|
|
|
|
This allows adding javascript/css to be available from the notebook server machine,
|
|
|
|
or overriding individual files in the IPython"""
|
|
|
|
)
|
|
|
|
def _extra_static_paths_default(self):
|
|
|
|
return [os.path.join(self.profile_dir.location, 'static')]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def static_file_path(self):
|
|
|
|
"""return extra paths + the default location"""
|
2013-02-15 00:52:54 +08:00
|
|
|
return self.extra_static_paths + [DEFAULT_STATIC_FILES_PATH]
|
2013-09-26 07:24:40 +08:00
|
|
|
|
2013-09-27 06:25:30 +08:00
|
|
|
nbextensions_path = List(Unicode, config=True,
|
|
|
|
help="""paths for Javascript extensions. By default, this is just IPYTHONDIR/nbextensions"""
|
2013-09-26 07:24:40 +08:00
|
|
|
)
|
2013-09-27 06:25:30 +08:00
|
|
|
def _nbextensions_path_default(self):
|
|
|
|
return [os.path.join(get_ipython_dir(), 'nbextensions')]
|
2012-01-27 01:41:41 +08:00
|
|
|
|
2014-06-24 06:27:59 +08:00
|
|
|
websocket_url = Unicode("", config=True,
|
|
|
|
help="""The base URL for websockets,
|
|
|
|
if it differs from the HTTP server (hint: it almost certainly doesn't).
|
|
|
|
|
|
|
|
Should be in the form of an HTTP origin: ws[s]://hostname[:port]
|
|
|
|
"""
|
|
|
|
)
|
2011-12-02 07:59:02 +08:00
|
|
|
mathjax_url = Unicode("", config=True,
|
|
|
|
help="""The url for MathJax.js."""
|
|
|
|
)
|
|
|
|
def _mathjax_url_default(self):
|
|
|
|
if not self.enable_mathjax:
|
|
|
|
return u''
|
2012-01-27 00:15:41 +08:00
|
|
|
static_url_prefix = self.webapp_settings.get("static_url_prefix",
|
2014-02-06 08:16:32 +08:00
|
|
|
url_path_join(self.base_url, "static")
|
2013-07-04 08:34:00 +08:00
|
|
|
)
|
2013-09-27 06:24:47 +08:00
|
|
|
|
2013-09-27 06:25:30 +08:00
|
|
|
# try local mathjax, either in nbextensions/mathjax or static/mathjax
|
2013-09-27 06:24:47 +08:00
|
|
|
for (url_prefix, search_path) in [
|
2014-02-06 08:16:32 +08:00
|
|
|
(url_path_join(self.base_url, "nbextensions"), self.nbextensions_path),
|
2013-09-27 06:24:47 +08:00
|
|
|
(static_url_prefix, self.static_file_path),
|
|
|
|
]:
|
|
|
|
self.log.debug("searching for local mathjax in %s", search_path)
|
|
|
|
try:
|
|
|
|
mathjax = filefind(os.path.join('mathjax', 'MathJax.js'), search_path)
|
|
|
|
except IOError:
|
|
|
|
continue
|
2012-02-29 03:05:09 +08:00
|
|
|
else:
|
2013-09-27 06:24:47 +08:00
|
|
|
url = url_path_join(url_prefix, u"mathjax/MathJax.js")
|
|
|
|
self.log.info("Serving local MathJax from %s at %s", mathjax, url)
|
|
|
|
return url
|
|
|
|
|
|
|
|
# no local mathjax, serve from CDN
|
2014-08-01 03:03:44 +08:00
|
|
|
url = u"https://cdn.mathjax.org/mathjax/latest/MathJax.js"
|
2013-09-27 06:24:47 +08:00
|
|
|
self.log.info("Using MathJax from CDN: %s", url)
|
|
|
|
return url
|
2011-12-02 07:59:02 +08:00
|
|
|
|
|
|
|
def _mathjax_url_changed(self, name, old, new):
|
|
|
|
if new and not self.enable_mathjax:
|
|
|
|
# enable_mathjax=False overrides mathjax_url
|
|
|
|
self.mathjax_url = u''
|
|
|
|
else:
|
|
|
|
self.log.info("Using MathJax: %s", new)
|
2011-08-27 13:09:57 +08:00
|
|
|
|
2014-05-31 04:01:21 +08:00
|
|
|
contents_manager_class = DottedObjectName('IPython.html.services.contents.filemanager.FileContentsManager',
|
2012-06-27 07:15:42 +08:00
|
|
|
config=True,
|
2014-04-23 05:50:51 +08:00
|
|
|
help='The notebook manager class to use.'
|
|
|
|
)
|
|
|
|
kernel_manager_class = DottedObjectName('IPython.html.services.kernels.kernelmanager.MappingKernelManager',
|
|
|
|
config=True,
|
|
|
|
help='The kernel manager class to use.'
|
|
|
|
)
|
|
|
|
session_manager_class = DottedObjectName('IPython.html.services.sessions.sessionmanager.SessionManager',
|
|
|
|
config=True,
|
|
|
|
help='The session manager class to use.'
|
|
|
|
)
|
|
|
|
cluster_manager_class = DottedObjectName('IPython.html.services.clusters.clustermanager.ClusterManager',
|
|
|
|
config=True,
|
|
|
|
help='The cluster manager class to use.'
|
|
|
|
)
|
2012-06-27 07:15:42 +08:00
|
|
|
|
2014-05-09 07:18:23 +08:00
|
|
|
kernel_spec_manager = Instance(KernelSpecManager)
|
|
|
|
|
|
|
|
def _kernel_spec_manager_default(self):
|
|
|
|
return KernelSpecManager(ipython_dir=self.ipython_dir)
|
|
|
|
|
2013-04-08 23:01:20 +08:00
|
|
|
trust_xheaders = Bool(False, config=True,
|
|
|
|
help=("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers"
|
2014-01-13 03:02:52 +08:00
|
|
|
"sent by the upstream reverse proxy. Necessary if the proxy handles SSL")
|
2013-04-08 23:01:20 +08:00
|
|
|
)
|
2013-08-23 15:17:35 +08:00
|
|
|
|
2014-01-09 05:02:36 +08:00
|
|
|
info_file = Unicode()
|
|
|
|
|
|
|
|
def _info_file_default(self):
|
|
|
|
info_file = "nbserver-%s.json"%os.getpid()
|
|
|
|
return os.path.join(self.profile_dir.security_dir, info_file)
|
2014-02-13 15:03:43 +08:00
|
|
|
|
|
|
|
notebook_dir = Unicode(py3compat.getcwd(), config=True,
|
|
|
|
help="The directory to use for notebooks and kernels."
|
|
|
|
)
|
2014-04-18 03:25:07 +08:00
|
|
|
|
|
|
|
pylab = Unicode('disabled', config=True,
|
|
|
|
help="""
|
|
|
|
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
def _pylab_changed(self, name, old, new):
|
2014-04-20 06:43:17 +08:00
|
|
|
"""when --pylab is specified, display a warning and exit"""
|
2014-04-18 03:25:07 +08:00
|
|
|
if new != 'warn':
|
|
|
|
backend = ' %s' % new
|
|
|
|
else:
|
|
|
|
backend = ''
|
2014-04-20 06:43:17 +08:00
|
|
|
self.log.error("Support for specifying --pylab on the command line has been removed.")
|
|
|
|
self.log.error(
|
2014-04-18 03:25:07 +08:00
|
|
|
"Please use `%pylab{0}` or `%matplotlib{0}` in the notebook itself.".format(backend)
|
|
|
|
)
|
2014-04-20 06:43:17 +08:00
|
|
|
self.exit(1)
|
2014-02-13 15:03:43 +08:00
|
|
|
|
|
|
|
def _notebook_dir_changed(self, name, old, new):
|
|
|
|
"""Do a bit of validation of the notebook dir."""
|
|
|
|
if not os.path.isabs(new):
|
|
|
|
# If we receive a non-absolute path, make it absolute.
|
|
|
|
self.notebook_dir = os.path.abspath(new)
|
|
|
|
return
|
2014-02-22 04:27:11 +08:00
|
|
|
if not os.path.isdir(new):
|
|
|
|
raise TraitError("No such notebook dir: %r" % new)
|
2014-02-13 15:03:43 +08:00
|
|
|
|
2014-02-13 15:34:04 +08:00
|
|
|
# setting App.notebook_dir implies setting notebook and kernel dirs as well
|
2014-05-31 04:01:21 +08:00
|
|
|
self.config.FileContentsManager.root_dir = new
|
2014-02-13 15:34:04 +08:00
|
|
|
self.config.MappingKernelManager.root_dir = new
|
|
|
|
|
2014-01-09 05:02:36 +08:00
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
def parse_command_line(self, argv=None):
|
2011-09-07 07:49:46 +08:00
|
|
|
super(NotebookApp, self).parse_command_line(argv)
|
2013-08-23 15:17:35 +08:00
|
|
|
|
2012-05-02 22:20:07 +08:00
|
|
|
if self.extra_args:
|
2013-10-22 11:31:10 +08:00
|
|
|
arg0 = self.extra_args[0]
|
|
|
|
f = os.path.abspath(arg0)
|
|
|
|
self.argv.remove(arg0)
|
2013-10-11 16:41:51 +08:00
|
|
|
if not os.path.exists(f):
|
|
|
|
self.log.critical("No such file or directory: %s", f)
|
|
|
|
self.exit(1)
|
2014-02-25 07:41:04 +08:00
|
|
|
|
|
|
|
# Use config here, to ensure that it takes higher priority than
|
|
|
|
# anything that comes from the profile.
|
2014-03-29 02:51:21 +08:00
|
|
|
c = Config()
|
2012-06-14 04:48:56 +08:00
|
|
|
if os.path.isdir(f):
|
2014-03-29 02:51:21 +08:00
|
|
|
c.NotebookApp.notebook_dir = f
|
2013-08-06 02:15:22 +08:00
|
|
|
elif os.path.isfile(f):
|
2014-03-29 02:51:21 +08:00
|
|
|
c.NotebookApp.file_to_run = f
|
|
|
|
self.update_config(c)
|
2012-05-02 22:20:07 +08:00
|
|
|
|
2013-08-23 15:17:35 +08:00
|
|
|
def init_kernel_argv(self):
|
|
|
|
"""construct the kernel arguments"""
|
|
|
|
# Kernel should get *absolute* path to profile directory
|
2014-02-01 12:54:29 +08:00
|
|
|
self.kernel_argv = ["--profile-dir", self.profile_dir.location]
|
2013-08-23 15:17:35 +08:00
|
|
|
|
2011-08-07 12:33:55 +08:00
|
|
|
def init_configurables(self):
|
2011-10-11 05:47:16 +08:00
|
|
|
# force Session default to be secure
|
|
|
|
default_secure(self.config)
|
2014-04-23 05:50:51 +08:00
|
|
|
kls = import_item(self.kernel_manager_class)
|
|
|
|
self.kernel_manager = kls(
|
2013-06-14 23:02:56 +08:00
|
|
|
parent=self, log=self.log, kernel_argv=self.kernel_argv,
|
2011-10-09 15:50:27 +08:00
|
|
|
connection_dir = self.profile_dir.security_dir,
|
2011-08-07 12:33:55 +08:00
|
|
|
)
|
2014-05-31 04:01:21 +08:00
|
|
|
kls = import_item(self.contents_manager_class)
|
|
|
|
self.contents_manager = kls(parent=self, log=self.log)
|
2014-04-23 05:50:51 +08:00
|
|
|
kls = import_item(self.session_manager_class)
|
2014-06-20 07:09:31 +08:00
|
|
|
self.session_manager = kls(parent=self, log=self.log,
|
|
|
|
kernel_manager=self.kernel_manager,
|
2014-05-31 04:01:21 +08:00
|
|
|
contents_manager=self.contents_manager)
|
2014-04-23 05:50:51 +08:00
|
|
|
kls = import_item(self.cluster_manager_class)
|
|
|
|
self.cluster_manager = kls(parent=self, log=self.log)
|
2012-02-16 06:44:23 +08:00
|
|
|
self.cluster_manager.update_profiles()
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2011-07-13 05:32:03 +08:00
|
|
|
def init_logging(self):
|
|
|
|
# This prevents double log messages because tornado use a root logger that
|
|
|
|
# self.log is a child of. The logging module dipatches log messages to a log
|
|
|
|
# and all of its ancenstors until propagate is set to False.
|
|
|
|
self.log.propagate = False
|
2013-04-26 02:58:58 +08:00
|
|
|
|
|
|
|
# hook up tornado 3's loggers to our app handlers
|
2014-04-19 03:50:59 +08:00
|
|
|
logger = logging.getLogger('tornado')
|
|
|
|
logger.propagate = True
|
|
|
|
logger.parent = self.log
|
|
|
|
logger.setLevel(self.log.level)
|
2011-12-21 04:26:06 +08:00
|
|
|
|
|
|
|
def init_webapp(self):
|
|
|
|
"""initialize tornado webapp and httpserver"""
|
2014-07-01 01:40:31 +08:00
|
|
|
self.webapp_settings['allow_origin'] = self.allow_origin
|
2014-07-10 05:25:36 +08:00
|
|
|
if self.allow_origin_pat:
|
|
|
|
self.webapp_settings['allow_origin_pat'] = re.compile(self.allow_origin_pat)
|
2014-07-01 01:40:31 +08:00
|
|
|
self.webapp_settings['allow_credentials'] = self.allow_credentials
|
2014-06-28 08:47:56 +08:00
|
|
|
|
2011-07-14 07:41:57 +08:00
|
|
|
self.web_app = NotebookWebApplication(
|
2014-05-31 04:01:21 +08:00
|
|
|
self, self.kernel_manager, self.contents_manager,
|
2014-05-09 07:18:23 +08:00
|
|
|
self.cluster_manager, self.session_manager, self.kernel_spec_manager,
|
2014-09-05 08:19:31 +08:00
|
|
|
self.log, self.base_url, self.default_url, self.webapp_settings,
|
2014-02-23 02:12:23 +08:00
|
|
|
self.jinja_environment_options
|
2011-07-14 07:41:57 +08:00
|
|
|
)
|
2011-08-11 00:28:47 +08:00
|
|
|
if self.certfile:
|
|
|
|
ssl_options = dict(certfile=self.certfile)
|
|
|
|
if self.keyfile:
|
|
|
|
ssl_options['keyfile'] = self.keyfile
|
|
|
|
else:
|
|
|
|
ssl_options = None
|
2011-08-31 02:24:52 +08:00
|
|
|
self.web_app.password = self.password
|
2013-04-08 23:01:20 +08:00
|
|
|
self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options,
|
|
|
|
xheaders=self.trust_xheaders)
|
2012-11-14 00:49:34 +08:00
|
|
|
if not self.ip:
|
2012-11-14 02:25:50 +08:00
|
|
|
warning = "WARNING: The notebook server is listening on all IP addresses"
|
2012-11-14 00:49:34 +08:00
|
|
|
if ssl_options is None:
|
2013-05-04 20:04:16 +08:00
|
|
|
self.log.critical(warning + " and not using encryption. This "
|
2012-11-14 07:06:15 +08:00
|
|
|
"is not recommended.")
|
2013-07-24 04:03:03 +08:00
|
|
|
if not self.password:
|
2013-05-04 20:04:16 +08:00
|
|
|
self.log.critical(warning + " and not using authentication. "
|
2012-11-14 07:06:15 +08:00
|
|
|
"This is highly insecure and not recommended.")
|
2012-06-01 06:06:33 +08:00
|
|
|
success = None
|
|
|
|
for port in random_ports(self.port, self.port_retries+1):
|
2011-08-15 12:41:01 +08:00
|
|
|
try:
|
|
|
|
self.http_server.listen(port, self.ip)
|
2012-07-01 00:34:56 +08:00
|
|
|
except socket.error as e:
|
2013-10-01 02:20:42 +08:00
|
|
|
if e.errno == errno.EADDRINUSE:
|
|
|
|
self.log.info('The port %i is already in use, trying another random port.' % port)
|
|
|
|
continue
|
|
|
|
elif e.errno in (errno.EACCES, getattr(errno, 'WSAEACCES', errno.EACCES)):
|
|
|
|
self.log.warn("Permission to listen on port %i denied" % port)
|
|
|
|
continue
|
|
|
|
else:
|
2011-08-15 12:41:01 +08:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self.port = port
|
2012-06-01 06:06:33 +08:00
|
|
|
success = True
|
2011-08-15 12:41:01 +08:00
|
|
|
break
|
2012-06-01 06:06:33 +08:00
|
|
|
if not success:
|
|
|
|
self.log.critical('ERROR: the notebook server could not be started because '
|
|
|
|
'no available port could be found.')
|
2012-06-01 07:24:22 +08:00
|
|
|
self.exit(1)
|
2011-12-21 04:26:06 +08:00
|
|
|
|
2014-01-09 05:02:36 +08:00
|
|
|
@property
|
|
|
|
def display_url(self):
|
|
|
|
ip = self.ip if self.ip else '[all ip addresses on your system]'
|
|
|
|
return self._url(ip)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connection_url(self):
|
2014-04-18 03:01:55 +08:00
|
|
|
ip = self.ip if self.ip else 'localhost'
|
2014-01-09 05:02:36 +08:00
|
|
|
return self._url(ip)
|
|
|
|
|
|
|
|
def _url(self, ip):
|
|
|
|
proto = 'https' if self.certfile else 'http'
|
2014-02-06 08:16:32 +08:00
|
|
|
return "%s://%s:%i%s" % (proto, ip, self.port, self.base_url)
|
2014-01-09 05:02:36 +08:00
|
|
|
|
2012-04-16 03:05:59 +08:00
|
|
|
def init_signal(self):
|
2013-03-29 01:22:16 +08:00
|
|
|
if not sys.platform.startswith('win'):
|
2012-04-17 02:59:52 +08:00
|
|
|
signal.signal(signal.SIGINT, self._handle_sigint)
|
2012-04-16 12:48:37 +08:00
|
|
|
signal.signal(signal.SIGTERM, self._signal_stop)
|
2013-03-25 23:18:36 +08:00
|
|
|
if hasattr(signal, 'SIGUSR1'):
|
|
|
|
# Windows doesn't support SIGUSR1
|
|
|
|
signal.signal(signal.SIGUSR1, self._signal_info)
|
2013-03-19 09:09:28 +08:00
|
|
|
if hasattr(signal, 'SIGINFO'):
|
|
|
|
# only on BSD-based systems
|
|
|
|
signal.signal(signal.SIGINFO, self._signal_info)
|
2012-04-16 03:05:59 +08:00
|
|
|
|
2012-04-16 12:48:37 +08:00
|
|
|
def _handle_sigint(self, sig, frame):
|
|
|
|
"""SIGINT handler spawns confirmation dialog"""
|
|
|
|
# register more forceful signal handler for ^C^C case
|
|
|
|
signal.signal(signal.SIGINT, self._signal_stop)
|
|
|
|
# request confirmation dialog in bg thread, to avoid
|
|
|
|
# blocking the App
|
|
|
|
thread = threading.Thread(target=self._confirm_exit)
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
def _restore_sigint_handler(self):
|
|
|
|
"""callback for restoring original SIGINT handler"""
|
|
|
|
signal.signal(signal.SIGINT, self._handle_sigint)
|
|
|
|
|
|
|
|
def _confirm_exit(self):
|
|
|
|
"""confirm shutdown on ^C
|
|
|
|
|
|
|
|
A second ^C, or answering 'y' within 5s will cause shutdown,
|
|
|
|
otherwise original SIGINT handler will be restored.
|
2012-06-25 07:06:12 +08:00
|
|
|
|
|
|
|
This doesn't work on Windows.
|
2012-04-16 12:48:37 +08:00
|
|
|
"""
|
2013-03-19 06:37:24 +08:00
|
|
|
info = self.log.info
|
|
|
|
info('interrupted')
|
2013-10-24 05:58:57 +08:00
|
|
|
print(self.notebook_info())
|
2013-03-25 01:53:59 +08:00
|
|
|
sys.stdout.write("Shutdown this notebook server (y/[n])? ")
|
2012-04-16 12:48:37 +08:00
|
|
|
sys.stdout.flush()
|
|
|
|
r,w,x = select.select([sys.stdin], [], [], 5)
|
|
|
|
if r:
|
|
|
|
line = sys.stdin.readline()
|
2014-04-07 17:17:11 +08:00
|
|
|
if line.lower().startswith('y') and 'n' not in line.lower():
|
2012-04-16 12:48:37 +08:00
|
|
|
self.log.critical("Shutdown confirmed")
|
|
|
|
ioloop.IOLoop.instance().stop()
|
|
|
|
return
|
|
|
|
else:
|
2013-10-24 05:58:57 +08:00
|
|
|
print("No answer for 5s:", end=' ')
|
|
|
|
print("resuming operation...")
|
2012-04-16 12:48:37 +08:00
|
|
|
# no answer, or answer is no:
|
|
|
|
# set it back to original SIGINT handler
|
|
|
|
# use IOLoop.add_callback because signal.signal must be called
|
|
|
|
# from main thread
|
|
|
|
ioloop.IOLoop.instance().add_callback(self._restore_sigint_handler)
|
|
|
|
|
|
|
|
def _signal_stop(self, sig, frame):
|
2012-04-16 03:05:59 +08:00
|
|
|
self.log.critical("received signal %s, stopping", sig)
|
|
|
|
ioloop.IOLoop.instance().stop()
|
2013-03-19 09:09:28 +08:00
|
|
|
|
|
|
|
def _signal_info(self, sig, frame):
|
2013-10-24 05:58:57 +08:00
|
|
|
print(self.notebook_info())
|
2012-04-16 03:05:59 +08:00
|
|
|
|
2013-05-04 06:44:05 +08:00
|
|
|
def init_components(self):
|
|
|
|
"""Check the components submodule, and warn if it's unclean"""
|
|
|
|
status = submodule.check_submodule_status()
|
|
|
|
if status == 'missing':
|
|
|
|
self.log.warn("components submodule missing, running `git submodule update`")
|
|
|
|
submodule.update_submodules(submodule.ipython_parent())
|
|
|
|
elif status == 'unclean':
|
|
|
|
self.log.warn("components submodule unclean, you may see 404s on static/components")
|
|
|
|
self.log.warn("run `setup.py submodule` or `git submodule update` to update")
|
|
|
|
|
2011-12-21 04:26:06 +08:00
|
|
|
@catch_config_error
|
|
|
|
def initialize(self, argv=None):
|
|
|
|
super(NotebookApp, self).initialize(argv)
|
2013-10-24 03:01:38 +08:00
|
|
|
self.init_logging()
|
2013-08-23 15:17:35 +08:00
|
|
|
self.init_kernel_argv()
|
2011-12-21 04:26:06 +08:00
|
|
|
self.init_configurables()
|
2013-05-04 06:44:05 +08:00
|
|
|
self.init_components()
|
2011-12-21 04:26:06 +08:00
|
|
|
self.init_webapp()
|
2012-04-16 03:05:59 +08:00
|
|
|
self.init_signal()
|
2011-12-21 04:26:06 +08:00
|
|
|
|
|
|
|
def cleanup_kernels(self):
|
2013-01-14 05:01:00 +08:00
|
|
|
"""Shutdown all kernels.
|
2011-12-21 04:26:06 +08:00
|
|
|
|
|
|
|
The kernels will shutdown themselves when this process no longer exists,
|
|
|
|
but explicit shutdown allows the KernelManagers to cleanup the connection files.
|
|
|
|
"""
|
|
|
|
self.log.info('Shutting down kernels')
|
2013-01-14 05:01:00 +08:00
|
|
|
self.kernel_manager.shutdown_all()
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2013-03-25 01:53:59 +08:00
|
|
|
def notebook_info(self):
|
|
|
|
"Return the current working directory and the server url information"
|
2014-05-31 04:01:21 +08:00
|
|
|
info = self.contents_manager.info_string() + "\n"
|
2013-09-25 00:49:29 +08:00
|
|
|
info += "%d active kernels \n" % len(self.kernel_manager._kernels)
|
2014-01-09 05:02:36 +08:00
|
|
|
return info + "The IPython Notebook is running at: %s" % self.display_url
|
|
|
|
|
|
|
|
def server_info(self):
|
|
|
|
"""Return a JSONable dict of information about this server."""
|
|
|
|
return {'url': self.connection_url,
|
|
|
|
'hostname': self.ip if self.ip else 'localhost',
|
|
|
|
'port': self.port,
|
|
|
|
'secure': bool(self.certfile),
|
2014-02-06 08:16:32 +08:00
|
|
|
'base_url': self.base_url,
|
2014-02-13 15:03:43 +08:00
|
|
|
'notebook_dir': os.path.abspath(self.notebook_dir),
|
2014-07-09 13:03:16 +08:00
|
|
|
'pid': os.getpid()
|
2014-01-09 05:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def write_server_info_file(self):
|
|
|
|
"""Write the result of server_info() to the JSON file info_file."""
|
2014-01-09 09:56:12 +08:00
|
|
|
with open(self.info_file, 'w') as f:
|
|
|
|
json.dump(self.server_info(), f, indent=2)
|
2014-01-09 05:02:36 +08:00
|
|
|
|
|
|
|
def remove_server_info_file(self):
|
|
|
|
"""Remove the nbserver-<pid>.json file created for this server.
|
|
|
|
|
|
|
|
Ignores the error raised when the file has already been removed.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
os.unlink(self.info_file)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
2013-03-19 06:37:24 +08:00
|
|
|
|
2011-07-13 03:10:34 +08:00
|
|
|
def start(self):
|
2013-04-26 02:58:58 +08:00
|
|
|
""" Start the IPython Notebook server app, after initialization
|
2013-03-19 01:35:07 +08:00
|
|
|
|
|
|
|
This method takes no arguments so all configuration and initialization
|
|
|
|
must be done prior to calling this method."""
|
2014-01-14 08:22:43 +08:00
|
|
|
if self.subapp is not None:
|
|
|
|
return self.subapp.start()
|
|
|
|
|
2011-12-19 13:55:05 +08:00
|
|
|
info = self.log.info
|
2013-03-25 01:53:59 +08:00
|
|
|
for line in self.notebook_info().split("\n"):
|
|
|
|
info(line)
|
2013-07-06 13:30:12 +08:00
|
|
|
info("Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).")
|
2011-12-19 13:55:05 +08:00
|
|
|
|
2014-01-09 05:02:36 +08:00
|
|
|
self.write_server_info_file()
|
|
|
|
|
2012-06-14 04:48:56 +08:00
|
|
|
if self.open_browser or self.file_to_run:
|
2012-06-23 06:43:11 +08:00
|
|
|
try:
|
|
|
|
browser = webbrowser.get(self.browser or None)
|
|
|
|
except webbrowser.Error as e:
|
|
|
|
self.log.warn('No web browser found: %s.' % e)
|
|
|
|
browser = None
|
2013-10-11 16:41:51 +08:00
|
|
|
|
2014-02-25 07:28:14 +08:00
|
|
|
if self.file_to_run:
|
|
|
|
fullpath = os.path.join(self.notebook_dir, self.file_to_run)
|
|
|
|
if not os.path.exists(fullpath):
|
|
|
|
self.log.critical("%s does not exist" % fullpath)
|
|
|
|
self.exit(1)
|
|
|
|
|
|
|
|
uri = url_path_join('notebooks', self.file_to_run)
|
2012-05-02 22:20:07 +08:00
|
|
|
else:
|
2014-02-25 07:28:14 +08:00
|
|
|
uri = 'tree'
|
2012-06-23 06:43:11 +08:00
|
|
|
if browser:
|
2014-02-25 07:28:14 +08:00
|
|
|
b = lambda : browser.open(url_path_join(self.connection_url, uri),
|
2014-01-09 05:02:36 +08:00
|
|
|
new=2)
|
2012-06-23 06:43:11 +08:00
|
|
|
threading.Thread(target=b).start()
|
2011-12-21 04:26:06 +08:00
|
|
|
try:
|
|
|
|
ioloop.IOLoop.instance().start()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
info("Interrupted...")
|
|
|
|
finally:
|
|
|
|
self.cleanup_kernels()
|
2014-01-09 05:02:36 +08:00
|
|
|
self.remove_server_info_file()
|
|
|
|
|
|
|
|
|
2014-01-14 08:22:43 +08:00
|
|
|
def list_running_servers(profile='default'):
|
2014-01-09 05:02:36 +08:00
|
|
|
"""Iterate over the server info files of running notebook servers.
|
2011-12-21 04:26:06 +08:00
|
|
|
|
2014-01-09 05:02:36 +08:00
|
|
|
Given a profile name, find nbserver-* files in the security directory of
|
|
|
|
that profile, and yield dicts of their information, each one pertaining to
|
|
|
|
a currently running notebook server instance.
|
|
|
|
"""
|
|
|
|
pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), name=profile)
|
|
|
|
for file in os.listdir(pd.security_dir):
|
|
|
|
if file.startswith('nbserver-'):
|
|
|
|
with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
|
2014-07-09 13:03:16 +08:00
|
|
|
info = json.load(f)
|
2011-07-13 03:10:34 +08:00
|
|
|
|
2014-07-09 13:03:16 +08:00
|
|
|
# Simple check whether that process is really still running
|
|
|
|
if check_pid(info['pid']):
|
|
|
|
yield info
|
|
|
|
else:
|
|
|
|
# If the process has died, try to delete its info file
|
|
|
|
try:
|
|
|
|
os.unlink(file)
|
|
|
|
except OSError:
|
|
|
|
pass # TODO: This should warn or log or something
|
2011-07-13 03:10:34 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Main entry point
|
|
|
|
#-----------------------------------------------------------------------------
|
2011-03-18 12:09:04 +08:00
|
|
|
|
2013-07-05 03:48:07 +08:00
|
|
|
launch_new_instance = NotebookApp.launch_instance
|
2011-03-18 12:09:04 +08:00
|
|
|
|