Skip to content

Commit

Permalink
Add StandaloneApp as a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
vidartf committed Aug 15, 2019
1 parent ab003db commit 8867d42
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
54 changes: 38 additions & 16 deletions jupyter_server/extension/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ def _preparse_command_line(Application):
app.exit(0)


class ExtensionApp(JupyterApp):

class StandaloneApp(JupyterApp):
"""Base class for configurable Jupyter Server Extension Applications.
ExtensionApp subclasses can be initialized two ways:
1. Extension is listed as a jpserver_extension, and ServerApp calls
its load_jupyter_server_extension classmethod. This is the
classic way of loading a server extension.
2. Extension is launched directly by calling its `launch_instance`
class method. This method can be set as a entry_point in
the extensions setup.py
StandaloneApp subclasses are launched directly by calling its
`launch_instance` class method. This method can be set as an
entry_point in the extensions setup.py
"""

standalone = True

# Name of the extension
extension_name = Unicode(
"",
Expand All @@ -88,11 +88,11 @@ def _validate_extension_name(self):
value = self.extension_name
if isinstance(value, str):
# Validate that extension_name doesn't contain any invalid characters.
for c in ExtensionApp.INVALID_EXTENSION_NAME_CHARS:
for c in StandaloneApp.INVALID_EXTENSION_NAME_CHARS:
if c in value:
raise ValueError("Extension name '{name}' cannot contain any of the following characters: "
"{invalid_chars}.".
format(name=value, invalid_chars=ExtensionApp.INVALID_EXTENSION_NAME_CHARS))
format(name=value, invalid_chars=StandaloneApp.INVALID_EXTENSION_NAME_CHARS))
return value
raise ValueError("Extension name must be a string, found {type}.".format(type=type(value)))

Expand Down Expand Up @@ -219,15 +219,15 @@ def _prepare_templates(self):
})
self.initialize_templates()

@staticmethod
def initialize_server(argv=[], **kwargs):
@classmethod
def initialize_server(cls, argv=[], **kwargs):
"""Get an instance of the Jupyter Server."""
# Get a jupyter server instance
serverapp = ServerApp(**kwargs)
# Initialize ServerApp config.
# Parses the command line looking for
# ServerApp configuration.
serverapp.initialize(argv=argv)
serverapp.initialize(argv=argv, load_extensions=cls.standalone is False)
return serverapp

def initialize(self, serverapp, argv=[]):
Expand All @@ -242,7 +242,7 @@ def initialize(self, serverapp, argv=[]):
"""
self._validate_extension_name()
# Initialize the extension application
super(ExtensionApp, self).initialize(argv=argv)
super(StandaloneApp, self).initialize(argv=argv)
self.serverapp = serverapp

# Initialize config, settings, templates, and handlers.
Expand Down Expand Up @@ -272,8 +272,8 @@ def start_server(self, **kwargs):
self.serverapp.start(**kwargs)

@classmethod
def load_jupyter_server_extension(cls, serverapp, argv=[], **kwargs):
"""Initialize and configure this extension, then add the extension's
def _load_app(cls, serverapp, argv=[], **kwargs):
"""Initialize and configure this app, then add the apps's
settings and handlers to the server's web application.
"""
# Configure and initialize extension.
Expand Down Expand Up @@ -321,3 +321,25 @@ def launch_instance(cls, argv=None, **kwargs):
# Start the ioloop.
extension.start_server()


class ExtensionApp(StandaloneApp):
"""Base class for configurable Jupyter Server Extension Applications.
ExtensionApp subclasses can be initialized two ways:
1. Extension is listed as a jpserver_extension, and ServerApp calls
its load_jupyter_server_extension classmethod. This is the
classic way of loading a server extension.
2. Extension is launched directly by calling its `launch_instance`
class method. This method can be set as a entry_point in
the extensions setup.py
"""

standalone = False

@classmethod
def load_jupyter_server_extension(cls, serverapp, argv=[], **kwargs):
"""Initialize and configure this extension, then add the extension's
settings and handlers to the server's web application.
"""
# Configure and initialize extension.
return cls._load_app(serverapp, argv=argv, **kwargs)
15 changes: 2 additions & 13 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,6 @@ def start(self):
_("Allow the server to be run from root user.")
)

flags['standalone']=(
{'ServerApp' : {'standalone' : True}},
_("Run the server without enabling extensions.")
)

# Add notebook manager flags
flags.update(boolean_flag('script', 'FileContentsManager.save_script',
'DEPRECATED, IGNORED',
Expand Down Expand Up @@ -1143,12 +1138,6 @@ def _update_server_extensions(self, change):
is not available.
"""))

standalone = Bool(
False,
config=True,
help="Run the server without enabling extensions."
)

def parse_command_line(self, argv=None):
super(ServerApp, self).parse_command_line(argv)

Expand Down Expand Up @@ -1470,7 +1459,7 @@ def init_shutdown_no_activity(self):
pc.start()

@catch_config_error
def initialize(self, argv=None):
def initialize(self, argv=None, load_extensions=True):
super(ServerApp, self).initialize(argv)
self.init_logging()
if self._dispatching:
Expand All @@ -1480,7 +1469,7 @@ def initialize(self, argv=None):
self.init_webapp()
self.init_terminals()
self.init_signal()
if self.standalone is False:
if load_extensions is True:
self.init_server_extensions()
self.init_mime_overrides()
self.init_shutdown_no_activity()
Expand Down

0 comments on commit 8867d42

Please sign in to comment.