Support Jupyverse as a single-user server

This commit is contained in:
David Brochart
2023-07-28 22:22:26 +02:00
parent 051a941e1e
commit c6325f3d85
2 changed files with 61 additions and 27 deletions

View File

@@ -27,6 +27,11 @@ if not _extension_env:
# default behavior: # default behavior:
# - extension, if jupyter server 2 # - extension, if jupyter server 2
# - older subclass app, otherwise # - older subclass app, otherwise
try:
import jupyverse_api # noqa: F401
_as_extension = False
except Exception:
try: try:
import jupyter_server import jupyter_server
@@ -67,6 +72,8 @@ else:
from .app import SingleUserNotebookApp, main from .app import SingleUserNotebookApp, main
# backward-compatibility # backward-compatibility
if SingleUserNotebookApp is not None:
# not Jupyverse
JupyterHubLoginHandler = SingleUserNotebookApp.login_handler_class JupyterHubLoginHandler = SingleUserNotebookApp.login_handler_class
JupyterHubLogoutHandler = SingleUserNotebookApp.logout_handler_class JupyterHubLogoutHandler = SingleUserNotebookApp.logout_handler_class
OAuthCallbackHandler = SingleUserNotebookApp.oauth_callback_handler_class OAuthCallbackHandler = SingleUserNotebookApp.oauth_callback_handler_class

View File

@@ -9,6 +9,7 @@
Use JUPYTERHUB_SINGLEUSER_APP='notebook' for the legacy 'classic' notebook server (requires notebook<7). Use JUPYTERHUB_SINGLEUSER_APP='notebook' for the legacy 'classic' notebook server (requires notebook<7).
""" """
import os import os
from urllib.parse import urlparse
from traitlets import import_item from traitlets import import_item
@@ -27,6 +28,7 @@ JUPYTERHUB_SINGLEUSER_APP = _app_shortcuts.get(
JUPYTERHUB_SINGLEUSER_APP.replace("_", "-"), JUPYTERHUB_SINGLEUSER_APP JUPYTERHUB_SINGLEUSER_APP.replace("_", "-"), JUPYTERHUB_SINGLEUSER_APP
) )
jupyverse = None
if JUPYTERHUB_SINGLEUSER_APP: if JUPYTERHUB_SINGLEUSER_APP:
if JUPYTERHUB_SINGLEUSER_APP in {"notebook", _app_shortcuts["notebook"]}: if JUPYTERHUB_SINGLEUSER_APP in {"notebook", _app_shortcuts["notebook"]}:
@@ -48,6 +50,11 @@ if JUPYTERHUB_SINGLEUSER_APP:
) )
App = import_item(JUPYTERHUB_SINGLEUSER_APP) App = import_item(JUPYTERHUB_SINGLEUSER_APP)
else: else:
try:
from jupyverse_api.cli import main as jupyverse
App = None
except Exception:
App = None App = None
_import_error = None _import_error = None
for JUPYTERHUB_SINGLEUSER_APP in ( for JUPYTERHUB_SINGLEUSER_APP in (
@@ -66,6 +73,9 @@ else:
raise _import_error raise _import_error
if App is None:
SingleUserNotebookApp = None
else:
SingleUserNotebookApp = make_singleuser_app(App) SingleUserNotebookApp = make_singleuser_app(App)
@@ -77,6 +87,23 @@ def main():
# This is a minimally extended ServerApp that does: # This is a minimally extended ServerApp that does:
# 1. ensure lab extension is enabled, and # 1. ensure lab extension is enabled, and
# 2. set default URL to `/lab` # 2. set default URL to `/lab`
if jupyverse:
service_url = os.environ.get("JUPYTERHUB_SERVICE_URL")
url = urlparse(service_url)
try:
return jupyverse.callback(
open_browser=True,
host=url.hostname,
port=url.port,
set_=[
f"frontend.base_url={url.path}",
f"app.mount_path={url.path}",
],
disable=[],
)
except Exception:
return
import re import re
_version_pat = re.compile(r"(\d+)\.(\d+)") _version_pat = re.compile(r"(\d+)\.(\d+)")