Skip to content

Commit

Permalink
Lab theme handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Feb 10, 2022
1 parent 15ccacf commit 6b3c68e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ packages = find:
python_requires = >=3.7
install_requires =
jupyter_server>=0.3.0,<2.0.0
jupyterlab_server>=2.3.0,<3
jupyter_client>=6.1.3,<8
nbclient>=0.4.0,<0.6
nbconvert>=6.0.0,<7
nbconvert>=6.4.2,<7
websockets>=9.0
traitlets>=5.0.3,<6

Expand Down
3 changes: 2 additions & 1 deletion share/jupyter/voila/templates/lab/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{{ include_css("static/index.css") }}
{{ include_css("static/theme-light.css") }}
{% else %}
<!-- TODO: Use custom css from theme labextension -->
{{ include_css("static/index.css") }}
{{ include_lab_theme(theme) }}
{% endif %}
{% endblock %}
12 changes: 12 additions & 0 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
from jupyter_server.utils import url_path_join, run_sync
from jupyter_server.services.config import ConfigManager

from jupyterlab_server.themes_handler import ThemesHandler

from jupyter_client.kernelspec import KernelSpecManager

from jupyter_core.paths import jupyter_config_path, jupyter_path
Expand Down Expand Up @@ -481,6 +483,16 @@ def start(self):
'default_filename': 'index.html'
},
),
(
url_path_join(self.server_url, r'/voila/themes/(.*)'),
ThemesHandler,
{
'themes_url': '/voila/themes',
'path': '',
'labextensions_path': jupyter_path('labextensions'),
'no_cache_paths': ['/']
},
),
(url_path_join(self.server_url, r'/voila/api/shutdown/(.*)'), VoilaShutdownKernelHandler)
])

Expand Down
12 changes: 12 additions & 0 deletions voila/server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from jupyter_server.utils import url_path_join
from jupyter_server.base.handlers import path_regex, FileFindHandler

from jupyterlab_server.themes_handler import ThemesHandler

from .paths import ROOT, collect_template_paths, collect_static_paths, jupyter_path
from .handler import VoilaHandler
from .treehandler import VoilaTreeHandler
Expand Down Expand Up @@ -67,6 +69,16 @@ def _load_jupyter_server_extension(server_app):
(url_path_join(base_url, '/voila'), VoilaTreeHandler, tree_handler_conf),
(url_path_join(base_url, '/voila/tree' + path_regex), VoilaTreeHandler, tree_handler_conf),
(url_path_join(base_url, '/voila/templates/(.*)'), TemplateStaticFileHandler),
(
url_path_join(base_url, r'/voila/themes/(.*)'),
ThemesHandler,
{
'themes_url': '/voila/themes',
'path': '',
'labextensions_path': jupyter_path('labextensions'),
'no_cache_paths': ['/']
},
),
(url_path_join(base_url, '/voila/static/(.*)'), MultiStaticFileHandler, {'paths': static_paths}),
(url_path_join(base_url, r'/voila/api/shutdown/(.*)'), VoilaShutdownKernelHandler),
(
Expand Down
21 changes: 20 additions & 1 deletion voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import jinja2

from nbconvert.exporters.html import find_lab_theme

from jupyterlab_server.themes_handler import ThemesHandler

from .static_file_handler import TemplateStaticFileHandler


Expand Down Expand Up @@ -125,9 +129,24 @@ def include_url(template_name, base_url, name):
return jinja2.Markup(make_url(template_name, base_url, name))


def include_lab_theme(base_url, name):
# Try to find the theme with the given name, looking through the labextensions
theme_name, _ = find_lab_theme(name)

settings = {
'static_url_prefix': f'{base_url}voila/themes/',
'static_path': None # not used in TemplateStaticFileHandler.get_absolute_path
}
url = ThemesHandler.make_static_url(settings, f'{theme_name}/index.css')

code = f'<link rel="stylesheet" type="text/css" href="{url}">'
return jinja2.Markup(code)


def create_include_assets_functions(template_name, base_url):
return {
"include_css": partial(include_css, template_name, base_url),
"include_js": partial(include_js, template_name, base_url),
"include_url": partial(include_url, template_name, base_url)
"include_url": partial(include_url, template_name, base_url),
"include_lab_theme": partial(include_lab_theme, base_url)
}

0 comments on commit 6b3c68e

Please sign in to comment.