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

Add view handler to serve files when Notebook 7 is installed #287

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions nbclassic/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def initialize_handlers(self):
handlers.extend(load_handlers('nbclassic.tree.handlers'))
handlers.extend(load_handlers('nbclassic.notebook.handlers'))
handlers.extend(load_handlers('nbclassic.edit.handlers'))
handlers.extend(load_handlers('nbclassic.view.handlers'))
self.handlers.extend(handlers)

# Wildcard routes
Expand Down
1 change: 1 addition & 0 deletions nbclassic/view/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tornado handlers for viewing HTML files."""
40 changes: 40 additions & 0 deletions nbclassic/view/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#encoding: utf-8
"""Tornado handlers for the terminal emulator."""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from tornado import web, gen


from jupyter_server.base.handlers import JupyterHandler, path_regex
from jupyter_server.utils import url_escape, ensure_async, url_path_join

from nbclassic import nbclassic_path


class CustomViewHandler(JupyterHandler):
"""Render HTML files within an iframe."""

@web.authenticated
@gen.coroutine
def get(self, path):
"""Get a view on a given path."""

path = path.strip('/')
exists = yield ensure_async(self.contents_manager.file_exists(path))
if not exists:
raise web.HTTPError(404, u'File does not exist: %s' % path)

basename = path.rsplit('/', 1)[-1]
file_url = url_path_join(self.base_url, "files", url_escape(path))
self.write(self.render_template('view.html',
file_url=file_url,
page_title=basename,
)
)


default_handlers = [
(r"{}/view{}".format(nbclassic_path(), path_regex), CustomViewHandler),
]
Loading