Skip to content

Commit a45c373

Browse files
committed
added --autoreload flag
When passed, the webapp will watch for any changes to its Python source. On change, all changed packages will be reimported and the webapp will restart. Also works on Python source files in Jupyter server extensions. Implemented using the built in `autoreload` parameter in the constructor of `tornado.web.Application`.
1 parent e498de6 commit a45c373

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

notebook/notebookapp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def __init__(self, jupyter_app, kernel_manager, contents_manager,
160160
default_url, settings_overrides, jinja_env_options)
161161
handlers = self.init_handlers(settings)
162162

163+
if settings['autoreload']:
164+
log.info('Autoreload enabled: the webapp will restart when any Python src file changes.')
165+
163166
super(NotebookWebApplication, self).__init__(handlers, **settings)
164167

165168
def init_settings(self, jupyter_app, kernel_manager, contents_manager,
@@ -542,6 +545,16 @@ def start(self):
542545
_("Allow the notebook to be run from root user.")
543546
)
544547

548+
flags['autoreload'] = (
549+
{'NotebookApp': {'autoreload': True}},
550+
"""Autoreload the webapp
551+
552+
Enable reloading of the tornado webapp and all imported Python packages
553+
when any changes are made to any Python src files in Notebook or
554+
extensions.
555+
"""
556+
)
557+
545558
# Add notebook manager flags
546559
flags.update(boolean_flag('script', 'FileContentsManager.save_script',
547560
'DEPRECATED, IGNORED',
@@ -648,6 +661,10 @@ def _default_log_format(self):
648661
help=_("Whether to allow the user to run the notebook as root.")
649662
)
650663

664+
autoreload = Bool(False, config=True,
665+
help= ("Reload the webapp when changes are made to any Python src files.")
666+
)
667+
651668
default_url = Unicode('/tree', config=True,
652669
help=_("The default URL to redirect to from `/`")
653670
)
@@ -1386,6 +1403,7 @@ def init_webapp(self):
13861403
if self.allow_origin_pat:
13871404
self.tornado_settings['allow_origin_pat'] = re.compile(self.allow_origin_pat)
13881405
self.tornado_settings['allow_credentials'] = self.allow_credentials
1406+
self.tornado_settings['autoreload'] = self.autoreload
13891407
self.tornado_settings['cookie_options'] = self.cookie_options
13901408
self.tornado_settings['get_secure_cookie_kwargs'] = self.get_secure_cookie_kwargs
13911409
self.tornado_settings['token'] = self.token

0 commit comments

Comments
 (0)