notebookapp.py 70 KB

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