Skip to content

Commit

Permalink
fix: allow voila to run a server extension for notebook <5
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Sep 30, 2019
1 parent 9f51f17 commit 6e772b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion voila/server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .treehandler import VoilaTreeHandler
from .static_file_handler import MultiStaticFileHandler, WhiteListFileHandler
from .configuration import VoilaConfiguration
from .utils import get_server_root_dir


def load_jupyter_server_extension(server_app):
Expand Down Expand Up @@ -67,7 +68,7 @@ def load_jupyter_server_extension(server_app):
{
'whitelist': voila_configuration.file_whitelist,
'blacklist': voila_configuration.file_blacklist,
'path': os.path.expanduser(web_app.settings['server_root_dir']),
'path': os.path.expanduser(get_server_root_dir(web_app.settings)),
},
),
])
Expand Down
4 changes: 3 additions & 1 deletion voila/treehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.utils import url_path_join, url_escape

from .utils import get_server_root_dir


class VoilaTreeHandler(JupyterHandler):
def initialize(self, **kwargs):
Expand Down Expand Up @@ -69,7 +71,7 @@ def allowed_content(content):
breadcrumbs=breadcrumbs,
contents=contents,
terminals_available=False,
server_root=self.settings['server_root_dir']))
server_root=get_server_root_dir(self.settings)))
elif cm.file_exists(path):
# it's not a directory, we have redirecting to do
model = cm.get(path, content=False)
Expand Down
17 changes: 17 additions & 0 deletions voila/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os


def get_server_root_dir(settings):
# notebook >= 5.0.0 has this in the settings
if 'server_root_dir' in settings:
return settings['server_root_dir']

# This copies the logic added in the notebook in
# https://github.com/jupyter/notebook/pull/2234
contents_manager = settings['contents_manager']
root_dir = contents_manager.root_dir
home = os.path.expanduser('~')
if root_dir.startswith(home + os.path.sep):
# collapse $HOME to ~
root_dir = '~' + root_dir[len(home):]
return root_dir

0 comments on commit 6e772b4

Please sign in to comment.