-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reuse voila to be a server extension + tree view + autoreload
The voila handler can be used both in the standalone version, as well as jupyter server extension. The three handler which is based on the notebook code shows a list of directories and notebooks. For development purposes the watchdog handler will send a reload message over the websocket when either the javascript, templates, or the notebook file is modified, or when the server autoreloads due to code changes. (Use with `voila --autoreload=True`)
- Loading branch information
1 parent
e32a1e2
commit 28faacc
Showing
9 changed files
with
435 additions
and
66 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
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,45 @@ | ||
import tornado.web | ||
|
||
from jupyter_server.base.handlers import JupyterHandler | ||
|
||
import nbformat | ||
from nbconvert.preprocessors.execute import executenb | ||
from nbconvert import HTMLExporter | ||
|
||
from .paths import TEMPLATE_ROOT | ||
|
||
|
||
class VoilaHandler(JupyterHandler): | ||
def initialize(self, notebook_path=None, strip_sources=True): | ||
self.notebook_path = notebook_path | ||
self.strip_sources = strip_sources | ||
|
||
@tornado.web.authenticated | ||
@tornado.gen.coroutine | ||
def get(self, path=None): | ||
if path: | ||
path = path.strip('/') # remove leading / | ||
path += '.ipynb' # when used as a jupyter server extension, we don't use the extension | ||
# if the handler got a notebook_path argument, always serve that | ||
notebook_path = self.notebook_path or path | ||
|
||
notebook = nbformat.read(notebook_path, as_version=4) | ||
|
||
# Ignore requested kernel name and make use of the one specified in the notebook. | ||
kernel_name = notebook.metadata.get('kernelspec', {}).get('name', self.kernel_manager.default_kernel_name) | ||
|
||
# Launch kernel and execute notebook. | ||
kernel_id = yield tornado.gen.maybe_future(self.kernel_manager.start_kernel(kernel_name=kernel_name)) | ||
km = self.kernel_manager.get_kernel(kernel_id) | ||
result = executenb(notebook, km=km) | ||
|
||
# render notebook to html | ||
resources = dict(kernel_id=kernel_id) | ||
html, resources = HTMLExporter(template_file=str(TEMPLATE_ROOT / 'voila.tpl'), exclude_input=self.strip_sources, | ||
exclude_output_prompt=self.strip_sources, exclude_input_prompt=self.strip_sources | ||
).from_notebook_node(result, resources=resources) | ||
|
||
# Compose reply | ||
self.set_header('Content-Type', 'text/html') | ||
self.write(html) | ||
|
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,7 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
ROOT = Path(os.path.dirname(__file__)) | ||
STATIC_ROOT = ROOT / 'static' | ||
TEMPLATE_ROOT = ROOT / 'templates' | ||
|
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,37 @@ | ||
import os | ||
import gettext | ||
from pathlib import Path | ||
|
||
from jinja2 import Environment, FileSystemLoader | ||
|
||
import tornado.web | ||
|
||
from jupyter_server.utils import url_path_join | ||
from jupyter_server.base.handlers import path_regex | ||
|
||
from .paths import ROOT, TEMPLATE_ROOT, STATIC_ROOT | ||
from .handler import VoilaHandler | ||
from .treehandler import VoilaTreeHandler | ||
from .watchdog import WatchDogHandler | ||
|
||
|
||
def load_jupyter_server_extension(server_app): | ||
web_app = server_app.web_app | ||
|
||
jenv_opt = {"autoescape": True} | ||
env = Environment(loader=FileSystemLoader(str(TEMPLATE_ROOT)), extensions=['jinja2.ext.i18n'], **jenv_opt) | ||
web_app.settings['voila_jinja2_env'] = env | ||
|
||
nbui = gettext.translation('nbui', localedir=str(ROOT / 'i18n'), fallback=True) | ||
env.install_gettext_translations(nbui, newstyle=False) | ||
|
||
host_pattern = '.*$' | ||
web_app.add_handlers(host_pattern, [ | ||
(url_path_join(web_app.settings['base_url'], '/voila/render' + path_regex), VoilaHandler), | ||
(url_path_join(web_app.settings['base_url'], '/voila/watchdog' + path_regex), WatchDogHandler), | ||
(url_path_join(web_app.settings['base_url'], '/voila'), VoilaTreeHandler), | ||
(url_path_join(web_app.settings['base_url'], '/voila/tree' + path_regex), VoilaTreeHandler), | ||
(url_path_join(web_app.settings['base_url'], '/voila/static/(.*)'), tornado.web.StaticFileHandler, | ||
{'path': str(STATIC_ROOT)}) | ||
|
||
]) |
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.