Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Mimic classic notebook behavior for /tree/<path>
Browse files Browse the repository at this point in the history
- Show directory listing if path is a directory
- Show notebook if path is a notebook
- Show editor if path is a file

Copied from https://github.com/jupyter/notebook/blob/6fe9755971dba3357054766806d56cb8c6c1bca7/notebook/tree/handlers.py#L39

Fixes #153
  • Loading branch information
yuvipanda committed Jun 8, 2021
1 parent 0f7c026 commit 67d0ab4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions retrolab/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin,
)
from jupyter_server.utils import url_path_join as ujoin
from jupyter_server.utils import url_path_join as ujoin, url_escape
from jupyterlab.commands import get_app_dir, get_user_settings_dir, get_workspaces_dir
from jupyterlab_server import LabServerApp
from jupyterlab_server.config import get_page_config, recursive_update, LabConfig
from jupyterlab_server.handlers import is_url, _camelCase
from nbclassic.shim import NBClassicConfigShimMixin
from tornado import web
from tornado.gen import maybe_future
from traitlets import Bool

from ._version import __version__
Expand Down Expand Up @@ -98,9 +99,34 @@ def get(self):

class RetroTreeHandler(RetroHandler):
@web.authenticated
def get(self, path=None):
tpl = self.render_template("tree.html", page_config=self.get_page_config())
return self.write(tpl)
async def get(self, path=None):
"""
Display appropriate page for given path.
- A directory listing is shown if path is a directory
- Redirected to notebook page if path is a notebook
- Redirected to editor page if path is a file
"""
path = path.strip('/')
cm = self.contents_manager

if await maybe_future(cm.dir_exists(path=path)):
if await maybe_future(cm.is_hidden(path)) and not cm.allow_hidden:
self.log.info("Refusing to serve hidden directory, via 404 Error")
raise web.HTTPError(404)
tpl = self.render_template("tree.html", page_config=self.get_page_config())
return self.write(tpl)
elif await maybe_future(cm.file_exists(path)):
# it's not a directory, we have redirecting to do
model = await maybe_future(cm.get(path, content=False))
service = 'notebooks' if model['type'] == 'notebook' else 'edit'
url = ujoin(
self.base_url, 'retro', service, url_escape(path),
)
self.log.debug("Redirecting %s to %s", self.request.path, url)
self.redirect(url)
else:
raise web.HTTPError(404)


class RetroTerminalHandler(RetroHandler):
Expand Down

0 comments on commit 67d0ab4

Please sign in to comment.