Skip to content

Commit

Permalink
Handle static files
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Nov 17, 2022
1 parent a311337 commit 03ed782
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
23 changes: 17 additions & 6 deletions voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#############################################################################

import asyncio
import datetime
import json
import os
import threading
Expand All @@ -17,16 +18,16 @@
from typing import Awaitable, Dict, List, Tuple

import websockets
from jupyter_core.paths import jupyter_path
from jupyter_server.utils import url_path_join
from jupyterlab_server.config import get_page_config as gpc
from jupyterlab_server.config import recursive_update
from jupyterlab_server.themes_handler import ThemesHandler
from markupsafe import Markup
from nbconvert.exporters.html import find_lab_theme

from jupyter_core.paths import jupyter_path
from jupyterlab_server.config import get_page_config as gpc, recursive_update
from jupyter_server.utils import url_path_join

from .static_file_handler import TemplateStaticFileHandler
from ._version import __version__
from .static_file_handler import TemplateStaticFileHandler


class ENV_VARIABLE(str, Enum):
Expand Down Expand Up @@ -60,7 +61,7 @@ def get_server_root_dir(settings):
home = os.path.expanduser('~')
if root_dir.startswith(home + os.path.sep):
# collapse $HOME to ~
root_dir = '~' + root_dir[len(home):]
root_dir = '~' + root_dir[len(home) :]
return root_dir


Expand Down Expand Up @@ -233,3 +234,13 @@ def find_all_lab_theme() -> List[Tuple[str, str]]:
pass

return theme_list


class DateTimeEncoder(json.JSONEncoder):
"""A custom date-aware JSON encoder"""

def default(self, o):
if isinstance(o, datetime.datetime):
return o.isoformat().replace('+00:00', 'Z')

return json.JSONEncoder.default(self, o)
47 changes: 46 additions & 1 deletion voila/voilite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from ..configuration import VoilaConfiguration
from ..paths import ROOT, collect_static_paths, collect_template_paths
from ..utils import find_all_lab_theme, get_page_config
from ..utils import DateTimeEncoder, find_all_lab_theme, get_page_config
from .exporter import VoiliteExporter
from .voilite_tree_exporter import VoiliteTreeExporter

Expand Down Expand Up @@ -66,6 +66,7 @@ class Voilite(Application):
'template': 'VoilaConfiguration.template',
'theme': 'VoilaConfiguration.theme',
'base_url': 'Voilite.base_url',
'contents': 'Voilite.contents'
}

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

contents = Unicode(
'files', config=True, help=_('Name of the user contents directory')
)

@default('log_level')
def _default_log_level(self):
return logging.INFO
Expand Down Expand Up @@ -215,6 +220,45 @@ def copy_static_files(self, federated_extensions: TypeList[str]) -> None:
)
shutil.copytree(theme[1], theme_dst, dirs_exist_ok=True)

# Copy additional files
in_files_path = os.path.join(os.getcwd(), self.contents)
out_files_path = os.path.join(dest_static_path, 'files')
if os.path.exists(in_files_path):
shutil.copytree(
in_files_path, os.path.join(out_files_path, self.contents)
)
self.index_user_files()

def index_user_files(self, current_path='') -> None:

dest_static_path = os.path.join(os.getcwd(), self.output_prefix)
cm = self.contents_manager
contents = cm.get(current_path)
contents['content'] = sorted(
contents['content'], key=lambda i: i['name']
)
if current_path == '':
contents['content'] = list(
filter(
lambda c: c['type'] == 'directory'
and c['name'] == self.contents,
contents['content'],
)
)
for item in contents['content']:
if item['type'] == 'directory':
self.index_user_files(item['path'])

output_dir = os.path.join(
dest_static_path, 'api', 'contents', current_path
)
if not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
with open(os.path.join(output_dir, 'all.json'), 'w') as f:
json.dump(
contents, f, sort_keys=True, indent=2, cls=DateTimeEncoder
)

def convert_notebook(
self,
nb_path: str,
Expand Down Expand Up @@ -262,6 +306,7 @@ def convert_directory(self, page_config):
contents_manager=self.contents_manager,
base_url=self.base_url,
page_config=page_config,
contents_directory=self.contents
)
nb_paths = tree_exporter.from_contents()
for nb in nb_paths:
Expand Down
7 changes: 6 additions & 1 deletion voila/voilite/voilite_tree_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
self.notebook_paths = []

self.page_config = self.filter_extensions(page_config)
self.contents_directory = kwargs.get('contents_directory', None)

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

Expand All @@ -52,7 +53,11 @@ def filter_extensions(self, page_config: Dict) -> Dict:
def allowed_content(self, content: Dict) -> bool:
if content['type'] == 'notebook':
return True
if content['type'] == 'directory' and content['name'] != '_output':
if (
content['type'] == 'directory'
and content['name'] != '_output'
and content['name'] != self.contents_directory
):
return True
__, ext = os.path.splitext(content.get('path'))
return ext in self.allowed_extensions
Expand Down

0 comments on commit 03ed782

Please sign in to comment.