44
55from jinja2 import Environment , FileSystemLoader
66
7+ from traitlets .config import Config
78from traitlets import (
89 HasTraits ,
910 Unicode ,
1213 Bool ,
1314 default
1415)
15- from traitlets .config import Config
1616from tornado .log import LogFormatter
1717from tornado .web import RedirectHandler
1818
@@ -186,6 +186,9 @@ def get_extension_point(cls):
186186 def _default_url (self ):
187187 return self .extension_url
188188
189+ # Is this linked to a serverapp yet?
190+ _linked = Bool (False )
191+
189192 # Extension can configure the ServerApp from the command-line
190193 classes = [
191194 ServerApp ,
@@ -196,9 +199,6 @@ def _default_url(self):
196199
197200 _log_formatter_cls = LogFormatter
198201
199- # Whether this app is the starter app
200- _is_starter_app = False
201-
202202 @default ('log_level' )
203203 def _default_log_level (self ):
204204 return logging .INFO
@@ -333,14 +333,14 @@ def _prepare_templates(self):
333333 })
334334 self .initialize_templates ()
335335
336- @classmethod
337- def _jupyter_server_config (cls ):
336+ def _jupyter_server_config (self ):
338337 base_config = {
339338 "ServerApp" : {
340- "jpserver_extensions" : {cls .get_extension_package (): True },
339+ "default_url" : self .default_url ,
340+ "open_browser" : self .open_browser
341341 }
342342 }
343- base_config ["ServerApp" ].update (cls .serverapp_config )
343+ base_config ["ServerApp" ].update (self .serverapp_config )
344344 return base_config
345345
346346 def _link_jupyter_server_extension (self , serverapp ):
@@ -351,6 +351,10 @@ def _link_jupyter_server_extension(self, serverapp):
351351 the command line contains traits for the ExtensionApp
352352 or the ExtensionApp's config files have server
353353 settings.
354+
355+ Note, the ServerApp has not initialized the Tornado
356+ Web Application yet, so do not try to affect the
357+ `web_app` attribute.
354358 """
355359 self .serverapp = serverapp
356360 # Load config from an ExtensionApp's config files.
@@ -370,23 +374,8 @@ def _link_jupyter_server_extension(self, serverapp):
370374 # ServerApp, do it here.
371375 # i.e. ServerApp traits <--- ExtensionApp config
372376 self .serverapp .update_config (self .config )
373-
374- @classmethod
375- def initialize_server (cls , argv = [], load_other_extensions = True , ** kwargs ):
376- """Creates an instance of ServerApp where this extension is enabled
377- (superceding disabling found in other config from files).
378-
379- This is necessary when launching the ExtensionApp directly from
380- the `launch_instance` classmethod.
381- """
382- # The ExtensionApp needs to add itself as enabled extension
383- # to the jpserver_extensions trait, so that the ServerApp
384- # initializes it.
385- config = Config (cls ._jupyter_server_config ())
386- serverapp = ServerApp .instance (** kwargs , argv = [], config = config )
387- cls ._is_starter_app = True
388- serverapp .initialize (argv = argv , find_extensions = load_other_extensions )
389- return serverapp
377+ # Acknowledge that this extension has been linked.
378+ self ._linked = True
390379
391380 def initialize (self ):
392381 """Initialize the extension app. The
@@ -440,12 +429,7 @@ def _load_jupyter_server_extension(cls, serverapp):
440429 except KeyError :
441430 extension = cls ()
442431 extension ._link_jupyter_server_extension (serverapp )
443- if cls ._is_starter_app :
444- serverapp ._starter_app = extension
445432 extension .initialize ()
446- # Set the serverapp's default url to the extension's url.
447- if cls ._is_starter_app :
448- serverapp .default_url = extension .default_url
449433 return extension
450434
451435 @classmethod
@@ -478,6 +462,24 @@ def load_classic_server_extension(cls, serverapp):
478462 ])
479463 extension .initialize ()
480464
465+ @classmethod
466+ def initialize_server (cls , argv = [], load_other_extensions = True , ** kwargs ):
467+ """Creates an instance of ServerApp and explicitly sets
468+ this extension to enabled=True (i.e. superceding disabling
469+ found in other config from files).
470+
471+ The `launch_instance` method uses this method to initialize
472+ and start a server.
473+ """
474+ serverapp = ServerApp .instance (
475+ jpserver_extensions = {cls .get_extension_package (): True }, ** kwargs )
476+ serverapp .initialize (
477+ argv = argv ,
478+ starter_extension = cls .name ,
479+ find_extensions = cls .load_other_extensions ,
480+ )
481+ return serverapp
482+
481483 @classmethod
482484 def launch_instance (cls , argv = None , ** kwargs ):
483485 """Launch the extension like an application. Initializes+configs a stock server
@@ -489,27 +491,29 @@ def launch_instance(cls, argv=None, **kwargs):
489491 args = sys .argv [1 :] # slice out extension config.
490492 else :
491493 args = argv
492- # Check for subcommands
494+
495+ # Handle all "stops" that could happen before
496+ # continuing to launch a server+extension.
493497 subapp = _preparse_for_subcommand (cls , args )
494498 if subapp :
495499 subapp .start ()
496- else :
497- # Check for help, version, and generate-config arguments
498- # before initializing server to make sure these
499- # arguments trigger actions from the extension not the server.
500- _preparse_for_stopping_flags (cls , args )
501- # Get a jupyter server instance.
502- serverapp = cls .initialize_server (
503- argv = args ,
504- load_other_extensions = cls .load_other_extensions
500+ return
501+
502+ # Check for help, version, and generate-config arguments
503+ # before initializing server to make sure these
504+ # arguments trigger actions from the extension not the server.
505+ _preparse_for_stopping_flags (cls , args )
506+
507+ serverapp = cls .initialize_server (argv = args )
508+
509+ # Log if extension is blocking other extensions from loading.
510+ if not cls .load_other_extensions :
511+ serverapp .log .info (
512+ "{ext_name} is running without loading "
513+ "other extensions." .format (ext_name = cls .name )
505514 )
506- # Log if extension is blocking other extensions from loading.
507- if not cls .load_other_extensions :
508- serverapp .log .info (
509- "{ext_name} is running without loading "
510- "other extensions." .format (ext_name = cls .name )
511- )
512- try :
513- serverapp .start ()
514- except NoStart :
515- pass
515+ # Start the server.
516+ try :
517+ serverapp .start ()
518+ except NoStart :
519+ pass
0 commit comments