From a50ac36bd554366a80f907d4d16b552d53c69ddf 2012-01-06 10:00:14 From: Min RK Date: 2012-01-06 10:00:14 Subject: [PATCH] Merge pull request #1211 from minrk/localfiles Files in notebook-dir are served by the nbserver as `files/`. --- diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index df3b65c..62bc795 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -108,6 +108,7 @@ def authenticate_unless_readonly(f, self, *args, **kwargs): @web.authenticated def auth_f(self, *args, **kwargs): return f(self, *args, **kwargs) + if self.application.read_only: return f(self, *args, **kwargs) else: @@ -175,6 +176,14 @@ class AuthenticatedHandler(RequestHandler): return "%s://%s" % (proto, self.request.host) +class AuthenticatedFileHandler(AuthenticatedHandler, web.StaticFileHandler): + """static files should only be accessible when logged in""" + + @authenticate_unless_readonly + def get(self, path): + return web.StaticFileHandler.get(self, path) + + class ProjectDashboardHandler(AuthenticatedHandler): @authenticate_unless_readonly diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index beb356b..b0360e5 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -48,7 +48,8 @@ from .kernelmanager import MappingKernelManager from .handlers import (LoginHandler, LogoutHandler, ProjectDashboardHandler, NewHandler, NamedNotebookHandler, MainKernelHandler, KernelHandler, KernelActionHandler, IOPubHandler, - ShellHandler, NotebookRootHandler, NotebookHandler, RSTHandler + ShellHandler, NotebookRootHandler, NotebookHandler, RSTHandler, + AuthenticatedFileHandler, ) from .notebookmanager import NotebookManager @@ -103,7 +104,8 @@ class NotebookWebApplication(web.Application): (r"/kernels/%s/shell" % _kernel_id_regex, ShellHandler), (r"/notebooks", NotebookRootHandler), (r"/notebooks/%s" % _notebook_id_regex, NotebookHandler), - (r"/rstservice/render", RSTHandler) + (r"/rstservice/render", RSTHandler), + (r"/files/(.*)", AuthenticatedFileHandler, {'path' : notebook_manager.notebook_dir}), ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), @@ -280,7 +282,7 @@ class NotebookApp(BaseIPythonApplication): static_path = self.webapp_settings.get("static_path", os.path.join(os.path.dirname(__file__), "static")) if os.path.exists(os.path.join(static_path, 'mathjax', "MathJax.js")): self.log.info("Using local MathJax") - return u"static/mathjax/MathJax.js" + return u"/static/mathjax/MathJax.js" else: self.log.info("Using MathJax from CDN") return u"http://cdn.mathjax.org/mathjax/latest/MathJax.js" diff --git a/IPython/frontend/html/notebook/templates/layout.html b/IPython/frontend/html/notebook/templates/layout.html index 8d5e4d2..407e2ab 100644 --- a/IPython/frontend/html/notebook/templates/layout.html +++ b/IPython/frontend/html/notebook/templates/layout.html @@ -6,10 +6,10 @@ {% block title %}IPython Notebook{% end %} - - - - + + + + {% block stylesheet %} {% end %} @@ -21,7 +21,7 @@ - - - - - + + + + + {% block script %} {% end %} diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index ab160a1..11daece 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -15,20 +15,20 @@ window.mathjax_url = "{{mathjax_url}}"; - - - - - - + + + + + + - + - - - - - + + + + + {% comment In the notebook, the read-only flag is used to determine %} {% comment whether to hide the side panels and switch off input %} @@ -42,7 +42,7 @@ > - - - + + + - - - - - - - - + + + + + + + + - + - + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/IPython/frontend/html/notebook/templates/projectdashboard.html b/IPython/frontend/html/notebook/templates/projectdashboard.html index d3036af..3dfc52b 100644 --- a/IPython/frontend/html/notebook/templates/projectdashboard.html +++ b/IPython/frontend/html/notebook/templates/projectdashboard.html @@ -5,7 +5,7 @@ IPython Dashboard {% end %} {% block stylesheet %} - + {% end %} {% block meta %} @@ -38,6 +38,6 @@ data-base-kernel-url={{base_kernel_url}} {% end %} {% block script %} - - + + {% end %} diff --git a/docs/examples/notebooks/00_notebook_tour.ipynb b/docs/examples/notebooks/00_notebook_tour.ipynb index dc99083..a62c2de 100644 --- a/docs/examples/notebooks/00_notebook_tour.ipynb +++ b/docs/examples/notebooks/00_notebook_tour.ipynb @@ -778,6 +778,45 @@ { "cell_type": "markdown", "source": [ + "## Local Files", + "", + "The above examples embed images and video from the notebook filesystem in the output", + "areas of code cells. It is also possible to request these files directly in markdown cells", + "if they reside in the notebook directory via relative urls prefixed with `files/`:", + "", + " files/[subdirectory/]", + "", + "", + "For example, in the example notebook folder, we have the Python logo, addressed as:", + "", + " ", + "", + "", + "", + "and a video with the HTML5 video tag:", + "", + "