@@ -144,12 +144,27 @@ class method. This method can be set as a entry_point in
144
144
# side-by-side when launched directly.
145
145
load_other_extensions = True
146
146
147
+ # A useful class property that subclasses can override to
148
+ # configure the underlying Jupyter Server when this extension
149
+ # is launched directly (using its `launch_instance` method).
150
+ serverapp_config = {
151
+ "open_browser" : True
152
+ }
153
+
147
154
# The extension name used to name the jupyter config
148
155
# file, jupyter_{name}_config.
149
156
# This should also match the jupyter subcommand used to launch
150
157
# this extension from the CLI, e.g. `jupyter {name}`.
151
158
name = None
152
159
160
+ @classmethod
161
+ def get_extension_package (cls ):
162
+ return cls .__module__ .split ('.' )[0 ]
163
+
164
+ @classmethod
165
+ def get_extension_point (cls ):
166
+ return cls .__module__
167
+
153
168
# Extension URL sets the default landing page for this extension.
154
169
extension_url = "/"
155
170
@@ -158,6 +173,9 @@ class method. This method can be set as a entry_point in
158
173
ServerApp ,
159
174
]
160
175
176
+ # A ServerApp is not defined yet, but will be initialized below.
177
+ serverapp = None
178
+
161
179
@property
162
180
def static_url_prefix (self ):
163
181
return "/static/{name}/" .format (
@@ -281,28 +299,17 @@ def _prepare_templates(self):
281
299
self .initialize_templates ()
282
300
283
301
@classmethod
284
- def initialize_server (cls , argv = [], load_other_extensions = True , ** kwargs ):
285
- """Creates an instance of ServerApp where this extension is enabled
286
- (superceding disabling found in other config from files).
287
-
288
- This is necessary when launching the ExtensionApp directly from
289
- the `launch_instance` classmethod.
290
- """
291
- # The ExtensionApp needs to add itself as enabled extension
292
- # to the jpserver_extensions trait, so that the ServerApp
293
- # initializes it.
294
- config = Config ({
302
+ def _jupyter_server_config (cls ):
303
+ base_config = {
295
304
"ServerApp" : {
296
- "jpserver_extensions" : {cls .name : True },
297
- "open_browser" : cls .open_browser ,
305
+ "jpserver_extensions" : {cls .get_extension_package (): True },
298
306
"default_url" : cls .extension_url
299
307
}
300
- })
301
- serverapp = ServerApp .instance (** kwargs , argv = [], config = config )
302
- serverapp .initialize (argv = argv , find_extensions = load_other_extensions )
303
- return serverapp
308
+ }
309
+ base_config ["ServerApp" ].update (cls .serverapp_config )
310
+ return base_config
304
311
305
- def link_to_serverapp (self , serverapp ):
312
+ def _link_jupyter_server_extension (self , serverapp ):
306
313
"""Link the ExtensionApp to an initialized ServerApp.
307
314
308
315
The ServerApp is stored as an attribute and config
@@ -315,7 +322,7 @@ def link_to_serverapp(self, serverapp):
315
322
# Load config from an ExtensionApp's config files.
316
323
self .load_config_file ()
317
324
# ServerApp's config might have picked up
318
- # CLI config for the ExtensionApp. We call
325
+ # config for the ExtensionApp. We call
319
326
# update_config to update ExtensionApp's
320
327
# traits with these values found in ServerApp's
321
328
# config.
@@ -330,6 +337,22 @@ def link_to_serverapp(self, serverapp):
330
337
# i.e. ServerApp traits <--- ExtensionApp config
331
338
self .serverapp .update_config (self .config )
332
339
340
+ @classmethod
341
+ def initialize_server (cls , argv = [], load_other_extensions = True , ** kwargs ):
342
+ """Creates an instance of ServerApp where this extension is enabled
343
+ (superceding disabling found in other config from files).
344
+
345
+ This is necessary when launching the ExtensionApp directly from
346
+ the `launch_instance` classmethod.
347
+ """
348
+ # The ExtensionApp needs to add itself as enabled extension
349
+ # to the jpserver_extensions trait, so that the ServerApp
350
+ # initializes it.
351
+ config = Config (cls ._jupyter_server_config ())
352
+ serverapp = ServerApp .instance (** kwargs , argv = [], config = config )
353
+ serverapp .initialize (argv = argv , find_extensions = load_other_extensions )
354
+ return serverapp
355
+
333
356
def initialize (self ):
334
357
"""Initialize the extension app. The
335
358
corresponding server app and webapp should already
@@ -341,7 +364,7 @@ def initialize(self):
341
364
3) Points Tornado Webapp to templates and
342
365
static assets.
343
366
"""
344
- if not hasattr ( self , ' serverapp' ) :
367
+ if not self . serverapp :
345
368
msg = (
346
369
"This extension has no attribute `serverapp`. "
347
370
"Try calling `.link_to_serverapp()` before calling "
@@ -374,12 +397,14 @@ def _load_jupyter_server_extension(cls, serverapp):
374
397
"""Initialize and configure this extension, then add the extension's
375
398
settings and handlers to the server's web application.
376
399
"""
400
+ extension_manager = serverapp .extension_manager
377
401
try :
378
402
# Get loaded extension from serverapp.
379
- extension = serverapp ._enabled_extensions [cls .name ]
403
+ point = extension_manager .extension_points [cls .name ]
404
+ extension = point .app
380
405
except KeyError :
381
406
extension = cls ()
382
- extension .link_to_serverapp (serverapp )
407
+ extension ._link_jupyter_server_extension (serverapp )
383
408
extension .initialize ()
384
409
return extension
385
410
0 commit comments