Skip to content

Commit

Permalink
Fix server-extension paths (3rd time's the charm) (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Mar 14, 2022
1 parent 93b1c83 commit 461b551
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
25 changes: 23 additions & 2 deletions jupyter_server/extension/application.py
Expand Up @@ -12,6 +12,7 @@
from traitlets import default
from traitlets import Dict
from traitlets import HasTraits
from traitlets import Instance
from traitlets import List
from traitlets import Unicode
from traitlets.config import Config
Expand Down Expand Up @@ -103,7 +104,7 @@ def _prepare_templates(self):
loader=FileSystemLoader(self.template_paths),
extensions=["jinja2.ext.i18n"],
autoescape=True,
**self.jinja2_options
**self.jinja2_options,
)

# Add the jinja2 environment for this extension to the tornado settings.
Expand Down Expand Up @@ -162,6 +163,12 @@ class method. This method can be set as a entry_point in
def _default_open_browser(self):
return self.serverapp.config["ServerApp"].get("open_browser", True)

@property
def config_file_paths(self):
"""Look on the same path as our parent for config files"""
# rely on parent serverapp, which should control all config loading
return self.serverapp.config_file_paths

# The extension name used to name the jupyter config
# file, jupyter_{name}_config.
# This should also match the jupyter subcommand used to launch
Expand Down Expand Up @@ -200,7 +207,21 @@ def _default_url(self):
]

# A ServerApp is not defined yet, but will be initialized below.
serverapp = None
serverapp = Instance(ServerApp)

@default("serverapp")
def _default_serverapp(self):
# load the current global instance, if any
if ServerApp.initialized():
try:
return ServerApp.instance()
except Exception:
# error retrieving instance, e.g. MultipleInstanceError
pass

# serverapp accessed before it was defined,
# declare an empty one
return ServerApp()

_log_formatter_cls = LogFormatter

Expand Down
7 changes: 1 addition & 6 deletions jupyter_server/serverapp.py
Expand Up @@ -104,7 +104,6 @@
base_flags,
base_aliases,
)
from jupyter_core.paths import jupyter_config_path
from jupyter_client import KernelManager
from jupyter_client.kernelspec import KernelSpecManager
from jupyter_client.session import Session
Expand Down Expand Up @@ -2154,11 +2153,7 @@ def find_server_extensions(self):
# This enables merging on keys, which we want for extension enabling.
# Regular config loading only merges at the class level,
# so each level clobbers the previous.
config_paths = jupyter_config_path()
if self.config_dir not in config_paths:
# add self.config_dir to the front, if set manually
config_paths.insert(0, self.config_dir)
manager = ExtensionConfigManager(read_config_path=config_paths)
manager = ExtensionConfigManager(read_config_path=self.config_file_paths)
extensions = manager.get_jpserver_extensions()

for modulename, enabled in sorted(extensions.items()):
Expand Down
8 changes: 8 additions & 0 deletions tests/extension/test_app.py
Expand Up @@ -69,6 +69,14 @@ def test_extensionapp_load_config_file(
assert mock_extension.mock_trait == "config from file"


def test_extensionapp_no_parent():
# make sure we can load config files, even when serverapp is not passed
# relevant for e.g. shortcuts to config-loading
app = MockExtensionApp()
assert isinstance(app.config_file_paths, list)
assert app.serverapp is not None


OPEN_BROWSER_COMBINATIONS = (
(True, {}),
(True, {"ServerApp": {"open_browser": True}}),
Expand Down

0 comments on commit 461b551

Please sign in to comment.