Skip to content

Commit

Permalink
Use server-mode option to expose types of servers
Browse files Browse the repository at this point in the history
These changes use a new option `--server-mode` to indicate which mode
the server will run in.  The available modes are 'notebooks' (default),
'kernels' and 'contents'.   The current behavior is exhibited with
'notebooks'.  However, when either 'kernels' or 'contents' are indicated,
only the services (REST API) relative to those modes will be available.

Because all modes are implemented in the same class, this approache (vs.
subclassing) shouldn't side-affect the ability for server extensions to
be written against the various modes.
  • Loading branch information
kevin-bates committed Feb 3, 2020
1 parent 7a6cd8e commit f7d1638
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions jupyter_server/gateway/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def _url_default(self):

@validate('url')
def _url_validate(self, proposal):
if self.parent.server_mode != 'notebooks':
raise TraitError("GatewayClient url (--gateway-url) can only be specified when server-mode is 'notebooks'!")
value = proposal['value']
# Ensure value, if present, starts with 'http'
if value is not None and len(value) > 0:
Expand Down
71 changes: 66 additions & 5 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ def start(self):
'browser': 'ServerApp.browser',
'pylab': 'ServerApp.pylab',
'gateway-url': 'GatewayClient.url',
'server-mode': 'ServerApp.server_mode',
})

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1208,6 +1209,58 @@ def _update_server_extensions(self, change):
is not available.
"""))

supported_server_modes = ('notebooks', 'kernels', 'contents')

server_mode = Unicode('notebooks', config=True,
help=_("The mode of the server. Must be one of {}.".format(supported_server_modes))
)

@default('server_mode')
def _default_server_mode(self):
return 'notebooks'

@validate('server_mode')
def _valdate_server_mode(self, proposal):
value = proposal['value']
if value in ServerApp.supported_server_modes:
return value
raise TraitError(trans.gettext("The mode of the server must be one of {}.".
format(ServerApp.supported_server_modes)))

def is_serving_kernels(self):
return self.server_mode == 'notebooks' or self.server_mode == 'kernels'

def is_serving_contents(self):
return self.server_mode == 'notebooks' or self.server_mode == 'contents'

def get_services(self):

if self.server_mode == 'kernels':
return (
'api',
'auth',
'config',
'kernels',
'kernelspecs',
'security',
'shutdown',
)

if self.server_mode == 'contents':
return (
'api',
'auth',
'config',
'contents',
'edit',
'files',
'nbconvert',
'security',
'shutdown',
'view'
)
return ServerApp.default_services

def parse_command_line(self, argv=None):

super(ServerApp, self).parse_command_line(argv)
Expand Down Expand Up @@ -1301,7 +1354,7 @@ def init_webapp(self):
sys.exit(1)

self.web_app = ServerWebApplication(
self, self.default_services, self.kernel_manager, self.contents_manager,
self, self.get_services(), self.kernel_manager, self.contents_manager,
self.session_manager, self.kernel_spec_manager,
self.config_manager, self.extra_services,
self.log, self.base_url, self.default_url, self.tornado_settings,
Expand Down Expand Up @@ -1684,17 +1737,26 @@ def cleanup_kernels(self):

def running_server_info(self, kernel_count=True):
"Return the current working directory and the server url information"
info = self.contents_manager.info_string() + "\n"
info = ""
if self.is_serving_contents():
info += self.contents_manager.info_string() + "\n"
if kernel_count:
n_kernels = len(self.kernel_manager.list_kernel_ids())
kernel_msg = trans.ngettext("%d active kernel", "%d active kernels", n_kernels)
info += kernel_msg % n_kernels
info += "\n"
# Format the info so that the URL fits on a single line in 80 char display
info += _("Jupyter Server {version} is running at:\n{url}".
format(version=ServerApp.version, url=self.display_url))
info += _("Jupyter Server {version} is serving {mode} at:\n{url}".
format(version=ServerApp.version, mode=self.server_mode, url=self.display_url))
if self.gateway_config.gateway_enabled:
info += _("\nKernels will be managed by the Gateway server running at:\n%s") % self.gateway_config.url

kernels_clause = ""
if self.is_serving_kernels():
kernels_clause += "and shut down all kernels "
info += _("\nUse Control-C to stop this server {kernels_clause}(twice to skip confirmation).".
format(kernels_clause=kernels_clause))

return info

def server_info(self):
Expand Down Expand Up @@ -1808,7 +1870,6 @@ def start_app(self):
info = self.log.info
for line in self.running_server_info(kernel_count=False).split("\n"):
info(line)
info(_("Use Control-C to stop this server and shut down all kernels (twice to skip confirmation)."))
if 'dev' in jupyter_server.__version__:
info(_("Welcome to Project Jupyter! Explore the various tools available"
" and their corresponding documentation. If you are interested"
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def rename_file(self, old_path, new_path):
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))

def info_string(self):
return _("Serving notebooks from local directory: %s") % self.root_dir
return _("Serving contents from local directory: %s") % self.root_dir

def get_kernel_path(self, path, model=None):