Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect paths from the notebooks route to the tree route if they are directories #7446

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this test!

In addition to this test, maybe it could be interesting to add a UI test to mimic real user interactions: https://github.com/jupyter/notebook/tree/main/ui-tests

But we can track that in a separate issue 👍



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