handlers.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Tornado handlers for viewing HTML files."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from jupyter_core.utils import ensure_async
  5. from tornado import web
  6. from jupyter_server.auth.decorator import authorized
  7. from ..base.handlers import JupyterHandler, path_regex
  8. from ..utils import url_escape, url_path_join
  9. AUTH_RESOURCE = "contents"
  10. class ViewHandler(JupyterHandler):
  11. """Render HTML files within an iframe."""
  12. auth_resource = AUTH_RESOURCE
  13. @web.authenticated
  14. @authorized
  15. async def get(self, path):
  16. """Get a view on a given path."""
  17. path = path.strip("/")
  18. if not await ensure_async(self.contents_manager.file_exists(path)):
  19. raise web.HTTPError(404, "File does not exist: %s" % path)
  20. basename = path.rsplit("/", 1)[-1]
  21. file_url = url_path_join(self.base_url, "files", url_escape(path))
  22. self.write(self.render_template("view.html", file_url=file_url, page_title=basename))
  23. default_handlers = [
  24. (r"/view%s" % path_regex, ViewHandler),
  25. ]