Skip to content

Commit 6b3c68e

Browse files
committed
Lab theme handling
1 parent 15ccacf commit 6b3c68e

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ packages = find:
3333
python_requires = >=3.7
3434
install_requires =
3535
jupyter_server>=0.3.0,<2.0.0
36+
jupyterlab_server>=2.3.0,<3
3637
jupyter_client>=6.1.3,<8
3738
nbclient>=0.4.0,<0.6
38-
nbconvert>=6.0.0,<7
39+
nbconvert>=6.4.2,<7
3940
websockets>=9.0
4041
traitlets>=5.0.3,<6
4142

share/jupyter/voila/templates/lab/page.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{{ include_css("static/index.css") }}
99
{{ include_css("static/theme-light.css") }}
1010
{% else %}
11-
<!-- TODO: Use custom css from theme labextension -->
11+
{{ include_css("static/index.css") }}
12+
{{ include_lab_theme(theme) }}
1213
{% endif %}
1314
{% endblock %}

voila/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
from jupyter_server.utils import url_path_join, run_sync
4545
from jupyter_server.services.config import ConfigManager
4646

47+
from jupyterlab_server.themes_handler import ThemesHandler
48+
4749
from jupyter_client.kernelspec import KernelSpecManager
4850

4951
from jupyter_core.paths import jupyter_config_path, jupyter_path
@@ -481,6 +483,16 @@ def start(self):
481483
'default_filename': 'index.html'
482484
},
483485
),
486+
(
487+
url_path_join(self.server_url, r'/voila/themes/(.*)'),
488+
ThemesHandler,
489+
{
490+
'themes_url': '/voila/themes',
491+
'path': '',
492+
'labextensions_path': jupyter_path('labextensions'),
493+
'no_cache_paths': ['/']
494+
},
495+
),
484496
(url_path_join(self.server_url, r'/voila/api/shutdown/(.*)'), VoilaShutdownKernelHandler)
485497
])
486498

voila/server_extension.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from jupyter_server.utils import url_path_join
1616
from jupyter_server.base.handlers import path_regex, FileFindHandler
1717

18+
from jupyterlab_server.themes_handler import ThemesHandler
19+
1820
from .paths import ROOT, collect_template_paths, collect_static_paths, jupyter_path
1921
from .handler import VoilaHandler
2022
from .treehandler import VoilaTreeHandler
@@ -67,6 +69,16 @@ def _load_jupyter_server_extension(server_app):
6769
(url_path_join(base_url, '/voila'), VoilaTreeHandler, tree_handler_conf),
6870
(url_path_join(base_url, '/voila/tree' + path_regex), VoilaTreeHandler, tree_handler_conf),
6971
(url_path_join(base_url, '/voila/templates/(.*)'), TemplateStaticFileHandler),
72+
(
73+
url_path_join(base_url, r'/voila/themes/(.*)'),
74+
ThemesHandler,
75+
{
76+
'themes_url': '/voila/themes',
77+
'path': '',
78+
'labextensions_path': jupyter_path('labextensions'),
79+
'no_cache_paths': ['/']
80+
},
81+
),
7082
(url_path_join(base_url, '/voila/static/(.*)'), MultiStaticFileHandler, {'paths': static_paths}),
7183
(url_path_join(base_url, r'/voila/api/shutdown/(.*)'), VoilaShutdownKernelHandler),
7284
(

voila/utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import jinja2
2020

21+
from nbconvert.exporters.html import find_lab_theme
22+
23+
from jupyterlab_server.themes_handler import ThemesHandler
24+
2125
from .static_file_handler import TemplateStaticFileHandler
2226

2327

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

127131

132+
def include_lab_theme(base_url, name):
133+
# Try to find the theme with the given name, looking through the labextensions
134+
theme_name, _ = find_lab_theme(name)
135+
136+
settings = {
137+
'static_url_prefix': f'{base_url}voila/themes/',
138+
'static_path': None # not used in TemplateStaticFileHandler.get_absolute_path
139+
}
140+
url = ThemesHandler.make_static_url(settings, f'{theme_name}/index.css')
141+
142+
code = f'<link rel="stylesheet" type="text/css" href="{url}">'
143+
return jinja2.Markup(code)
144+
145+
128146
def create_include_assets_functions(template_name, base_url):
129147
return {
130148
"include_css": partial(include_css, template_name, base_url),
131149
"include_js": partial(include_js, template_name, base_url),
132-
"include_url": partial(include_url, template_name, base_url)
150+
"include_url": partial(include_url, template_name, base_url),
151+
"include_lab_theme": partial(include_lab_theme, base_url)
133152
}

0 commit comments

Comments
 (0)