4
4
5
5
from jinja2 import Environment , FileSystemLoader
6
6
7
+ from traitlets .config import Config
7
8
from traitlets import (
8
9
HasTraits ,
9
10
Unicode ,
12
13
Bool ,
13
14
default
14
15
)
15
- from traitlets .config import Config
16
16
from tornado .log import LogFormatter
17
17
from tornado .web import RedirectHandler
18
18
@@ -186,6 +186,9 @@ def get_extension_point(cls):
186
186
def _default_url (self ):
187
187
return self .extension_url
188
188
189
+ # Is this linked to a serverapp yet?
190
+ _linked = Bool (False )
191
+
189
192
# Extension can configure the ServerApp from the command-line
190
193
classes = [
191
194
ServerApp ,
@@ -196,9 +199,6 @@ def _default_url(self):
196
199
197
200
_log_formatter_cls = LogFormatter
198
201
199
- # Whether this app is the starter app
200
- _is_starter_app = False
201
-
202
202
@default ('log_level' )
203
203
def _default_log_level (self ):
204
204
return logging .INFO
@@ -333,14 +333,14 @@ def _prepare_templates(self):
333
333
})
334
334
self .initialize_templates ()
335
335
336
- @classmethod
337
- def _jupyter_server_config (cls ):
336
+ def _jupyter_server_config (self ):
338
337
base_config = {
339
338
"ServerApp" : {
340
- "jpserver_extensions" : {cls .get_extension_package (): True },
339
+ "default_url" : self .default_url ,
340
+ "open_browser" : self .open_browser
341
341
}
342
342
}
343
- base_config ["ServerApp" ].update (cls .serverapp_config )
343
+ base_config ["ServerApp" ].update (self .serverapp_config )
344
344
return base_config
345
345
346
346
def _link_jupyter_server_extension (self , serverapp ):
@@ -351,6 +351,10 @@ def _link_jupyter_server_extension(self, serverapp):
351
351
the command line contains traits for the ExtensionApp
352
352
or the ExtensionApp's config files have server
353
353
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.
354
358
"""
355
359
self .serverapp = serverapp
356
360
# Load config from an ExtensionApp's config files.
@@ -370,23 +374,8 @@ def _link_jupyter_server_extension(self, serverapp):
370
374
# ServerApp, do it here.
371
375
# i.e. ServerApp traits <--- ExtensionApp config
372
376
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
390
379
391
380
def initialize (self ):
392
381
"""Initialize the extension app. The
@@ -440,12 +429,7 @@ def _load_jupyter_server_extension(cls, serverapp):
440
429
except KeyError :
441
430
extension = cls ()
442
431
extension ._link_jupyter_server_extension (serverapp )
443
- if cls ._is_starter_app :
444
- serverapp ._starter_app = extension
445
432
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
449
433
return extension
450
434
451
435
@classmethod
@@ -478,6 +462,24 @@ def load_classic_server_extension(cls, serverapp):
478
462
])
479
463
extension .initialize ()
480
464
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
+
481
483
@classmethod
482
484
def launch_instance (cls , argv = None , ** kwargs ):
483
485
"""Launch the extension like an application. Initializes+configs a stock server
@@ -489,27 +491,29 @@ def launch_instance(cls, argv=None, **kwargs):
489
491
args = sys .argv [1 :] # slice out extension config.
490
492
else :
491
493
args = argv
492
- # Check for subcommands
494
+
495
+ # Handle all "stops" that could happen before
496
+ # continuing to launch a server+extension.
493
497
subapp = _preparse_for_subcommand (cls , args )
494
498
if subapp :
495
499
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 )
505
514
)
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