You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1810 lines
69 KiB

4 years ago
  1. # coding: utf-8
  2. """A tornado based Jupyter notebook server."""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. from __future__ import absolute_import, print_function
  6. import notebook
  7. import binascii
  8. import datetime
  9. import errno
  10. import gettext
  11. import hashlib
  12. import hmac
  13. import importlib
  14. import io
  15. import ipaddress
  16. import json
  17. import logging
  18. import mimetypes
  19. import os
  20. import random
  21. import re
  22. import select
  23. import signal
  24. import socket
  25. import sys
  26. import threading
  27. import time
  28. import warnings
  29. import webbrowser
  30. try: #PY3
  31. from base64 import encodebytes
  32. except ImportError: #PY2
  33. from base64 import encodestring as encodebytes
  34. from jinja2 import Environment, FileSystemLoader
  35. from notebook.transutils import trans, _
  36. # Install the pyzmq ioloop. This has to be done before anything else from
  37. # tornado is imported.
  38. from zmq.eventloop import ioloop
  39. ioloop.install()
  40. # check for tornado 3.1.0
  41. try:
  42. import tornado
  43. except ImportError:
  44. raise ImportError(_("The Jupyter Notebook requires tornado >= 4.0"))
  45. try:
  46. version_info = tornado.version_info
  47. except AttributeError:
  48. raise ImportError(_("The Jupyter Notebook requires tornado >= 4.0, but you have < 1.1.0"))
  49. if version_info < (4,0):
  50. raise ImportError(_("The Jupyter Notebook requires tornado >= 4.0, but you have %s") % tornado.version)
  51. from tornado import httpserver
  52. from tornado import web
  53. from tornado.httputil import url_concat
  54. from tornado.log import LogFormatter, app_log, access_log, gen_log
  55. from notebook import (
  56. DEFAULT_STATIC_FILES_PATH,
  57. DEFAULT_TEMPLATE_PATH_LIST,
  58. __version__,
  59. )
  60. # py23 compatibility
  61. try:
  62. raw_input = raw_input
  63. except NameError:
  64. raw_input = input
  65. from .base.handlers import Template404, RedirectWithParams
  66. from .log import log_request
  67. from .services.kernels.kernelmanager import MappingKernelManager
  68. from .services.config import ConfigManager
  69. from .services.contents.manager import ContentsManager
  70. from .services.contents.filemanager import FileContentsManager
  71. from .services.contents.largefilemanager import LargeFileManager
  72. from .services.sessions.sessionmanager import SessionManager
  73. from .auth.login import LoginHandler
  74. from .auth.logout import LogoutHandler
  75. from .base.handlers import FileFindHandler
  76. from traitlets.config import Config
  77. from traitlets.config.application import catch_config_error, boolean_flag
  78. from jupyter_core.application import (
  79. JupyterApp, base_flags, base_aliases,
  80. )
  81. from jupyter_core.paths import jupyter_config_path
  82. from jupyter_client import KernelManager
  83. from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel, NATIVE_KERNEL_NAME
  84. from jupyter_client.session import Session
  85. from nbformat.sign import NotebookNotary
  86. from traitlets import (
  87. Any, Dict, Unicode, Integer, List, Bool, Bytes, Instance,
  88. TraitError, Type, Float, observe, default, validate
  89. )
  90. from ipython_genutils import py3compat
  91. from jupyter_core.paths import jupyter_runtime_dir, jupyter_path
  92. from notebook._sysinfo import get_sys_info
  93. from ._tz import utcnow, utcfromtimestamp
  94. from .utils import url_path_join, check_pid, url_escape
  95. #-----------------------------------------------------------------------------
  96. # Module globals
  97. #-----------------------------------------------------------------------------
  98. _examples = """
  99. jupyter notebook # start the notebook
  100. jupyter notebook --certfile=mycert.pem # use SSL/TLS certificate
  101. jupyter notebook password # enter a password to protect the server
  102. """
  103. #-----------------------------------------------------------------------------
  104. # Helper functions
  105. #-----------------------------------------------------------------------------
  106. def random_ports(port, n):
  107. """Generate a list of n random ports near the given port.
  108. The first 5 ports will be sequential, and the remaining n-5 will be
  109. randomly selected in the range [port-2*n, port+2*n].
  110. """
  111. for i in range(min(5, n)):
  112. yield port + i
  113. for i in range(n-5):
  114. yield max(1, port + random.randint(-2*n, 2*n))
  115. def load_handlers(name):
  116. """Load the (URL pattern, handler) tuples for each component."""
  117. mod = __import__(name, fromlist=['default_handlers'])
  118. return mod.default_handlers
  119. #-----------------------------------------------------------------------------
  120. # The Tornado web application
  121. #-----------------------------------------------------------------------------
  122. class NotebookWebApplication(web.Application):
  123. def __init__(self, jupyter_app, kernel_manager, contents_manager,
  124. session_manager, kernel_spec_manager,
  125. config_manager, extra_services, log,
  126. base_url, default_url, settings_overrides, jinja_env_options):
  127. settings = self.init_settings(
  128. jupyter_app, kernel_manager, contents_manager,
  129. session_manager, kernel_spec_manager, config_manager,
  130. extra_services, log, base_url,
  131. default_url, settings_overrides, jinja_env_options)
  132. handlers = self.init_handlers(settings)
  133. super(NotebookWebApplication, self).__init__(handlers, **settings)
  134. def init_settings(self, jupyter_app, kernel_manager, contents_manager,
  135. session_manager, kernel_spec_manager,
  136. config_manager, extra_services,
  137. log, base_url, default_url, settings_overrides,
  138. jinja_env_options=None):
  139. _template_path = settings_overrides.get(
  140. "template_path",
  141. jupyter_app.template_file_path,
  142. )
  143. if isinstance(_template_path, py3compat.string_types):
  144. _template_path = (_template_path,)
  145. template_path = [os.path.expanduser(path) for path in _template_path]
  146. jenv_opt = {"autoescape": True}
  147. jenv_opt.update(jinja_env_options if jinja_env_options else {})
  148. env = Environment(loader=FileSystemLoader(template_path), extensions=['jinja2.ext.i18n'], **jenv_opt)
  149. sys_info = get_sys_info()
  150. # If the user is running the notebook in a git directory, make the assumption
  151. # that this is a dev install and suggest to the developer `npm run build:watch`.
  152. base_dir = os.path.realpath(os.path.join(__file__, '..', '..'))
  153. dev_mode = os.path.exists(os.path.join(base_dir, '.git'))
  154. nbui = gettext.translation('nbui', localedir=os.path.join(base_dir, 'notebook/i18n'), fallback=True)
  155. env.install_gettext_translations(nbui, newstyle=False)
  156. if dev_mode:
  157. DEV_NOTE_NPM = """It looks like you're running the notebook from source.
  158. If you're working on the Javascript of the notebook, try running
  159. %s
  160. in another terminal window to have the system incrementally
  161. watch and build the notebook's JavaScript for you, as you make changes.""" % 'npm run build:watch'
  162. log.info(DEV_NOTE_NPM)
  163. if sys_info['commit_source'] == 'repository':
  164. # don't cache (rely on 304) when working from master
  165. version_hash = ''
  166. else:
  167. # reset the cache on server restart
  168. version_hash = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  169. if jupyter_app.ignore_minified_js:
  170. log.warning(_("""The `ignore_minified_js` flag is deprecated and no longer works."""))
  171. log.warning(_("""Alternatively use `%s` when working on the notebook's Javascript and LESS""") % 'npm run build:watch')
  172. warnings.warn(_("The `ignore_minified_js` flag is deprecated and will be removed in Notebook 6.0"), DeprecationWarning)
  173. now = utcnow()
  174. root_dir = contents_manager.root_dir
  175. home = os.path.expanduser('~')
  176. if root_dir.startswith(home + os.path.sep):
  177. # collapse $HOME to ~
  178. root_dir = '~' + root_dir[len(home):]
  179. settings = dict(
  180. # basics
  181. log_function=log_request,
  182. base_url=base_url,
  183. default_url=default_url,
  184. template_path=template_path,
  185. static_path=jupyter_app.static_file_path,
  186. static_custom_path=jupyter_app.static_custom_path,
  187. static_handler_class = FileFindHandler,
  188. static_url_prefix = url_path_join(base_url,'/static/'),
  189. static_handler_args = {
  190. # don't cache custom.js
  191. 'no_cache_paths': [url_path_join(base_url, 'static', 'custom')],
  192. },
  193. version_hash=version_hash,
  194. ignore_minified_js=jupyter_app.ignore_minified_js,
  195. # rate limits
  196. iopub_msg_rate_limit=jupyter_app.iopub_msg_rate_limit,
  197. iopub_data_rate_limit=jupyter_app.iopub_data_rate_limit,
  198. rate_limit_window=jupyter_app.rate_limit_window,
  199. # authentication
  200. cookie_secret=jupyter_app.cookie_secret,
  201. login_url=url_path_join(base_url,'/login'),
  202. login_handler_class=jupyter_app.login_handler_class,
  203. logout_handler_class=jupyter_app.logout_handler_class,
  204. password=jupyter_app.password,
  205. xsrf_cookies=True,
  206. disable_check_xsrf=jupyter_app.disable_check_xsrf,
  207. allow_remote_access=jupyter_app.allow_remote_access,
  208. local_hostnames=jupyter_app.local_hostnames,
  209. # managers
  210. kernel_manager=kernel_manager,
  211. contents_manager=contents_manager,
  212. session_manager=session_manager,
  213. kernel_spec_manager=kernel_spec_manager,
  214. config_manager=config_manager,
  215. # handlers
  216. extra_services=extra_services,
  217. # Jupyter stuff
  218. started=now,
  219. # place for extensions to register activity
  220. # so that they can prevent idle-shutdown
  221. last_activity_times={},
  222. jinja_template_vars=jupyter_app.jinja_template_vars,
  223. nbextensions_path=jupyter_app.nbextensions_path,
  224. websocket_url=jupyter_app.websocket_url,
  225. mathjax_url=jupyter_app.mathjax_url,
  226. mathjax_config=jupyter_app.mathjax_config,
  227. shutdown_button=jupyter_app.quit_button,
  228. config=jupyter_app.config,
  229. config_dir=jupyter_app.config_dir,
  230. allow_password_change=jupyter_app.allow_password_change,
  231. server_root_dir=root_dir,
  232. jinja2_env=env,
  233. terminals_available=False, # Set later if terminals are available
  234. )
  235. # allow custom overrides for the tornado web app.
  236. settings.update(settings_overrides)
  237. return settings
  238. def init_handlers(self, settings):
  239. """Load the (URL pattern, handler) tuples for each component."""
  240. # Order matters. The first handler to match the URL will handle the request.
  241. handlers = []
  242. # load extra services specified by users before default handlers
  243. for service in settings['extra_services']:
  244. handlers.extend(load_handlers(service))
  245. handlers.extend(load_handlers('notebook.tree.handlers'))
  246. handlers.extend([(r"/login", settings['login_handler_class'])])
  247. handlers.extend([(r"/logout", settings['logout_handler_class'])])
  248. handlers.extend(load_handlers('notebook.files.handlers'))
  249. handlers.extend(load_handlers('notebook.view.handlers'))
  250. handlers.extend(load_handlers('notebook.notebook.handlers'))
  251. handlers.extend(load_handlers('notebook.nbconvert.handlers'))
  252. handlers.extend(load_handlers('notebook.bundler.handlers'))
  253. handlers.extend(load_handlers('notebook.kernelspecs.handlers'))
  254. handlers.extend(load_handlers('notebook.edit.handlers'))
  255. handlers.extend(load_handlers('notebook.services.api.handlers'))
  256. handlers.extend(load_handlers('notebook.services.config.handlers'))
  257. handlers.extend(load_handlers('notebook.services.kernels.handlers'))
  258. handlers.extend(load_handlers('notebook.services.contents.handlers'))
  259. handlers.extend(load_handlers('notebook.services.sessions.handlers'))
  260. handlers.extend(load_handlers('notebook.services.nbconvert.handlers'))
  261. handlers.extend(load_handlers('notebook.services.kernelspecs.handlers'))
  262. handlers.extend(load_handlers('notebook.services.security.handlers'))
  263. handlers.extend(load_handlers('notebook.services.shutdown'))
  264. handlers.extend(settings['contents_manager'].get_extra_handlers())
  265. handlers.append(
  266. (r"/nbextensions/(.*)", FileFindHandler, {
  267. 'path': settings['nbextensions_path'],
  268. 'no_cache_paths': ['/'], # don't cache anything in nbextensions
  269. }),
  270. )
  271. handlers.append(
  272. (r"/custom/(.*)", FileFindHandler, {
  273. 'path': settings['static_custom_path'],
  274. 'no_cache_paths': ['/'], # don't cache anything in custom
  275. })
  276. )
  277. # register base handlers last
  278. handlers.extend(load_handlers('notebook.base.handlers'))
  279. # set the URL that will be redirected from `/`
  280. handlers.append(
  281. (r'/?', RedirectWithParams, {
  282. 'url' : settings['default_url'],
  283. 'permanent': False, # want 302, not 301
  284. })
  285. )
  286. # prepend base_url onto the patterns that we match
  287. new_handlers = []
  288. for handler in handlers:
  289. pattern = url_path_join(settings['base_url'], handler[0])
  290. new_handler = tuple([pattern] + list(handler[1:]))
  291. new_handlers.append(new_handler)
  292. # add 404 on the end, which will catch everything that falls through
  293. new_handlers.append((r'(.*)', Template404))
  294. return new_handlers
  295. def last_activity(self):
  296. """Get a UTC timestamp for when the server last did something.
  297. Includes: API activity, kernel activity, kernel shutdown, and terminal
  298. activity.
  299. """
  300. sources = [
  301. self.settings['started'],
  302. self.settings['kernel_manager'].last_kernel_activity,
  303. ]
  304. try:
  305. sources.append(self.settings['api_last_activity'])
  306. except KeyError:
  307. pass
  308. try:
  309. sources.append(self.settings['terminal_last_activity'])
  310. except KeyError:
  311. pass
  312. sources.extend(self.settings['last_activity_times'].values())
  313. return max(sources)
  314. class NotebookPasswordApp(JupyterApp):
  315. """Set a password for the notebook server.
  316. Setting a password secures the notebook server
  317. and removes the need for token-based authentication.
  318. """
  319. description = __doc__
  320. def _config_file_default(self):
  321. return os.path.join(self.config_dir, 'jupyter_notebook_config.json')
  322. def start(self):
  323. from .auth.security import set_password
  324. set_password(config_file=self.config_file)
  325. self.log.info("Wrote hashed password to %s" % self.config_file)
  326. def shutdown_server(server_info, timeout=5, log=None):
  327. """Shutdown a notebook server in a separate process.
  328. *server_info* should be a dictionary as produced by list_running_servers().
  329. Will first try to request shutdown using /api/shutdown .
  330. On Unix, if the server is still running after *timeout* seconds, it will
  331. send SIGTERM. After another timeout, it escalates to SIGKILL.
  332. Returns True if the server was stopped by any means, False if stopping it
  333. failed (on Windows).
  334. """
  335. from tornado.httpclient import HTTPClient, HTTPRequest
  336. url = server_info['url']
  337. pid = server_info['pid']
  338. req = HTTPRequest(url + 'api/shutdown', method='POST', body=b'', headers={
  339. 'Authorization': 'token ' + server_info['token']
  340. })
  341. if log: log.debug("POST request to %sapi/shutdown", url)
  342. HTTPClient().fetch(req)
  343. # Poll to see if it shut down.
  344. for _ in range(timeout*10):
  345. if check_pid(pid):
  346. if log: log.debug("Server PID %s is gone", pid)
  347. return True
  348. time.sleep(0.1)
  349. if sys.platform.startswith('win'):
  350. return False
  351. if log: log.debug("SIGTERM to PID %s", pid)
  352. os.kill(pid, signal.SIGTERM)
  353. # Poll to see if it shut down.
  354. for _ in range(timeout * 10):
  355. if check_pid(pid):
  356. if log: log.debug("Server PID %s is gone", pid)
  357. return True
  358. time.sleep(0.1)
  359. if log: log.debug("SIGKILL to PID %s", pid)
  360. os.kill(pid, signal.SIGKILL)
  361. return True # SIGKILL cannot be caught
  362. class NbserverStopApp(JupyterApp):
  363. version = __version__
  364. description="Stop currently running notebook server for a given port"
  365. port = Integer(8888, config=True,
  366. help="Port of the server to be killed. Default 8888")
  367. def parse_command_line(self, argv=None):
  368. super(NbserverStopApp, self).parse_command_line(argv)
  369. if self.extra_args:
  370. self.port=int(self.extra_args[0])
  371. def shutdown_server(self, server):
  372. return shutdown_server(server, log=self.log)
  373. def start(self):
  374. servers = list(list_running_servers(self.runtime_dir))
  375. if not servers:
  376. self.exit("There are no running servers")
  377. for server in servers:
  378. if server['port'] == self.port:
  379. print("Shutting down server on port", self.port, "...")
  380. if not self.shutdown_server(server):
  381. sys.exit("Could not stop server")
  382. return
  383. else:
  384. print("There is currently no server running on port {}".format(self.port), file=sys.stderr)
  385. print("Ports currently in use:", file=sys.stderr)
  386. for server in servers:
  387. print(" - {}".format(server['port']), file=sys.stderr)
  388. self.exit(1)
  389. class NbserverListApp(JupyterApp):
  390. version = __version__
  391. description=_("List currently running notebook servers.")
  392. flags = dict(
  393. jsonlist=({'NbserverListApp': {'jsonlist': True}},
  394. _("Produce machine-readable JSON list output.")),
  395. json=({'NbserverListApp': {'json': True}},
  396. _("Produce machine-readable JSON object on each line of output.")),
  397. )
  398. jsonlist = Bool(False, config=True,
  399. help=_("If True, the output will be a JSON list of objects, one per "
  400. "active notebook server, each with the details from the "
  401. "relevant server info file."))
  402. json = Bool(False, config=True,
  403. help=_("If True, each line of output will be a JSON object with the "
  404. "details from the server info file. For a JSON list output, "
  405. "see the NbserverListApp.jsonlist configuration value"))
  406. def start(self):
  407. serverinfo_list = list(list_running_servers(self.runtime_dir))
  408. if self.jsonlist:
  409. print(json.dumps(serverinfo_list, indent=2))
  410. elif self.json:
  411. for serverinfo in serverinfo_list:
  412. print(json.dumps(serverinfo))
  413. else:
  414. print("Currently running servers:")
  415. for serverinfo in serverinfo_list:
  416. url = serverinfo['url']
  417. if serverinfo.get('token'):
  418. url = url + '?token=%s' % serverinfo['token']
  419. print(url, "::", serverinfo['notebook_dir'])
  420. #-----------------------------------------------------------------------------
  421. # Aliases and Flags
  422. #-----------------------------------------------------------------------------
  423. flags = dict(base_flags)
  424. flags['no-browser']=(
  425. {'NotebookApp' : {'open_browser' : False}},
  426. _("Don't open the notebook in a browser after startup.")
  427. )
  428. flags['pylab']=(
  429. {'NotebookApp' : {'pylab' : 'warn'}},
  430. _("DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.")
  431. )
  432. flags['no-mathjax']=(
  433. {'NotebookApp' : {'enable_mathjax' : False}},
  434. """Disable MathJax
  435. MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
  436. very large, so you may want to disable it if you have a slow internet
  437. connection, or for offline use of the notebook.
  438. When disabled, equations etc. will appear as their untransformed TeX source.
  439. """
  440. )
  441. flags['allow-root']=(
  442. {'NotebookApp' : {'allow_root' : True}},
  443. _("Allow the notebook to be run from root user.")
  444. )
  445. # Add notebook manager flags
  446. flags.update(boolean_flag('script', 'FileContentsManager.save_script',
  447. 'DEPRECATED, IGNORED',
  448. 'DEPRECATED, IGNORED'))
  449. aliases = dict(base_aliases)
  450. aliases.update({
  451. 'ip': 'NotebookApp.ip',
  452. 'port': 'NotebookApp.port',
  453. 'port-retries': 'NotebookApp.port_retries',
  454. 'transport': 'KernelManager.transport',
  455. 'keyfile': 'NotebookApp.keyfile',
  456. 'certfile': 'NotebookApp.certfile',
  457. 'client-ca': 'NotebookApp.client_ca',
  458. 'notebook-dir': 'NotebookApp.notebook_dir',
  459. 'browser': 'NotebookApp.browser',
  460. 'pylab': 'NotebookApp.pylab',
  461. })
  462. #-----------------------------------------------------------------------------
  463. # NotebookApp
  464. #-----------------------------------------------------------------------------
  465. class NotebookApp(JupyterApp):
  466. name = 'jupyter-notebook'
  467. version = __version__
  468. description = _("""The Jupyter HTML Notebook.
  469. This launches a Tornado based HTML Notebook Server that serves up an HTML5/Javascript Notebook client.""")
  470. examples = _examples
  471. aliases = aliases
  472. flags = flags
  473. classes = [
  474. KernelManager, Session, MappingKernelManager,
  475. ContentsManager, FileContentsManager, NotebookNotary,
  476. KernelSpecManager,
  477. ]
  478. flags = Dict(flags)
  479. aliases = Dict(aliases)
  480. subcommands = dict(
  481. list=(NbserverListApp, NbserverListApp.description.splitlines()[0]),
  482. stop=(NbserverStopApp, NbserverStopApp.description.splitlines()[0]),
  483. password=(NotebookPasswordApp, NotebookPasswordApp.description.splitlines()[0]),
  484. )
  485. _log_formatter_cls = LogFormatter
  486. @default('log_level')
  487. def _default_log_level(self):
  488. return logging.INFO
  489. @default('log_datefmt')
  490. def _default_log_datefmt(self):
  491. """Exclude date from default date format"""
  492. return "%H:%M:%S"
  493. @default('log_format')
  494. def _default_log_format(self):
  495. """override default log format to include time"""
  496. return u"%(color)s[%(levelname)1.1s %(asctime)s.%(msecs).03d %(name)s]%(end_color)s %(message)s"
  497. ignore_minified_js = Bool(False,
  498. config=True,
  499. help=_('Deprecated: Use minified JS file or not, mainly use during dev to avoid JS recompilation'),
  500. )
  501. # file to be opened in the notebook server
  502. file_to_run = Unicode('', config=True)
  503. # Network related information
  504. allow_origin = Unicode('', config=True,
  505. help="""Set the Access-Control-Allow-Origin header
  506. Use '*' to allow any origin to access your server.
  507. Takes precedence over allow_origin_pat.
  508. """
  509. )
  510. allow_origin_pat = Unicode('', config=True,
  511. help="""Use a regular expression for the Access-Control-Allow-Origin header
  512. Requests from an origin matching the expression will get replies with:
  513. Access-Control-Allow-Origin: origin
  514. where `origin` is the origin of the request.
  515. Ignored if allow_origin is set.
  516. """
  517. )
  518. allow_credentials = Bool(False, config=True,
  519. help=_("Set the Access-Control-Allow-Credentials: true header")
  520. )
  521. allow_root = Bool(False, config=True,
  522. help=_("Whether to allow the user to run the notebook as root.")
  523. )
  524. default_url = Unicode('/tree', config=True,
  525. help=_("The default URL to redirect to from `/`")
  526. )
  527. ip = Unicode('localhost', config=True,
  528. help=_("The IP address the notebook server will listen on.")
  529. )
  530. @default('ip')
  531. def _default_ip(self):
  532. """Return localhost if available, 127.0.0.1 otherwise.
  533. On some (horribly broken) systems, localhost cannot be bound.
  534. """
  535. s = socket.socket()
  536. try:
  537. s.bind(('localhost', 0))
  538. except socket.error as e:
  539. self.log.warning(_("Cannot bind to localhost, using 127.0.0.1 as default ip\n%s"), e)
  540. return '127.0.0.1'
  541. else:
  542. s.close()
  543. return 'localhost'
  544. @validate('ip')
  545. def _valdate_ip(self, proposal):
  546. value = proposal['value']
  547. if value == u'*':
  548. value = u''
  549. return value
  550. custom_display_url = Unicode(u'', config=True,
  551. help=_("""Override URL shown to users.
  552. Replace actual URL, including protocol, address, port and base URL,
  553. with the given value when displaying URL to the users. Do not change
  554. the actual connection URL. If authentication token is enabled, the
  555. token is added to the custom URL automatically.
  556. This option is intended to be used when the URL to display to the user
  557. cannot be determined reliably by the Jupyter notebook server (proxified
  558. or containerized setups for example).""")
  559. )
  560. port = Integer(8888, config=True,
  561. help=_("The port the notebook server will listen on.")
  562. )
  563. port_retries = Integer(50, config=True,
  564. help=_("The number of additional ports to try if the specified port is not available.")
  565. )
  566. certfile = Unicode(u'', config=True,
  567. help=_("""The full path to an SSL/TLS certificate file.""")
  568. )
  569. keyfile = Unicode(u'', config=True,
  570. help=_("""The full path to a private key file for usage with SSL/TLS.""")
  571. )
  572. client_ca = Unicode(u'', config=True,
  573. help=_("""The full path to a certificate authority certificate for SSL/TLS client authentication.""")
  574. )
  575. cookie_secret_file = Unicode(config=True,
  576. help=_("""The file where the cookie secret is stored.""")
  577. )
  578. @default('cookie_secret_file')
  579. def _default_cookie_secret_file(self):
  580. return os.path.join(self.runtime_dir, 'notebook_cookie_secret')
  581. cookie_secret = Bytes(b'', config=True,
  582. help="""The random bytes used to secure cookies.
  583. By default this is a new random number every time you start the Notebook.
  584. Set it to a value in a config file to enable logins to persist across server sessions.
  585. Note: Cookie secrets should be kept private, do not share config files with
  586. cookie_secret stored in plaintext (you can read the value from a file).
  587. """
  588. )
  589. @default('cookie_secret')
  590. def _default_cookie_secret(self):
  591. if os.path.exists(self.cookie_secret_file):
  592. with io.open(self.cookie_secret_file, 'rb') as f:
  593. key = f.read()
  594. else:
  595. key = encodebytes(os.urandom(32))
  596. self._write_cookie_secret_file(key)
  597. h = hmac.new(key, digestmod=hashlib.sha256)
  598. h.update(self.password.encode())
  599. return h.digest()
  600. def _write_cookie_secret_file(self, secret):
  601. """write my secret to my secret_file"""
  602. self.log.info(_("Writing notebook server cookie secret to %s"), self.cookie_secret_file)
  603. try:
  604. with io.open(self.cookie_secret_file, 'wb') as f:
  605. f.write(secret)
  606. except OSError as e:
  607. self.log.error(_("Failed to write cookie secret to %s: %s"),
  608. self.cookie_secret_file, e)
  609. try:
  610. os.chmod(self.cookie_secret_file, 0o600)
  611. except OSError:
  612. self.log.warning(
  613. _("Could not set permissions on %s"),
  614. self.cookie_secret_file
  615. )
  616. token = Unicode('<generated>',
  617. help=_("""Token used for authenticating first-time connections to the server.
  618. When no password is enabled,
  619. the default is to generate a new, random token.
  620. Setting to an empty string disables authentication altogether, which is NOT RECOMMENDED.
  621. """)
  622. ).tag(config=True)
  623. one_time_token = Unicode(
  624. help=_("""One-time token used for opening a browser.
  625. Once used, this token cannot be used again.
  626. """)
  627. )
  628. _token_generated = True
  629. @default('token')
  630. def _token_default(self):
  631. if os.getenv('JUPYTER_TOKEN'):
  632. self._token_generated = False
  633. return os.getenv('JUPYTER_TOKEN')
  634. if self.password:
  635. # no token if password is enabled
  636. self._token_generated = False
  637. return u''
  638. else:
  639. self._token_generated = True
  640. return binascii.hexlify(os.urandom(24)).decode('ascii')
  641. max_body_size = Integer(512 * 1024 * 1024, config=True,
  642. help="""
  643. Sets the maximum allowed size of the client request body, specified in
  644. the Content-Length request header field. If the size in a request
  645. exceeds the configured value, a malformed HTTP message is returned to
  646. the client.
  647. Note: max_body_size is applied even in streaming mode.
  648. """
  649. )
  650. max_buffer_size = Integer(512 * 1024 * 1024, config=True,
  651. help="""
  652. Gets or sets the maximum amount of memory, in bytes, that is allocated
  653. for use by the buffer manager.
  654. """
  655. )
  656. @observe('token')
  657. def _token_changed(self, change):
  658. self._token_generated = False
  659. password = Unicode(u'', config=True,
  660. help="""Hashed password to use for web authentication.
  661. To generate, type in a python/IPython shell:
  662. from notebook.auth import passwd; passwd()
  663. The string should be of the form type:salt:hashed-password.
  664. """
  665. )
  666. password_required = Bool(False, config=True,
  667. help="""Forces users to use a password for the Notebook server.
  668. This is useful in a multi user environment, for instance when
  669. everybody in the LAN can access each other's machine through ssh.
  670. In such a case, server the notebook server on localhost is not secure
  671. since any user can connect to the notebook server via ssh.
  672. """
  673. )
  674. allow_password_change = Bool(True, config=True,
  675. help="""Allow password to be changed at login for the notebook server.
  676. While loggin in with a token, the notebook server UI will give the opportunity to
  677. the user to enter a new password at the same time that will replace
  678. the token login mechanism.
  679. This can be set to false to prevent changing password from the UI/API.
  680. """
  681. )
  682. disable_check_xsrf = Bool(False, config=True,
  683. help="""Disable cross-site-request-forgery protection
  684. Jupyter notebook 4.3.1 introduces protection from cross-site request forgeries,
  685. requiring API requests to either:
  686. - originate from pages served by this server (validated with XSRF cookie and token), or
  687. - authenticate with a token
  688. Some anonymous compute resources still desire the ability to run code,
  689. completely without authentication.
  690. These services can disable all authentication and security checks,
  691. with the full knowledge of what that implies.
  692. """
  693. )
  694. allow_remote_access = Bool(config=True,
  695. help="""Allow requests where the Host header doesn't point to a local server
  696. By default, requests get a 403 forbidden response if the 'Host' header
  697. shows that the browser thinks it's on a non-local domain.
  698. Setting this option to True disables this check.
  699. This protects against 'DNS rebinding' attacks, where a remote web server
  700. serves you a page and then changes its DNS to send later requests to a
  701. local IP, bypassing same-origin checks.
  702. Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local,
  703. along with hostnames configured in local_hostnames.
  704. """)
  705. @default('allow_remote_access')
  706. def _default_allow_remote(self):
  707. """Disallow remote access if we're listening only on loopback addresses"""
  708. try:
  709. addr = ipaddress.ip_address(self.ip)
  710. except ValueError:
  711. # Address is a hostname
  712. for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
  713. addr = info[4][0]
  714. if not py3compat.PY3:
  715. addr = addr.decode('ascii')
  716. try:
  717. parsed = ipaddress.ip_address(addr.split('%')[0])
  718. except ValueError:
  719. self.log.warning("Unrecognised IP address: %r", addr)
  720. continue
  721. # Macs map localhost to 'fe80::1%lo0', a link local address
  722. # scoped to the loopback interface. For now, we'll assume that
  723. # any scoped link-local address is effectively local.
  724. if not (parsed.is_loopback
  725. or (('%' in addr) and parsed.is_link_local)):
  726. return True
  727. return False
  728. else:
  729. return not addr.is_loopback
  730. local_hostnames = List(Unicode(), ['localhost'], config=True,
  731. help="""Hostnames to allow as local when allow_remote_access is False.
  732. Local IP addresses (such as 127.0.0.1 and ::1) are automatically accepted
  733. as local as well.
  734. """
  735. )
  736. open_browser = Bool(True, config=True,
  737. help="""Whether to open in a browser after starting.
  738. The specific browser used is platform dependent and
  739. determined by the python standard library `webbrowser`
  740. module, unless it is overridden using the --browser
  741. (NotebookApp.browser) configuration option.
  742. """)
  743. browser = Unicode(u'', config=True,
  744. help="""Specify what command to use to invoke a web
  745. browser when opening the notebook. If not specified, the
  746. default browser will be determined by the `webbrowser`
  747. standard library module, which allows setting of the
  748. BROWSER environment variable to override it.
  749. """)
  750. webbrowser_open_new = Integer(2, config=True,
  751. help=_("""Specify Where to open the notebook on startup. This is the
  752. `new` argument passed to the standard library method `webbrowser.open`.
  753. The behaviour is not guaranteed, but depends on browser support. Valid
  754. values are:
  755. - 2 opens a new tab,
  756. - 1 opens a new window,
  757. - 0 opens in an existing window.
  758. See the `webbrowser.open` documentation for details.
  759. """))
  760. webapp_settings = Dict(config=True,
  761. help=_("DEPRECATED, use tornado_settings")
  762. )
  763. @observe('webapp_settings')
  764. def _update_webapp_settings(self, change):
  765. self.log.warning(_("\n webapp_settings is deprecated, use tornado_settings.\n"))
  766. self.tornado_settings = change['new']
  767. tornado_settings = Dict(config=True,
  768. help=_("Supply overrides for the tornado.web.Application that the "
  769. "Jupyter notebook uses."))
  770. websocket_compression_options = Any(None, config=True,
  771. help=_("""
  772. Set the tornado compression options for websocket connections.
  773. This value will be returned from :meth:`WebSocketHandler.get_compression_options`.
  774. None (default) will disable compression.
  775. A dict (even an empty one) will enable compression.
  776. See the tornado docs for WebSocketHandler.get_compression_options for details.
  777. """)
  778. )
  779. terminado_settings = Dict(config=True,
  780. help=_('Supply overrides for terminado. Currently only supports "shell_command".'))
  781. cookie_options = Dict(config=True,
  782. help=_("Extra keyword arguments to pass to `set_secure_cookie`."
  783. " See tornado's set_secure_cookie docs for details.")
  784. )
  785. get_secure_cookie_kwargs = Dict(config=True,
  786. help=_("Extra keyword arguments to pass to `get_secure_cookie`."
  787. " See tornado's get_secure_cookie docs for details.")
  788. )
  789. ssl_options = Dict(config=True,
  790. help=_("""Supply SSL options for the tornado HTTPServer.
  791. See the tornado docs for details."""))
  792. jinja_environment_options = Dict(config=True,
  793. help=_("Supply extra arguments that will be passed to Jinja environment."))
  794. jinja_template_vars = Dict(
  795. config=True,
  796. help=_("Extra variables to supply to jinja templates when rendering."),
  797. )
  798. enable_mathjax = Bool(True, config=True,
  799. help="""Whether to enable MathJax for typesetting math/TeX
  800. MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
  801. very large, so you may want to disable it if you have a slow internet
  802. connection, or for offline use of the notebook.
  803. When disabled, equations etc. will appear as their untransformed TeX source.
  804. """
  805. )
  806. @observe('enable_mathjax')
  807. def _update_enable_mathjax(self, change):
  808. """set mathjax url to empty if mathjax is disabled"""
  809. if not change['new']:
  810. self.mathjax_url = u''
  811. base_url = Unicode('/', config=True,
  812. help='''The base URL for the notebook server.
  813. Leading and trailing slashes can be omitted,
  814. and will automatically be added.
  815. ''')
  816. @validate('base_url')
  817. def _update_base_url(self, proposal):
  818. value = proposal['value']
  819. if not value.startswith('/'):
  820. value = '/' + value
  821. if not value.endswith('/'):
  822. value = value + '/'
  823. return value
  824. base_project_url = Unicode('/', config=True, help=_("""DEPRECATED use base_url"""))
  825. @observe('base_project_url')
  826. def _update_base_project_url(self, change):
  827. self.log.warning(_("base_project_url is deprecated, use base_url"))
  828. self.base_url = change['new']
  829. extra_static_paths = List(Unicode(), config=True,
  830. help="""Extra paths to search for serving static files.
  831. This allows adding javascript/css to be available from the notebook server machine,
  832. or overriding individual files in the IPython"""
  833. )
  834. @property
  835. def static_file_path(self):
  836. """return extra paths + the default location"""
  837. return self.extra_static_paths + [DEFAULT_STATIC_FILES_PATH]
  838. static_custom_path = List(Unicode(),
  839. help=_("""Path to search for custom.js, css""")
  840. )
  841. @default('static_custom_path')
  842. def _default_static_custom_path(self):
  843. return [
  844. os.path.join(d, 'custom') for d in (
  845. self.config_dir,
  846. DEFAULT_STATIC_FILES_PATH)
  847. ]
  848. extra_template_paths = List(Unicode(), config=True,
  849. help=_("""Extra paths to search for serving jinja templates.
  850. Can be used to override templates from notebook.templates.""")
  851. )
  852. @property
  853. def template_file_path(self):
  854. """return extra paths + the default locations"""
  855. return self.extra_template_paths + DEFAULT_TEMPLATE_PATH_LIST
  856. extra_nbextensions_path = List(Unicode(), config=True,
  857. help=_("""extra paths to look for Javascript notebook extensions""")
  858. )
  859. extra_services = List(Unicode(), config=True,
  860. help=_("""handlers that should be loaded at higher priority than the default services""")
  861. )
  862. @property
  863. def nbextensions_path(self):
  864. """The path to look for Javascript notebook extensions"""
  865. path = self.extra_nbextensions_path + jupyter_path('nbextensions')
  866. # FIXME: remove IPython nbextensions path after a migration period
  867. try:
  868. from IPython.paths import get_ipython_dir
  869. except ImportError:
  870. pass
  871. else:
  872. path.append(os.path.join(get_ipython_dir(), 'nbextensions'))
  873. return path
  874. websocket_url = Unicode("", config=True,
  875. help="""The base URL for websockets,
  876. if it differs from the HTTP server (hint: it almost certainly doesn't).
  877. Should be in the form of an HTTP origin: ws[s]://hostname[:port]
  878. """
  879. )
  880. mathjax_url = Unicode("", config=True,
  881. help="""A custom url for MathJax.js.
  882. Should be in the form of a case-sensitive url to MathJax,
  883. for example: /static/components/MathJax/MathJax.js
  884. """
  885. )
  886. @default('mathjax_url')
  887. def _default_mathjax_url(self):
  888. if not self.enable_mathjax:
  889. return u''
  890. static_url_prefix = self.tornado_settings.get("static_url_prefix", "static")
  891. return url_path_join(static_url_prefix, 'components', 'MathJax', 'MathJax.js')
  892. @observe('mathjax_url')
  893. def _update_mathjax_url(self, change):
  894. new = change['new']
  895. if new and not self.enable_mathjax:
  896. # enable_mathjax=False overrides mathjax_url
  897. self.mathjax_url = u''
  898. else:
  899. self.log.info(_("Using MathJax: %s"), new)
  900. mathjax_config = Unicode("TeX-AMS-MML_HTMLorMML-full,Safe", config=True,
  901. help=_("""The MathJax.js configuration file that is to be used.""")
  902. )
  903. @observe('mathjax_config')
  904. def _update_mathjax_config(self, change):
  905. self.log.info(_("Using MathJax configuration file: %s"), change['new'])
  906. quit_button = Bool(True, config=True,
  907. help="""If True, display a button in the dashboard to quit
  908. (shutdown the notebook server)."""
  909. )
  910. contents_manager_class = Type(
  911. default_value=LargeFileManager,
  912. klass=ContentsManager,
  913. config=True,
  914. help=_('The notebook manager class to use.')
  915. )
  916. kernel_manager_class = Type(
  917. default_value=MappingKernelManager,
  918. config=True,
  919. help=_('The kernel manager class to use.')
  920. )
  921. session_manager_class = Type(
  922. default_value=SessionManager,
  923. config=True,
  924. help=_('The session manager class to use.')
  925. )
  926. config_manager_class = Type(
  927. default_value=ConfigManager,
  928. config = True,
  929. help=_('The config manager class to use')
  930. )
  931. kernel_spec_manager = Instance(KernelSpecManager, allow_none=True)
  932. kernel_spec_manager_class = Type(
  933. default_value=KernelSpecManager,
  934. config=True,
  935. help="""
  936. The kernel spec manager class to use. Should be a subclass
  937. of `jupyter_client.kernelspec.KernelSpecManager`.
  938. The Api of KernelSpecManager is provisional and might change
  939. without warning between this version of Jupyter and the next stable one.
  940. """
  941. )
  942. login_handler_class = Type(
  943. default_value=LoginHandler,
  944. klass=web.RequestHandler,
  945. config=True,
  946. help=_('The login handler class to use.'),
  947. )
  948. logout_handler_class = Type(
  949. default_value=LogoutHandler,
  950. klass=web.RequestHandler,
  951. config=True,
  952. help=_('The logout handler class to use.'),
  953. )
  954. trust_xheaders = Bool(False, config=True,
  955. help=(_("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers"
  956. "sent by the upstream reverse proxy. Necessary if the proxy handles SSL"))
  957. )
  958. info_file = Unicode()
  959. @default('info_file')
  960. def _default_info_file(self):
  961. info_file = "nbserver-%s.json" % os.getpid()
  962. return os.path.join(self.runtime_dir, info_file)
  963. pylab = Unicode('disabled', config=True,
  964. help=_("""
  965. DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
  966. """)
  967. )
  968. @observe('pylab')
  969. def _update_pylab(self, change):
  970. """when --pylab is specified, display a warning and exit"""
  971. if change['new'] != 'warn':
  972. backend = ' %s' % change['new']
  973. else:
  974. backend = ''
  975. self.log.error(_("Support for specifying --pylab on the command line has been removed."))
  976. self.log.error(
  977. _("Please use `%pylab{0}` or `%matplotlib{0}` in the notebook itself.").format(backend)
  978. )
  979. self.exit(1)
  980. notebook_dir = Unicode(config=True,
  981. help=_("The directory to use for notebooks and kernels.")
  982. )
  983. @default('notebook_dir')
  984. def _default_notebook_dir(self):
  985. if self.file_to_run:
  986. return os.path.dirname(os.path.abspath(self.file_to_run))
  987. else:
  988. return py3compat.getcwd()
  989. @validate('notebook_dir')
  990. def _notebook_dir_validate(self, proposal):
  991. value = proposal['value']
  992. # Strip any trailing slashes
  993. # *except* if it's root
  994. _, path = os.path.splitdrive(value)
  995. if path == os.sep:
  996. return value
  997. value = value.rstrip(os.sep)
  998. if not os.path.isabs(value):
  999. # If we receive a non-absolute path, make it absolute.
  1000. value = os.path.abspath(value)
  1001. if not os.path.isdir(value):
  1002. raise TraitError(trans.gettext("No such notebook dir: '%r'") % value)
  1003. return value
  1004. @observe('notebook_dir')
  1005. def _update_notebook_dir(self, change):
  1006. """Do a bit of validation of the notebook dir."""
  1007. # setting App.notebook_dir implies setting notebook and kernel dirs as well
  1008. new = change['new']
  1009. self.config.FileContentsManager.root_dir = new
  1010. self.config.MappingKernelManager.root_dir = new
  1011. # TODO: Remove me in notebook 5.0
  1012. server_extensions = List(Unicode(), config=True,
  1013. help=(_("DEPRECATED use the nbserver_extensions dict instead"))
  1014. )
  1015. @observe('server_extensions')
  1016. def _update_server_extensions(self, change):
  1017. self.log.warning(_("server_extensions is deprecated, use nbserver_extensions"))
  1018. self.server_extensions = change['new']
  1019. nbserver_extensions = Dict({}, config=True,
  1020. help=(_("Dict of Python modules to load as notebook server extensions."
  1021. "Entry values can be used to enable and disable the loading of"
  1022. "the extensions. The extensions will be loaded in alphabetical "
  1023. "order."))
  1024. )
  1025. reraise_server_extension_failures = Bool(
  1026. False,
  1027. config=True,
  1028. help=_("Reraise exceptions encountered loading server extensions?"),
  1029. )
  1030. iopub_msg_rate_limit = Float(1000, config=True, help=_("""(msgs/sec)
  1031. Maximum rate at which messages can be sent on iopub before they are
  1032. limited."""))
  1033. iopub_data_rate_limit = Float(1000000, config=True, help=_("""(bytes/sec)
  1034. Maximum rate at which stream output can be sent on iopub before they are
  1035. limited."""))
  1036. rate_limit_window = Float(3, config=True, help=_("""(sec) Time window used to
  1037. check the message and data rate limits."""))
  1038. shutdown_no_activity_timeout = Integer(0, config=True,
  1039. help=("Shut down the server after N seconds with no kernels or "
  1040. "terminals running and no activity. "
  1041. "This can be used together with culling idle kernels "
  1042. "(MappingKernelManager.cull_idle_timeout) to "
  1043. "shutdown the notebook server when it's not in use. This is not "
  1044. "precisely timed: it may shut down up to a minute later. "
  1045. "0 (the default) disables this automatic shutdown.")
  1046. )
  1047. terminals_enabled = Bool(True, config=True,
  1048. help=_("""Set to False to disable terminals.
  1049. This does *not* make the notebook server more secure by itself.
  1050. Anything the user can in a terminal, they can also do in a notebook.
  1051. Terminals may also be automatically disabled if the terminado package
  1052. is not available.
  1053. """))
  1054. def parse_command_line(self, argv=None):
  1055. super(NotebookApp, self).parse_command_line(argv)
  1056. if self.extra_args:
  1057. arg0 = self.extra_args[0]
  1058. f = os.path.abspath(arg0)
  1059. self.argv.remove(arg0)
  1060. if not os.path.exists(f):
  1061. self.log.critical(_("No such file or directory: %s"), f)
  1062. self.exit(1)
  1063. # Use config here, to ensure that it takes higher priority than
  1064. # anything that comes from the config dirs.
  1065. c = Config()
  1066. if os.path.isdir(f):
  1067. c.NotebookApp.notebook_dir = f
  1068. elif os.path.isfile(f):
  1069. c.NotebookApp.file_to_run = f
  1070. self.update_config(c)
  1071. def init_configurables(self):
  1072. self.kernel_spec_manager = self.kernel_spec_manager_class(
  1073. parent=self,
  1074. )
  1075. self.kernel_manager = self.kernel_manager_class(
  1076. parent=self,
  1077. log=self.log,
  1078. connection_dir=self.runtime_dir,
  1079. kernel_spec_manager=self.kernel_spec_manager,
  1080. )
  1081. self.contents_manager = self.contents_manager_class(
  1082. parent=self,
  1083. log=self.log,
  1084. )
  1085. self.session_manager = self.session_manager_class(
  1086. parent=self,
  1087. log=self.log,
  1088. kernel_manager=self.kernel_manager,
  1089. contents_manager=self.contents_manager,
  1090. )
  1091. self.config_manager = self.config_manager_class(
  1092. parent=self,
  1093. log=self.log,
  1094. )
  1095. def init_logging(self):
  1096. # This prevents double log messages because tornado use a root logger that
  1097. # self.log is a child of. The logging module dipatches log messages to a log
  1098. # and all of its ancenstors until propagate is set to False.
  1099. self.log.propagate = False
  1100. for log in app_log, access_log, gen_log:
  1101. # consistent log output name (NotebookApp instead of tornado.access, etc.)
  1102. log.name = self.log.name
  1103. # hook up tornado 3's loggers to our app handlers
  1104. logger = logging.getLogger('tornado')
  1105. logger.propagate = True
  1106. logger.parent = self.log
  1107. logger.setLevel(self.log.level)
  1108. def init_webapp(self):
  1109. """initialize tornado webapp and httpserver"""
  1110. self.tornado_settings['allow_origin'] = self.allow_origin
  1111. self.tornado_settings['websocket_compression_options'] = self.websocket_compression_options
  1112. if self.allow_origin_pat:
  1113. self.tornado_settings['allow_origin_pat'] = re.compile(self.allow_origin_pat)
  1114. self.tornado_settings['allow_credentials'] = self.allow_credentials
  1115. self.tornado_settings['cookie_options'] = self.cookie_options
  1116. self.tornado_settings['get_secure_cookie_kwargs'] = self.get_secure_cookie_kwargs
  1117. self.tornado_settings['token'] = self.token
  1118. if (self.open_browser or self.file_to_run) and not self.password:
  1119. self.one_time_token = binascii.hexlify(os.urandom(24)).decode('ascii')
  1120. self.tornado_settings['one_time_token'] = self.one_time_token
  1121. # ensure default_url starts with base_url
  1122. if not self.default_url.startswith(self.base_url):
  1123. self.default_url = url_path_join(self.base_url, self.default_url)
  1124. if self.password_required and (not self.password):
  1125. self.log.critical(_("Notebook servers are configured to only be run with a password."))
  1126. self.log.critical(_("Hint: run the following command to set a password"))
  1127. self.log.critical(_("\t$ python -m notebook.auth password"))
  1128. sys.exit(1)
  1129. self.web_app = NotebookWebApplication(
  1130. self, self.kernel_manager, self.contents_manager,
  1131. self.session_manager, self.kernel_spec_manager,
  1132. self.config_manager, self.extra_services,
  1133. self.log, self.base_url, self.default_url, self.tornado_settings,
  1134. self.jinja_environment_options,
  1135. )
  1136. ssl_options = self.ssl_options
  1137. if self.certfile:
  1138. ssl_options['certfile'] = self.certfile
  1139. if self.keyfile:
  1140. ssl_options['keyfile'] = self.keyfile
  1141. if self.client_ca:
  1142. ssl_options['ca_certs'] = self.client_ca
  1143. if not ssl_options:
  1144. # None indicates no SSL config
  1145. ssl_options = None
  1146. else:
  1147. # SSL may be missing, so only import it if it's to be used
  1148. import ssl
  1149. # Disable SSLv3 by default, since its use is discouraged.
  1150. ssl_options.setdefault('ssl_version', ssl.PROTOCOL_TLSv1)
  1151. if ssl_options.get('ca_certs', False):
  1152. ssl_options.setdefault('cert_reqs', ssl.CERT_REQUIRED)
  1153. self.login_handler_class.validate_security(self, ssl_options=ssl_options)
  1154. self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options,
  1155. xheaders=self.trust_xheaders,
  1156. max_body_size=self.max_body_size,
  1157. max_buffer_size=self.max_buffer_size)
  1158. success = None
  1159. for port in random_ports(self.port, self.port_retries+1):
  1160. try:
  1161. self.http_server.listen(port, self.ip)
  1162. except socket.error as e:
  1163. if e.errno == errno.EADDRINUSE:
  1164. self.log.info(_('The port %i is already in use, trying another port.') % port)
  1165. continue
  1166. elif e.errno in (errno.EACCES, getattr(errno, 'WSAEACCES', errno.EACCES)):
  1167. self.log.warning(_("Permission to listen on port %i denied") % port)
  1168. continue
  1169. else:
  1170. raise
  1171. else:
  1172. self.port = port
  1173. success = True
  1174. break
  1175. if not success:
  1176. self.log.critical(_('ERROR: the notebook server could not be started because '
  1177. 'no available port could be found.'))
  1178. self.exit(1)
  1179. @property
  1180. def display_url(self):
  1181. if self.custom_display_url:
  1182. url = self.custom_display_url
  1183. if not url.endswith('/'):
  1184. url += '/'
  1185. else:
  1186. if self.ip in ('', '0.0.0.0'):
  1187. ip = "(%s or 127.0.0.1)" % socket.gethostname()
  1188. else:
  1189. ip = self.ip
  1190. url = self._url(ip)
  1191. if self.token:
  1192. # Don't log full token if it came from config
  1193. token = self.token if self._token_generated else '...'
  1194. url = url_concat(url, {'token': token})
  1195. return url
  1196. @property
  1197. def connection_url(self):
  1198. ip = self.ip if self.ip else 'localhost'
  1199. return self._url(ip)
  1200. def _url(self, ip):
  1201. proto = 'https' if self.certfile else 'http'
  1202. return "%s://%s:%i%s" % (proto, ip, self.port, self.base_url)
  1203. def init_terminals(self):
  1204. if not self.terminals_enabled:
  1205. return
  1206. try:
  1207. from .terminal import initialize
  1208. initialize(self.web_app, self.notebook_dir, self.connection_url, self.terminado_settings)
  1209. self.web_app.settings['terminals_available'] = True
  1210. except ImportError as e:
  1211. self.log.warning(_("Terminals not available (error was %s)"), e)
  1212. def init_signal(self):
  1213. if not sys.platform.startswith('win') and sys.stdin and sys.stdin.isatty():
  1214. signal.signal(signal.SIGINT, self._handle_sigint)
  1215. signal.signal(signal.SIGTERM, self._signal_stop)
  1216. if hasattr(signal, 'SIGUSR1'):
  1217. # Windows doesn't support SIGUSR1
  1218. signal.signal(signal.SIGUSR1, self._signal_info)
  1219. if hasattr(signal, 'SIGINFO'):
  1220. # only on BSD-based systems
  1221. signal.signal(signal.SIGINFO, self._signal_info)
  1222. def _handle_sigint(self, sig, frame):
  1223. """SIGINT handler spawns confirmation dialog"""
  1224. # register more forceful signal handler for ^C^C case
  1225. signal.signal(signal.SIGINT, self._signal_stop)
  1226. # request confirmation dialog in bg thread, to avoid
  1227. # blocking the App
  1228. thread = threading.Thread(target=self._confirm_exit)
  1229. thread.daemon = True
  1230. thread.start()
  1231. def _restore_sigint_handler(self):
  1232. """callback for restoring original SIGINT handler"""
  1233. signal.signal(signal.SIGINT, self._handle_sigint)
  1234. def _confirm_exit(self):
  1235. """confirm shutdown on ^C
  1236. A second ^C, or answering 'y' within 5s will cause shutdown,
  1237. otherwise original SIGINT handler will be restored.
  1238. This doesn't work on Windows.
  1239. """
  1240. info = self.log.info
  1241. info(_('interrupted'))
  1242. print(self.notebook_info())
  1243. yes = _('y')
  1244. no = _('n')
  1245. sys.stdout.write(_("Shutdown this notebook server (%s/[%s])? ") % (yes, no))
  1246. sys.stdout.flush()
  1247. r,w,x = select.select([sys.stdin], [], [], 5)
  1248. if r:
  1249. line = sys.stdin.readline()
  1250. if line.lower().startswith(yes) and no not in line.lower():
  1251. self.log.critical(_("Shutdown confirmed"))
  1252. # schedule stop on the main thread,
  1253. # since this might be called from a signal handler
  1254. self.io_loop.add_callback_from_signal(self.io_loop.stop)
  1255. return
  1256. else:
  1257. print(_("No answer for 5s:"), end=' ')
  1258. print(_("resuming operation..."))
  1259. # no answer, or answer is no:
  1260. # set it back to original SIGINT handler
  1261. # use IOLoop.add_callback because signal.signal must be called
  1262. # from main thread
  1263. self.io_loop.add_callback_from_signal(self._restore_sigint_handler)
  1264. def _signal_stop(self, sig, frame):
  1265. self.log.critical(_("received signal %s, stopping"), sig)
  1266. self.io_loop.add_callback_from_signal(self.io_loop.stop)
  1267. def _signal_info(self, sig, frame):
  1268. print(self.notebook_info())
  1269. def init_components(self):
  1270. """Check the components submodule, and warn if it's unclean"""
  1271. # TODO: this should still check, but now we use bower, not git submodule
  1272. pass
  1273. def init_server_extensions(self):
  1274. """Load any extensions specified by config.
  1275. Import the module, then call the load_jupyter_server_extension function,
  1276. if one exists.
  1277. The extension API is experimental, and may change in future releases.
  1278. """
  1279. # TODO: Remove me in notebook 5.0
  1280. for modulename in self.server_extensions:
  1281. # Don't override disable state of the extension if it already exist
  1282. # in the new traitlet
  1283. if not modulename in self.nbserver_extensions:
  1284. self.nbserver_extensions[modulename] = True
  1285. # Load server extensions with ConfigManager.
  1286. # This enables merging on keys, which we want for extension enabling.
  1287. # Regular config loading only merges at the class level,
  1288. # so each level (user > env > system) clobbers the previous.
  1289. config_path = jupyter_config_path()
  1290. if self.config_dir not in config_path:
  1291. # add self.config_dir to the front, if set manually
  1292. config_path.insert(0, self.config_dir)
  1293. manager = ConfigManager(read_config_path=config_path)
  1294. section = manager.get(self.config_file_name)
  1295. extensions = section.get('NotebookApp', {}).get('nbserver_extensions', {})
  1296. for modulename, enabled in self.nbserver_extensions.items():
  1297. if modulename not in extensions:
  1298. # not present in `extensions` means it comes from Python config,
  1299. # so we need to add it.
  1300. # Otherwise, trust ConfigManager to have loaded it.
  1301. extensions[modulename] = enabled
  1302. for modulename, enabled in sorted(extensions.items()):
  1303. if enabled:
  1304. try:
  1305. mod = importlib.import_module(modulename)
  1306. func = getattr(mod, 'load_jupyter_server_extension', None)
  1307. if func is not None:
  1308. func(self)
  1309. except Exception:
  1310. if self.reraise_server_extension_failures:
  1311. raise
  1312. self.log.warning(_("Error loading server extension %s"), modulename,
  1313. exc_info=True)
  1314. def init_mime_overrides(self):
  1315. # On some Windows machines, an application has registered an incorrect
  1316. # mimetype for CSS in the registry. Tornado uses this when serving
  1317. # .css files, causing browsers to reject the stylesheet. We know the
  1318. # mimetype always needs to be text/css, so we override it here.
  1319. mimetypes.add_type('text/css', '.css')
  1320. def shutdown_no_activity(self):
  1321. """Shutdown server on timeout when there are no kernels or terminals."""
  1322. km = self.kernel_manager
  1323. if len(km) != 0:
  1324. return # Kernels still running
  1325. try:
  1326. term_mgr = self.web_app.settings['terminal_manager']
  1327. except KeyError:
  1328. pass # Terminals not enabled
  1329. else:
  1330. if term_mgr.terminals:
  1331. return # Terminals still running
  1332. seconds_since_active = \
  1333. (utcnow() - self.web_app.last_activity()).total_seconds()
  1334. self.log.debug("No activity for %d seconds.",
  1335. seconds_since_active)
  1336. if seconds_since_active > self.shutdown_no_activity_timeout:
  1337. self.log.info("No kernels or terminals for %d seconds; shutting down.",
  1338. seconds_since_active)
  1339. self.stop()
  1340. def init_shutdown_no_activity(self):
  1341. if self.shutdown_no_activity_timeout > 0:
  1342. self.log.info("Will shut down after %d seconds with no kernels or terminals.",
  1343. self.shutdown_no_activity_timeout)
  1344. pc = ioloop.PeriodicCallback(self.shutdown_no_activity, 60000)
  1345. pc.start()
  1346. @catch_config_error
  1347. def initialize(self, argv=None):
  1348. super(NotebookApp, self).initialize(argv)
  1349. self.init_logging()
  1350. if self._dispatching:
  1351. return
  1352. self.init_configurables()
  1353. self.init_components()
  1354. self.init_webapp()
  1355. self.init_terminals()
  1356. self.init_signal()
  1357. self.init_server_extensions()
  1358. self.init_mime_overrides()
  1359. self.init_shutdown_no_activity()
  1360. def cleanup_kernels(self):
  1361. """Shutdown all kernels.
  1362. The kernels will shutdown themselves when this process no longer exists,
  1363. but explicit shutdown allows the KernelManagers to cleanup the connection files.
  1364. """
  1365. n_kernels = len(self.kernel_manager.list_kernel_ids())
  1366. kernel_msg = trans.ngettext('Shutting down %d kernel', 'Shutting down %d kernels', n_kernels)
  1367. self.log.info(kernel_msg % n_kernels)
  1368. self.kernel_manager.shutdown_all()
  1369. def notebook_info(self, kernel_count=True):
  1370. "Return the current working directory and the server url information"
  1371. info = self.contents_manager.info_string() + "\n"
  1372. if kernel_count:
  1373. n_kernels = len(self.kernel_manager.list_kernel_ids())
  1374. kernel_msg = trans.ngettext("%d active kernel", "%d active kernels", n_kernels)
  1375. info += kernel_msg % n_kernels
  1376. info += "\n"
  1377. # Format the info so that the URL fits on a single line in 80 char display
  1378. info += _("The Jupyter Notebook is running at:\n%s") % self.display_url
  1379. return info
  1380. def server_info(self):
  1381. """Return a JSONable dict of information about this server."""
  1382. return {'url': self.connection_url,
  1383. 'hostname': self.ip if self.ip else 'localhost',
  1384. 'port': self.port,
  1385. 'secure': bool(self.certfile),
  1386. 'base_url': self.base_url,
  1387. 'token': self.token,
  1388. 'notebook_dir': os.path.abspath(self.notebook_dir),
  1389. 'password': bool(self.password),
  1390. 'pid': os.getpid(),
  1391. }
  1392. def write_server_info_file(self):
  1393. """Write the result of server_info() to the JSON file info_file."""
  1394. try:
  1395. with open(self.info_file, 'w') as f:
  1396. json.dump(self.server_info(), f, indent=2, sort_keys=True)
  1397. except OSError as e:
  1398. self.log.error(_("Failed to write server-info to %s: %s"),
  1399. self.info_file, e)
  1400. def remove_server_info_file(self):
  1401. """Remove the nbserver-<pid>.json file created for this server.
  1402. Ignores the error raised when the file has already been removed.
  1403. """
  1404. try:
  1405. os.unlink(self.info_file)
  1406. except OSError as e:
  1407. if e.errno != errno.ENOENT:
  1408. raise
  1409. def start(self):
  1410. """ Start the Notebook server app, after initialization
  1411. This method takes no arguments so all configuration and initialization
  1412. must be done prior to calling this method."""
  1413. super(NotebookApp, self).start()
  1414. if not self.allow_root:
  1415. # check if we are running as root, and abort if it's not allowed
  1416. try:
  1417. uid = os.geteuid()
  1418. except AttributeError:
  1419. uid = -1 # anything nonzero here, since we can't check UID assume non-root
  1420. if uid == 0:
  1421. self.log.critical(_("Running as root is not recommended. Use --allow-root to bypass."))
  1422. self.exit(1)
  1423. info = self.log.info
  1424. for line in self.notebook_info(kernel_count=False).split("\n"):
  1425. info(line)
  1426. info(_("Use Control-C to stop this server and shut down all kernels (twice to skip confirmation)."))
  1427. if 'dev' in notebook.__version__:
  1428. info(_("Welcome to Project Jupyter! Explore the various tools available"
  1429. " and their corresponding documentation. If you are interested"
  1430. " in contributing to the platform, please visit the community"
  1431. "resources section at https://jupyter.org/community.html."))
  1432. self.write_server_info_file()
  1433. if self.open_browser or self.file_to_run:
  1434. try:
  1435. browser = webbrowser.get(self.browser or None)
  1436. except webbrowser.Error as e:
  1437. self.log.warning(_('No web browser found: %s.') % e)
  1438. browser = None
  1439. if self.file_to_run:
  1440. if not os.path.exists(self.file_to_run):
  1441. self.log.critical(_("%s does not exist") % self.file_to_run)
  1442. self.exit(1)
  1443. relpath = os.path.relpath(self.file_to_run, self.notebook_dir)
  1444. uri = url_escape(url_path_join('notebooks', *relpath.split(os.sep)))
  1445. else:
  1446. # default_url contains base_url, but so does connection_url
  1447. uri = self.default_url[len(self.base_url):]
  1448. if self.one_time_token:
  1449. uri = url_concat(uri, {'token': self.one_time_token})
  1450. if browser:
  1451. b = lambda : browser.open(url_path_join(self.connection_url, uri),
  1452. new=self.webbrowser_open_new)
  1453. threading.Thread(target=b).start()
  1454. if self.token and self._token_generated:
  1455. # log full URL with generated token, so there's a copy/pasteable link
  1456. # with auth info.
  1457. self.log.critical('\n'.join([
  1458. '\n',
  1459. 'Copy/paste this URL into your browser when you connect for the first time,',
  1460. 'to login with a token:',
  1461. ' %s' % self.display_url,
  1462. ]))
  1463. self.io_loop = ioloop.IOLoop.current()
  1464. if sys.platform.startswith('win'):
  1465. # add no-op to wake every 5s
  1466. # to handle signals that may be ignored by the inner loop
  1467. pc = ioloop.PeriodicCallback(lambda : None, 5000)
  1468. pc.start()
  1469. try:
  1470. self.io_loop.start()
  1471. except KeyboardInterrupt:
  1472. info(_("Interrupted..."))
  1473. finally:
  1474. self.remove_server_info_file()
  1475. self.cleanup_kernels()
  1476. def stop(self):
  1477. def _stop():
  1478. self.http_server.stop()
  1479. self.io_loop.stop()
  1480. self.io_loop.add_callback(_stop)
  1481. def list_running_servers(runtime_dir=None):
  1482. """Iterate over the server info files of running notebook servers.
  1483. Given a runtime directory, find nbserver-* files in the security directory,
  1484. and yield dicts of their information, each one pertaining to
  1485. a currently running notebook server instance.
  1486. """
  1487. if runtime_dir is None:
  1488. runtime_dir = jupyter_runtime_dir()
  1489. # The runtime dir might not exist
  1490. if not os.path.isdir(runtime_dir):
  1491. return
  1492. for file_name in os.listdir(runtime_dir):
  1493. if file_name.startswith('nbserver-'):
  1494. with io.open(os.path.join(runtime_dir, file_name), encoding='utf-8') as f:
  1495. info = json.load(f)
  1496. # Simple check whether that process is really still running
  1497. # Also remove leftover files from IPython 2.x without a pid field
  1498. if ('pid' in info) and check_pid(info['pid']):
  1499. yield info
  1500. else:
  1501. # If the process has died, try to delete its info file
  1502. try:
  1503. os.unlink(os.path.join(runtime_dir, file_name))
  1504. except OSError:
  1505. pass # TODO: This should warn or log or something
  1506. #-----------------------------------------------------------------------------
  1507. # Main entry point
  1508. #-----------------------------------------------------------------------------
  1509. main = launch_new_instance = NotebookApp.launch_instance