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
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.extension.handler import (
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin,
)
from jupyterlab import LabApp
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 ._version import __version__
HERE = os.path.dirname(__file__)
app_dir = get_app_dir()
version = __version__
class ClassicHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
@web.authenticated
def get(self):
config_data = {
def get(self, path=None):
config = LabConfig()
app = self.extensionapp
base_url = self.settings.get("base_url")
page_config = {
"appVersion": version,
"baseUrl": self.base_url,
"token": self.settings["token"],
"fullStaticUrl": ujoin(self.base_url, "static", self.name),
"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(
self.render_template(
"index.html",
static=self.static_url,
"notebooks.html",
base_url=self.base_url,
token=self.settings["token"],
page_config=config_data,
page_config=page_config,
)
)
class ClassicApp(LabApp):
extension_url = '/classic'
app_url = "/classic"
load_other_extensions = True
class ClassicApp(LabServerApp):
name = "classic"
app_name = 'JupyterLab Classic'
static_dir = os.path.join(HERE, 'static')
templates_dir = os.path.join(HERE, 'templates')
app_name = "JupyterLab Classic"
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):
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
if __name__ == '__main__':
if __name__ == "__main__":
main()