Fix handlers for server extensions

This commit is contained in:
Jeremy Tuloup 2020-12-07 15:09:16 +01:00
parent 519fcee7a3
commit 7277010345
2 changed files with 70 additions and 16 deletions

View File

@ -1,58 +1,112 @@
import os import os
from os import path
from os.path import join as pjoin
from urllib.parse import urlparse
from jupyter_server.base.handlers import JupyterHandler from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.extension.handler import ( from jupyter_server.extension.handler import (
ExtensionHandlerMixin, ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin, ExtensionHandlerJinjaMixin,
) )
from jupyterlab import LabApp
from jupyter_server.utils import url_path_join as ujoin from jupyter_server.utils import url_path_join as ujoin
from jupyterlab.commands import get_app_dir, get_user_settings_dir, get_workspaces_dir
from jupyterlab_server import LabServerApp
from jupyterlab_server.config import get_page_config, recursive_update, LabConfig
from jupyterlab_server.handlers import is_url, _camelCase
from tornado import web from tornado import web
from ._version import __version__ from ._version import __version__
HERE = os.path.dirname(__file__) HERE = os.path.dirname(__file__)
app_dir = get_app_dir()
version = __version__ version = __version__
class ClassicHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler): class ClassicHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
@web.authenticated @web.authenticated
def get(self): def get(self, path=None):
config_data = { config = LabConfig()
app = self.extensionapp
base_url = self.settings.get("base_url")
page_config = {
"appVersion": version, "appVersion": version,
"baseUrl": self.base_url, "baseUrl": self.base_url,
"token": self.settings["token"], "token": self.settings["token"],
"fullStaticUrl": ujoin(self.base_url, "static", self.name), "fullStaticUrl": ujoin(self.base_url, "static", self.name),
"frontendUrl": ujoin(self.base_url, "classic/"), "frontendUrl": ujoin(self.base_url, "classic/"),
} }
mathjax_config = self.settings.get("mathjax_config", "TeX-AMS_HTML-full,Safe")
# TODO Remove CDN usage.
mathjax_url = self.settings.get(
"mathjax_url",
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js",
)
page_config.setdefault("mathjaxConfig", mathjax_config)
page_config.setdefault("fullMathjaxUrl", mathjax_url)
# Put all our config in page_config
for name in config.trait_names():
page_config[_camelCase(name)] = getattr(app, name)
# Add full versions of all the urls
for name in config.trait_names():
if not name.endswith("_url"):
continue
full_name = _camelCase("full_" + name)
full_url = getattr(app, name)
if not is_url(full_url):
# Relative URL will be prefixed with base_url
full_url = ujoin(base_url, full_url)
page_config[full_name] = full_url
labextensions_path = app.extra_labextensions_path + app.labextensions_path
recursive_update(
page_config,
get_page_config(
labextensions_path,
logger=self.log,
),
)
return self.write( return self.write(
self.render_template( self.render_template(
"index.html", "notebooks.html",
static=self.static_url,
base_url=self.base_url, base_url=self.base_url,
token=self.settings["token"], token=self.settings["token"],
page_config=config_data, page_config=page_config,
) )
) )
class ClassicApp(LabApp): class ClassicApp(LabServerApp):
extension_url = '/classic'
app_url = "/classic"
load_other_extensions = True
name = "classic" name = "classic"
app_name = 'JupyterLab Classic' app_name = "JupyterLab Classic"
static_dir = os.path.join(HERE, 'static')
templates_dir = os.path.join(HERE, 'templates')
app_version = version app_version = version
extension_url = "/classic"
load_other_extensions = True
app_dir = app_dir
app_settings_dir = pjoin(app_dir, "settings")
schemas_dir = pjoin(app_dir, "schemas")
themes_dir = pjoin(app_dir, "themes")
user_settings_dir = get_user_settings_dir()
workspaces_dir = get_workspaces_dir()
def initialize_handlers(self): def initialize_handlers(self):
super().initialize_handlers() super().initialize_handlers()
self.handlers.append(('/classic', ClassicHandler)) self.handlers.append(("/classic/notebooks(.*)", ClassicHandler))
def initialize_templates(self):
super().initialize_templates()
self.static_dir = os.path.join(HERE, "static")
self.templates_dir = os.path.join(HERE, "templates")
self.static_paths = [self.static_dir]
self.template_paths = [self.templates_dir]
main = launch_new_instance = ClassicApp.launch_instance main = launch_new_instance = ClassicApp.launch_instance
if __name__ == '__main__': if __name__ == "__main__":
main() main()