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

Fix opening files from the CLI #6946

Merged
merged 1 commit into from
Jun 26, 2023
Merged

Fix opening files from the CLI #6946

merged 1 commit into from
Jun 26, 2023

Conversation

jtpio
Copy link
Member

@jtpio jtpio commented Jun 23, 2023

Investigate ways to fix #6914

@github-actions
Copy link
Contributor

Binder 👈 Launch a Binder on branch jtpio/notebook/redirect-regex

@jtpio jtpio added this to the 7.0 milestone Jun 23, 2023
@jtpio
Copy link
Member Author

jtpio commented Jun 23, 2023

So the idea if that we should be able to open files from the CLI by using the existing /tree/<file> redirection logic.

@jtpio
Copy link
Member Author

jtpio commented Jun 23, 2023

cc @parmentelat if you want to try with the built artifacts: https://github.com/jupyter/notebook/actions/runs/5356963835?pr=6946

@jtpio jtpio marked this pull request as ready for review June 26, 2023 06:50
@jtpio
Copy link
Member Author

jtpio commented Jun 26, 2023

For reference file_url_prefix is documented here in Jupyter Server: https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html#anatomy-of-an-extensionapp

file_url_prefix: the prefix URL added when opening a document directly from the command line. For example, classic Notebook uses /notebooks to open a document at http://localhost:8888/notebooks/path/to/notebook.ipynb.

@@ -234,7 +234,7 @@ class JupyterNotebookApp(NotebookConfigShimMixin, LabServerApp):
app_version = version
extension_url = "/"
default_url = Unicode("/tree", config=True, help="The default URL to redirect to from `/`")
file_url_prefix = "/notebooks"
file_url_prefix = "/tree"
Copy link
Member Author

Choose a reason for hiding this comment

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

So the idea of using /tree is that there is already a redirect handler to open either with /edit or /notebooks:

notebook/notebook/app.py

Lines 127 to 160 in 7f1a2f4

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 ensure_async(cm.dir_exists(path=path)):
if await ensure_async(cm.is_hidden(path)) and not cm.allow_hidden:
self.log.info("Refusing to serve hidden directory, via 404 Error")
raise web.HTTPError(404)
# Set treePath for routing to the directory
page_config = self.get_page_config()
page_config["treePath"] = path
tpl = self.render_template("tree.html", page_config=page_config)
return self.write(tpl)
elif await ensure_async(cm.file_exists(path)):
# it's not a directory, we have redirecting to do
model = await ensure_async(cm.get(path, content=False))
if model["type"] == "notebook":
url = ujoin(self.base_url, "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)

@jtpio jtpio merged commit 27c00bc into jupyter:main Jun 26, 2023
@jtpio jtpio deleted the redirect-regex branch June 26, 2023 07:50
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cannot open a jupytext notebook in nb7
1 participant