Skip to content

Commit ad3bbca

Browse files
authored
Merge pull request #232 from Zsailer/extension_name
ExtensionApp: change extension_name to name.
2 parents 104e0c1 + b6183f5 commit ad3bbca

File tree

11 files changed

+65
-87
lines changed

11 files changed

+65
-87
lines changed

docs/source/developers/extensions.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ An ExtensionApp:
109109

110110
- has traits.
111111
- is configurable (from file or CLI)
112-
- has a name (see the ``extension_name`` trait).
113-
- has an entrypoint, ``jupyter <extension_name>``.
114-
- can serve static content from the ``/static/<extension_name>/`` endpoint.
112+
- has a name (see the ``name`` trait).
113+
- has an entrypoint, ``jupyter <name>``.
114+
- can serve static content from the ``/static/<name>/`` endpoint.
115115
- can add new endpoints to the Jupyter Server.
116116

117117
The basic structure of an ExtensionApp is shown below:
@@ -124,7 +124,7 @@ The basic structure of an ExtensionApp is shown below:
124124
class MyExtensionApp(ExtensionApp):
125125
126126
# -------------- Required traits --------------
127-
extension_name = "myextension"
127+
name = "myextension"
128128
extension_url = "/myextension"
129129
load_other_extensions = True
130130
@@ -163,7 +163,7 @@ Methods
163163

164164
Properties
165165

166-
* ``extension_name``: the name of the extension
166+
* ``name``: the name of the extension
167167
* ``extension_url``: the default url for this extension—i.e. the landing page for this extension when launched from the CLI.
168168
* ``load_other_extensions``: a boolean enabling/disabling other extensions when launching this extension directly.
169169

@@ -174,8 +174,8 @@ Properties
174174

175175
* ``config``: the ExtensionApp's config object.
176176
* ``server_config``: the ServerApp's config object.
177-
* ``extension_name``: the name of the extension to which this handler is linked.
178-
* ``static_url()``: a method that returns the url to static files (prefixed with ``/static/<extension_name>``).
177+
* ``name``: the name of the extension to which this handler is linked.
178+
* ``static_url()``: a method that returns the url to static files (prefixed with ``/static/<name>``).
179179

180180
Jupyter Server provides a convenient mixin class for adding these properties to any ``JupyterHandler``. For example, the basic server extension handler in the section above becomes:
181181

@@ -202,7 +202,7 @@ Jinja templating from frontend extensions
202202

203203
Many Jupyter frontend applications use Jinja for basic HTML templating. Since this is common enough, Jupyter Server provides some extra mixin that integrate Jinja with Jupyter server extensions.
204204

205-
Use ``ExtensionAppJinjaMixin`` to automatically add a Jinja templating environment to an ``ExtensionApp``. This adds a ``<extension_name>_jinja2_env`` setting to Tornado Web Server's settings that will be used by request handlers.
205+
Use ``ExtensionAppJinjaMixin`` to automatically add a Jinja templating environment to an ``ExtensionApp``. This adds a ``<name>_jinja2_env`` setting to Tornado Web Server's settings that will be used by request handlers.
206206

207207
.. code-block:: python
208208

docs/source/operators/configuring-extensions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Jupyter Server looks for an extension's config file in a set of specific paths.
2626
Extension config from file
2727
--------------------------
2828

29-
Jupyter Server expects the file to be named after the extension's name like so: ``jupyter_{extension_name}_config``. For example, the Jupyter Notebook's config file is ``jupyter_notebook_config``.
29+
Jupyter Server expects the file to be named after the extension's name like so: ``jupyter_{name}_config``. For example, the Jupyter Notebook's config file is ``jupyter_notebook_config``.
3030

3131
Configuration files can be Python or JSON files.
3232

examples/simple/simple_ext1/application.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class SimpleApp1(ExtensionAppJinjaMixin, ExtensionApp):
1111

1212
# The name of the extension.
13-
extension_name = "simple_ext1"
13+
name = "simple_ext1"
1414

1515
# The url that your extension will serve its homepage.
1616
extension_url = '/simple_ext1/default'
@@ -45,11 +45,11 @@ class SimpleApp1(ExtensionAppJinjaMixin, ExtensionApp):
4545

4646
def initialize_handlers(self):
4747
self.handlers.extend([
48-
(r'/{}/default'.format(self.extension_name), DefaultHandler),
49-
(r'/{}/params/(.+)$'.format(self.extension_name), ParameterHandler),
50-
(r'/{}/template1/(.*)$'.format(self.extension_name), TemplateHandler),
51-
(r'/{}/redirect'.format(self.extension_name), RedirectHandler),
52-
(r'/{}/typescript/?'.format(self.extension_name), TypescriptHandler),
48+
(r'/{}/default'.format(self.name), DefaultHandler),
49+
(r'/{}/params/(.+)$'.format(self.name), ParameterHandler),
50+
(r'/{}/template1/(.*)$'.format(self.name), TemplateHandler),
51+
(r'/{}/redirect'.format(self.name), RedirectHandler),
52+
(r'/{}/typescript/?'.format(self.name), TypescriptHandler),
5353
(r'/{}/(.*)', ErrorHandler)
5454
])
5555

examples/simple/simple_ext1/handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
class DefaultHandler(ExtensionHandlerMixin, JupyterHandler):
55
def get(self):
66
# The name of the extension to which this handler is linked.
7-
self.log.info("Extension Name in {} Default Handler: {}".format(self.extension_name, self.extension_name))
8-
# A method for getting the url to static files (prefixed with /static/<extension_name>).
7+
self.log.info("Extension Name in {} Default Handler: {}".format(self.name, self.name))
8+
# A method for getting the url to static files (prefixed with /static/<name>).
99
self.log.info("Static URL for / in simple_ext1 Default Handler:".format(self.static_url(path='/')))
1010
self.write('<h1>Hello Simple 1 - I am the default...</h1>')
11-
self.write('Config in {} Default Handler: {}'.format(self.extension_name, self.config))
11+
self.write('Config in {} Default Handler: {}'.format(self.name, self.config))
1212

1313
class RedirectHandler(ExtensionHandlerMixin, JupyterHandler):
1414
def get(self):
15-
self.redirect("/static/{}/favicon.ico".format(self.extension_name))
15+
self.redirect("/static/{}/favicon.ico".format(self.name))
1616

17-
class ParameterHandler(ExtensionHandlerMixin, JupyterHandler):
17+
class ParameterHandler(ExtensionHandlerMixin, JupyterHandler):
1818
def get(self, matched_part=None, *args, **kwargs):
1919
var1 = self.get_argument('var1', default=None)
2020
components = [x for x in self.request.path.split("/") if x]

examples/simple/simple_ext11/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SimpleApp11(SimpleApp1):
1515
})
1616

1717
# The name of the extension.
18-
extension_name = "simple_ext11"
18+
name = "simple_ext11"
1919

2020
# Te url that your extension will serve its homepage.
2121
extension_url = '/simple_ext11/default'

examples/simple/simple_ext2/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class SimpleApp2(ExtensionAppJinjaMixin, ExtensionApp):
1010

1111
# The name of the extension.
12-
extension_name = "simple_ext2"
12+
name = "simple_ext2"
1313

1414
# Te url that your extension will serve its homepage.
1515
extension_url = '/simple_ext2'

jupyter_server/extension/application.py

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _prepare_templates(self):
9696
# Add templates to web app settings if extension has templates.
9797
if len(self.template_paths) > 0:
9898
self.settings.update({
99-
"{}_template_paths".format(self.extension_name): self.template_paths
99+
"{}_template_paths".format(self.name): self.template_paths
100100
})
101101

102102
# Create a jinja environment for logging html templates.
@@ -111,7 +111,7 @@ def _prepare_templates(self):
111111
# Add the jinja2 environment for this extension to the tornado settings.
112112
self.settings.update(
113113
{
114-
"{}_jinja2_env".format(self.extension_name): self.jinja2_env
114+
"{}_jinja2_env".format(self.name): self.jinja2_env
115115
}
116116
)
117117

@@ -144,32 +144,11 @@ class method. This method can be set as a entry_point in
144144
# side-by-side when launched directly.
145145
load_other_extensions = True
146146

147-
# Name of the extension
148-
extension_name = Unicode(
149-
help="Name of extension."
150-
)
151-
152-
@default('extension_name')
153-
def _extension_name_default(self):
154-
try:
155-
return self.name
156-
except AttributeError:
157-
raise ValueError("The extension must be given a `name`.")
158-
159-
INVALID_EXTENSION_NAME_CHARS = [' ', '.', '+', '/']
160-
161-
@validate('extension_name')
162-
def _validate_extension_name(self, proposal):
163-
value = proposal['value']
164-
if isinstance(value, str):
165-
# Validate that extension_name doesn't contain any invalid characters.
166-
for c in ExtensionApp.INVALID_EXTENSION_NAME_CHARS:
167-
if c in value:
168-
raise ValueError("Extension name '{name}' cannot contain any of the following characters: "
169-
"{invalid_chars}.".
170-
format(name=value, invalid_chars=ExtensionApp.INVALID_EXTENSION_NAME_CHARS))
171-
return value
172-
raise ValueError("Extension name must be a string, found {type}.".format(type=type(value)))
147+
# The extension name used to name the jupyter config
148+
# file, jupyter_{name}_config.
149+
# This should also match the jupyter subcommand used to launch
150+
# this extension from the CLI, e.g. `jupyter {name}`.
151+
name = None
173152

174153
# Extension URL sets the default landing page for this extension.
175154
extension_url = "/"
@@ -181,8 +160,8 @@ def _validate_extension_name(self, proposal):
181160

182161
@property
183162
def static_url_prefix(self):
184-
return "/static/{extension_name}/".format(
185-
extension_name=self.extension_name)
163+
return "/static/{name}/".format(
164+
name=self.name)
186165

187166
static_paths = List(Unicode(),
188167
help="""paths to search for serving static files.
@@ -211,10 +190,9 @@ def static_url_prefix(self):
211190

212191
def _config_file_name_default(self):
213192
"""The default config file name."""
214-
if not self.extension_name:
193+
if not self.name:
215194
return ''
216-
return 'jupyter_{}_config'.format(self.extension_name.replace('-','_'))
217-
195+
return 'jupyter_{}_config'.format(self.name.replace('-','_'))
218196

219197
def initialize_settings(self):
220198
"""Override this method to add handling of settings."""
@@ -230,11 +208,11 @@ def initialize_templates(self):
230208

231209
def _prepare_config(self):
232210
"""Builds a Config object from the extension's traits and passes
233-
the object to the webapp's settings as `<extension_name>_config`.
211+
the object to the webapp's settings as `<name>_config`.
234212
"""
235213
traits = self.class_own_traits().keys()
236214
self.extension_config = Config({t: getattr(self, t) for t in traits})
237-
self.settings['{}_config'.format(self.extension_name)] = self.extension_config
215+
self.settings['{}_config'.format(self.name)] = self.extension_config
238216

239217
def _prepare_settings(self):
240218
# Make webapp settings accessible to initialize_settings method
@@ -243,8 +221,8 @@ def _prepare_settings(self):
243221

244222
# Add static and template paths to settings.
245223
self.settings.update({
246-
"{}_static_paths".format(self.extension_name): self.static_paths,
247-
"{}".format(self.extension_name): self
224+
"{}_static_paths".format(self.name): self.static_paths,
225+
"{}".format(self.name): self
248226
})
249227

250228
# Get setting defined by subclass using initialize_settings method.
@@ -269,7 +247,8 @@ def _prepare_handlers(self):
269247
# Get handler kwargs, if given
270248
kwargs = {}
271249
if issubclass(handler, ExtensionHandlerMixin):
272-
kwargs['extension_name'] = self.extension_name
250+
kwargs['name'] = self.name
251+
273252
try:
274253
kwargs.update(handler_items[2])
275254
except IndexError:
@@ -297,7 +276,7 @@ def _prepare_templates(self):
297276
# Add templates to web app settings if extension has templates.
298277
if len(self.template_paths) > 0:
299278
self.settings.update({
300-
"{}_template_paths".format(self.extension_name): self.template_paths
279+
"{}_template_paths".format(self.name): self.template_paths
301280
})
302281
self.initialize_templates()
303282

@@ -314,7 +293,7 @@ def initialize_server(cls, argv=[], load_other_extensions=True, **kwargs):
314293
# initializes it.
315294
config = Config({
316295
"ServerApp": {
317-
"jpserver_extensions": {cls.extension_name: True},
296+
"jpserver_extensions": {cls.name: True},
318297
"open_browser": cls.open_browser,
319298
"default_url": cls.extension_url
320299
}
@@ -397,7 +376,7 @@ def _load_jupyter_server_extension(cls, serverapp):
397376
"""
398377
try:
399378
# Get loaded extension from serverapp.
400-
extension = serverapp._enabled_extensions[cls.extension_name]
379+
extension = serverapp._enabled_extensions[cls.name]
401380
except KeyError:
402381
extension = cls()
403382
extension.link_to_serverapp(serverapp)
@@ -433,6 +412,6 @@ def launch_instance(cls, argv=None, **kwargs):
433412
if not cls.load_other_extensions:
434413
serverapp.log.info(
435414
"{ext_name} is running without loading "
436-
"other extensions.".format(ext_name=cls.extension_name)
415+
"other extensions.".format(ext_name=cls.name)
437416
)
438417
serverapp.start()

jupyter_server/extension/handler.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ class ExtensionHandlerJinjaMixin:
88
"""
99
def get_template(self, name):
1010
"""Return the jinja template object for a given name"""
11-
env = '{}_jinja2_env'.format(self.extension_name)
11+
env = '{}_jinja2_env'.format(self.name)
1212
return self.settings[env].get_template(name)
1313

1414

1515
class ExtensionHandlerMixin:
1616
"""Base class for Jupyter server extension handlers.
1717
1818
Subclasses can serve static files behind a namespaced
19-
endpoint: "/static/<extension_name>/"
19+
endpoint: "/static/<name>/"
2020
2121
This allows multiple extensions to serve static files under
2222
their own namespace and avoid intercepting requests for
2323
other extensions.
2424
"""
25-
def initialize(self, extension_name):
26-
self.extension_name = extension_name
25+
def initialize(self, name):
26+
self.name = name
2727

2828
@property
2929
def extensionapp(self):
30-
return self.settings[self.extension_name]
30+
return self.settings[self.name]
3131

3232
@property
3333
def serverapp(self):
@@ -36,24 +36,23 @@ def serverapp(self):
3636

3737
@property
3838
def config(self):
39-
return self.settings["{}_config".format(self.extension_name)]
39+
return self.settings["{}_config".format(self.name)]
4040

4141
@property
4242
def server_config(self):
4343
return self.settings["config"]
4444

4545
@property
4646
def static_url_prefix(self):
47-
return "/static/{extension_name}/".format(
48-
extension_name=self.extension_name)
47+
return "/static/{name}/".format(name=self.name)
4948

5049
@property
5150
def static_path(self):
52-
return self.settings['{}_static_paths'.format(self.extension_name)]
51+
return self.settings['{}_static_paths'.format(self.name)]
5352

5453
def static_url(self, path, include_host=None, **kwargs):
5554
"""Returns a static URL for the given relative static file path.
56-
This method requires you set the ``{extension_name}_static_path``
55+
This method requires you set the ``{name}_static_path``
5756
setting in your extension (which specifies the root directory
5857
of your static files).
5958
This method returns a versioned url (by default appending
@@ -68,7 +67,7 @@ def static_url(self, path, include_host=None, **kwargs):
6867
that value will be used as the default for all `static_url`
6968
calls that do not pass ``include_host`` as a keyword argument.
7069
"""
71-
key = "{}_static_paths".format(self.extension_name)
70+
key = "{}_static_paths".format(self.name)
7271
try:
7372
self.require_setting(key, "static_url")
7473
except Exception as e:

jupyter_server/extension/serverextension.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _get_load_jupyter_server_extension(obj):
154154
return func
155155

156156

157-
def validate_server_extension(extension_name):
157+
def validate_server_extension(name):
158158
"""Validates that you can import the extension module,
159159
gather all extension metadata, and find `load_jupyter_server_extension`
160160
functions for each extension.
@@ -176,10 +176,10 @@ def validate_server_extension(extension_name):
176176
"""
177177
# If the extension does not exist, raise an exception
178178
try:
179-
mod, metadata = _get_server_extension_metadata(extension_name)
179+
mod, metadata = _get_server_extension_metadata(name)
180180
version = getattr(mod, '__version__', '')
181181
except ImportError:
182-
raise ExtensionValidationError('{} is not importable.'.format(extension_name))
182+
raise ExtensionValidationError('{} is not importable.'.format(name))
183183

184184
try:
185185
for item in metadata:
@@ -194,7 +194,7 @@ def validate_server_extension(extension_name):
194194
raise AttributeError
195195
# If the extension does not have a `load_jupyter_server_extension` function, raise exception.
196196
except AttributeError:
197-
raise ExtensionValidationError('Found "{}" module but cannot load it.'.format(extension_name))
197+
raise ExtensionValidationError('Found "{}" module but cannot load it.'.format(name))
198198
return version
199199

200200

jupyter_server/serverapp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ def init_server_extensions(self):
15671567
app = extapp()
15681568
app.link_to_serverapp(self)
15691569
# Build a new list where we
1570-
self._enabled_extensions[app.extension_name] = app
1570+
self._enabled_extensions[app.name] = app
15711571
elif extloc:
15721572
extmod = importlib.import_module(extloc)
15731573
func = _get_load_jupyter_server_extension(extmod)
@@ -1599,8 +1599,8 @@ def load_server_extensions(self):
15991599
)
16001600
else:
16011601
log_msg = (
1602-
"Extension {extension_name} enabled and "
1603-
"loaded".format(extension_name=extension.extension_name)
1602+
"Extension {name} enabled and "
1603+
"loaded".format(name=extension.name)
16041604
)
16051605
# Find the extension loading function.
16061606
func = None

0 commit comments

Comments
 (0)