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

Commit

Permalink
Merge pull request #159 from yuvipanda/tree-redirect
Browse files Browse the repository at this point in the history
Mimic classic notebook behavior for /tree/<path>
  • Loading branch information
jtpio authored Jun 9, 2021
2 parents 0f7c026 + b8928e4 commit 752e21f
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 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,35 @@ 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
- Render the raw file if path is any other 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))
if model['type'] == 'notebook':
url = ujoin(self.base_url, 'retro/notebooks', url_escape(path))
else:
# Return raw content if file is not a notebook
url = ujoin(self.base_url, 'files', 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 752e21f

Please sign in to comment.