Skip to content

Commit

Permalink
Redirect paths from the notebooks route to the tree route if they are…
Browse files Browse the repository at this point in the history
… directories (#7446)
  • Loading branch information
andyscho authored Sep 9, 2024
1 parent 43b8cce commit 59f8c30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 10 additions & 2 deletions notebook/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,16 @@ class NotebookHandler(NotebookBaseHandler):
"""A notebook page handler."""

@web.authenticated
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002
"""Get the notebook page."""
async def get(self, path: str = "") -> t.Any:
"""Get the notebook page. Redirect if it's a directory."""
path = path.strip("/")
cm = self.contents_manager

if await ensure_async(cm.dir_exists(path=path)):
url = ujoin(self.base_url, "tree", url_escape(path))
self.log.debug("Redirecting %s to %s since path is a directory", self.request.path, url)
self.redirect(url)
return None
tpl = self.render_template("notebooks.html", page_config=self.get_page_config())
return self.write(tpl)

Expand Down
12 changes: 11 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from tornado.httpclient import HTTPClientError

from notebook.app import JupyterNotebookApp, TreeHandler
from notebook.app import JupyterNotebookApp, NotebookHandler, TreeHandler


@pytest.fixture()
Expand Down Expand Up @@ -32,6 +32,16 @@ async def test_notebook_handler(notebooks, jp_fetch):
html = r.body.decode()
assert "Jupyter Notebook" in html

redirected_url = None

def redirect(self, url):
nonlocal redirected_url
redirected_url = url

NotebookHandler.redirect = redirect
await jp_fetch("notebooks", "jlab_test_notebooks")
assert redirected_url == "/a%40b/tree/jlab_test_notebooks"


async def test_tree_handler(notebooks, notebookapp, jp_fetch):
app: JupyterNotebookApp = notebookapp
Expand Down

0 comments on commit 59f8c30

Please sign in to comment.