-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b10cf6
commit 089dd02
Showing
7 changed files
with
403 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from fps.config import PluginModel, get_config # type: ignore | ||
from fps.hooks import register_config, register_plugin_name # type: ignore | ||
|
||
|
||
class VoilaConfig(PluginModel): | ||
notebook_path: str = "" | ||
|
||
|
||
def get_voila_config(): | ||
return get_config(VoilaConfig) | ||
|
||
|
||
c = register_config(VoilaConfig) | ||
n = register_plugin_name("Voila") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import sys | ||
import os | ||
import uuid | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from voila.handler import _VoilaHandler, _get | ||
|
||
from mimetypes import guess_type | ||
from fastapi import APIRouter, Depends | ||
from fastapi.responses import RedirectResponse, HTMLResponse, Response | ||
from fastapi.staticfiles import StaticFiles | ||
from fps.hooks import register_router # type: ignore | ||
from fps_kernels.kernel_server.server import KernelServer, kernels # type: ignore | ||
from kernel_driver import KernelDriver | ||
from kernel_driver.driver import receive_message | ||
|
||
from .config import get_voila_config | ||
|
||
|
||
class FPSVoilaHandler(_VoilaHandler): | ||
is_fps = True | ||
fps_arguments = {} | ||
html = [] | ||
|
||
def redirect(self, url): | ||
return RedirectResponse(url) | ||
|
||
def write(self, html): | ||
self.html += [html] | ||
|
||
def flush(self): | ||
pass | ||
|
||
def return_html(self): | ||
return HTMLResponse("".join(self.html)) | ||
|
||
def get_argument(self, name, default): | ||
if self.fps_arguments[name] is None: | ||
return default | ||
return self.fps_arguments[name] | ||
|
||
|
||
def init_voila_handler( | ||
notebook_path, | ||
template_paths, | ||
config, | ||
voila_configuration, | ||
contents_manager, | ||
base_url, | ||
kernel_manager, | ||
kernel_spec_manager, | ||
allow_remote_access, | ||
autoreload, | ||
voila_jinja2_env, | ||
jinja2_env, | ||
static_path, | ||
server_root_dir, | ||
config_manager, | ||
static_paths, | ||
): | ||
global fps_voila_handler | ||
fps_voila_handler = FPSVoilaHandler() | ||
fps_voila_handler.initialize( | ||
notebook_path=notebook_path, | ||
template_paths=template_paths, | ||
traitlet_config=config, | ||
voila_configuration=voila_configuration, | ||
) | ||
fps_voila_handler.contents_manager = contents_manager | ||
fps_voila_handler.base_url = base_url | ||
fps_voila_handler.kernel_manager = kernel_manager | ||
fps_voila_handler.kernel_spec_manager = kernel_spec_manager | ||
fps_voila_handler.allow_remote_access = allow_remote_access | ||
fps_voila_handler.autoreload = autoreload | ||
fps_voila_handler.voila_jinja2_env = voila_jinja2_env | ||
fps_voila_handler.jinja2_env = jinja2_env | ||
fps_voila_handler.static_path = static_path | ||
fps_voila_handler.server_root_dir = server_root_dir | ||
fps_voila_handler.config_manager = config_manager | ||
fps_voila_handler.static_paths = static_paths | ||
|
||
|
||
router = APIRouter() | ||
|
||
@router.get("/") | ||
async def get_root(voila_template: Optional[str] = None, voila_theme: Optional[str] = None, voila_config=Depends(get_voila_config)): | ||
fps_voila_handler.fps_arguments["voila-template"] = voila_template | ||
fps_voila_handler.fps_arguments["voila-theme"] = voila_theme | ||
path = "" #voila_config.notebook_path or "/" | ||
return await _get(fps_voila_handler, path) | ||
|
||
@router.get("/voila/static/{path}") | ||
def get_file1(path): | ||
return get_file(path) | ||
|
||
@router.get("/voila/templates/lab/static/{path:path}") | ||
def get_file2(path): | ||
return get_file(path) | ||
|
||
def get_file(path): | ||
for i, static_path in enumerate(fps_voila_handler.static_paths): | ||
file_path = Path(static_path) / path | ||
if os.path.exists(file_path): | ||
with open(file_path) as f: | ||
content = f.read() | ||
content_type, _ = guess_type(file_path) | ||
return Response(content, media_type=content_type) | ||
|
||
r = register_router(router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from setuptools import setup, find_packages # type: ignore | ||
|
||
setup( | ||
name="fps_voila", | ||
version="0.0.1", | ||
packages=find_packages(), | ||
install_requires=["fps", "fps-kernels", "aiofiles"], | ||
entry_points={ | ||
"fps_router": ["fps-voila = fps_voila.routes"], | ||
"fps_config": ["fps-voila = fps_voila.config"], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.