Skip to content

Commit fbb57e1

Browse files
committed
Handle static files
1 parent a311337 commit fbb57e1

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

voila/utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#############################################################################
99

1010
import asyncio
11+
import datetime
1112
import json
1213
import os
1314
import threading
@@ -17,16 +18,16 @@
1718
from typing import Awaitable, Dict, List, Tuple
1819

1920
import websockets
21+
from jupyter_core.paths import jupyter_path
22+
from jupyter_server.utils import url_path_join
23+
from jupyterlab_server.config import get_page_config as gpc
24+
from jupyterlab_server.config import recursive_update
2025
from jupyterlab_server.themes_handler import ThemesHandler
2126
from markupsafe import Markup
2227
from nbconvert.exporters.html import find_lab_theme
2328

24-
from jupyter_core.paths import jupyter_path
25-
from jupyterlab_server.config import get_page_config as gpc, recursive_update
26-
from jupyter_server.utils import url_path_join
27-
28-
from .static_file_handler import TemplateStaticFileHandler
2929
from ._version import __version__
30+
from .static_file_handler import TemplateStaticFileHandler
3031

3132

3233
class ENV_VARIABLE(str, Enum):
@@ -233,3 +234,13 @@ def find_all_lab_theme() -> List[Tuple[str, str]]:
233234
pass
234235

235236
return theme_list
237+
238+
239+
class DateTimeEncoder(json.JSONEncoder):
240+
"""A custom date-aware JSON encoder"""
241+
242+
def default(self, o):
243+
if isinstance(o, datetime.datetime):
244+
return o.isoformat().replace('+00:00', 'Z')
245+
246+
return json.JSONEncoder.default(self, o)

voila/voilite/app.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from ..configuration import VoilaConfiguration
2222
from ..paths import ROOT, collect_static_paths, collect_template_paths
23-
from ..utils import find_all_lab_theme, get_page_config
23+
from ..utils import DateTimeEncoder, find_all_lab_theme, get_page_config
2424
from .exporter import VoiliteExporter
2525
from .voilite_tree_exporter import VoiliteTreeExporter
2626

@@ -66,6 +66,7 @@ class Voilite(Application):
6666
'template': 'VoilaConfiguration.template',
6767
'theme': 'VoilaConfiguration.theme',
6868
'base_url': 'Voilite.base_url',
69+
'contents': 'Voilite.contents',
6970
}
7071

7172
output_prefix = Unicode(
@@ -74,6 +75,10 @@ class Voilite(Application):
7475
help=_('Path to the output directory'),
7576
)
7677

78+
contents = Unicode(
79+
'files', config=True, help=_('Name of the user contents directory')
80+
)
81+
7782
@default('log_level')
7883
def _default_log_level(self):
7984
return logging.INFO
@@ -186,11 +191,14 @@ def copy_static_files(self, federated_extensions: TypeList[str]) -> None:
186191
)
187192

188193
template_name = self.voilite_configuration.template
189-
ignore_func = lambda dir, files: [
190-
f
191-
for f in files
192-
if os.path.isfile(os.path.join(dir, f)) and f[-3:] != 'css'
193-
]
194+
195+
def ignore_func(dir, files) -> List[str]:
196+
return [
197+
f
198+
for f in files
199+
if os.path.isfile(os.path.join(dir, f)) and f[-3:] != 'css'
200+
]
201+
194202
for root in self.static_paths:
195203
abspath = os.path.abspath(root)
196204
if os.path.exists(abspath):
@@ -215,6 +223,45 @@ def copy_static_files(self, federated_extensions: TypeList[str]) -> None:
215223
)
216224
shutil.copytree(theme[1], theme_dst, dirs_exist_ok=True)
217225

226+
# Copy additional files
227+
in_files_path = os.path.join(os.getcwd(), self.contents)
228+
out_files_path = os.path.join(dest_static_path, 'files')
229+
if os.path.exists(in_files_path):
230+
shutil.copytree(
231+
in_files_path, os.path.join(out_files_path, self.contents)
232+
)
233+
self.index_user_files()
234+
235+
def index_user_files(self, current_path='') -> None:
236+
237+
dest_static_path = os.path.join(os.getcwd(), self.output_prefix)
238+
cm = self.contents_manager
239+
contents = cm.get(current_path)
240+
contents['content'] = sorted(
241+
contents['content'], key=lambda i: i['name']
242+
)
243+
if current_path == '':
244+
contents['content'] = list(
245+
filter(
246+
lambda c: c['type'] == 'directory'
247+
and c['name'] == self.contents,
248+
contents['content'],
249+
)
250+
)
251+
for item in contents['content']:
252+
if item['type'] == 'directory':
253+
self.index_user_files(item['path'])
254+
255+
output_dir = os.path.join(
256+
dest_static_path, 'api', 'contents', current_path
257+
)
258+
if not os.path.exists(output_dir):
259+
os.makedirs(output_dir, exist_ok=True)
260+
with open(os.path.join(output_dir, 'all.json'), 'w') as f:
261+
json.dump(
262+
contents, f, sort_keys=True, indent=2, cls=DateTimeEncoder
263+
)
264+
218265
def convert_notebook(
219266
self,
220267
nb_path: str,
@@ -262,6 +309,7 @@ def convert_directory(self, page_config):
262309
contents_manager=self.contents_manager,
263310
base_url=self.base_url,
264311
page_config=page_config,
312+
contents_directory=self.contents,
265313
)
266314
nb_paths = tree_exporter.from_contents()
267315
for nb in nb_paths:

voila/voilite/voilite_tree_exporter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
self.notebook_paths = []
3535

3636
self.page_config = self.filter_extensions(page_config)
37+
self.contents_directory = kwargs.get('contents_directory', None)
3738

3839
def filter_extensions(self, page_config: Dict) -> Dict:
3940

@@ -52,7 +53,11 @@ def filter_extensions(self, page_config: Dict) -> Dict:
5253
def allowed_content(self, content: Dict) -> bool:
5354
if content['type'] == 'notebook':
5455
return True
55-
if content['type'] == 'directory' and content['name'] != '_output':
56+
if (
57+
content['type'] == 'directory'
58+
and content['name'] != '_output'
59+
and content['name'] != self.contents_directory
60+
):
5661
return True
5762
__, ext = os.path.splitext(content.get('path'))
5863
return ext in self.allowed_extensions

0 commit comments

Comments
 (0)