diff --git a/IPython/frontend/html/notebook/notebook.py b/IPython/frontend/html/notebook/notebook.py index 0cc8725..164e11f 100644 --- a/IPython/frontend/html/notebook/notebook.py +++ b/IPython/frontend/html/notebook/notebook.py @@ -1,6 +1,8 @@ +import datetime import json import logging import os +import urllib import zmq @@ -103,6 +105,53 @@ class ShellStreamHandler(ZMQStreamHandler): stream_name = 'shell' +class NotebookRootHandler(web.RequestHandler): + + def get(self): + files = os.listdir(os.getcwd()) + files = [file for file in files if file.endswith(".nb")] + self.write(json.dumps(files)) + + +class NotebookHandler(web.RequestHandler): + + SUPPORTED_METHODS = ("GET", "DELETE", "PUT") + + def find_path(self, filename): + filename = urllib.unquote(filename) + ".nb" + path = os.path.join(os.getcwd(), filename) + return path + + def get(self, filename): + path = self.find_path(filename) + if not os.path.isfile(path): + raise web.HTTPError(404) + info = os.stat(path) + self.set_header("Content-Type", "application/unknown") + self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp( + info.st_mtime)) + f = open(path, "r") + try: + self.finish(f.read()) + finally: + f.close() + + def put(self, filename): + path = self.find_path(filename) + f = open(path, "w") + f.write(self.request.body) + f.close() + self.finish() + + def delete(self, filename): + path = self.find_path(filename) + if not os.path.isfile(path): + raise web.HTTPError(404) + os.unlink(path) + self.set_status(204) + self.finish() + + class NotebookApplication(web.Application): def __init__(self): @@ -112,6 +161,8 @@ class NotebookApplication(web.Application): (r"/kernels/%s/sessions" % (_kernel_id_regex,), SessionHandler), (r"/kernels/%s/sessions/%s/iopub" % (_kernel_id_regex,_session_id_regex), IOPubStreamHandler), (r"/kernels/%s/sessions/%s/shell" % (_kernel_id_regex,_session_id_regex), ShellStreamHandler), + (r"/notebooks", NotebookRootHandler), + (r"/notebooks/([^/]+)", NotebookHandler) ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index 3cead66..0808b82 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -1,6 +1,26 @@ var IPYTHON = {}; + +// $.get("/notebooks/number2.nb",function (data, status, xhr) {console.log(data);}); +// +// settings = { +// processData : false, +// cache : false, +// type : "DELETE", +// success : function (data, status, xhr) {console.log(data);} +// } +// $.ajax("/notebooks/number2.nb",settings) +// +// settings = { +// processData : false, +// cache : false, +// type : "PUT", +// success : function (data, status, xhr) {console.log(data);} +// } +// $.ajax("/notebooks/number2.nb",settings) + + //============================================================================ // Utilities //============================================================================ diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index 17a56e0..e47d901 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -8,9 +8,8 @@ <link rel="stylesheet" href="static/css/notebook.css" type="text/css" /> <link rel="stylesheet" href="static/jquery/css/jquery.wijmo-open.1.1.3.css" type="text/css" /> <link rel="stylesheet" href="static/jquery/css/themes/aristo/jquery-wijmo.css" type="text/css" /> - <!-- <link rel="stylesheet" href="static/jquery/css/themes/ui-lightness/jquery-ui-1.8.10.custom.css" type="text/css" /> --> - <!-- <script src="static/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript" charset="utf-8"></script> --> <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" charset="utf-8"></script> + </head> <body> @@ -83,6 +82,7 @@ <script src="static/jquery/js/jquery.autogrow.js" type="text/javascript" charset="utf-8"></script> <script src="static/jquery/js/jquery.wijmo-open.1.1.3.min.js" type="text/javascript" charset="utf-8"></script> <script src="static/js/notebook.js" type="text/javascript" charset="utf-8"></script> + </body> </html> \ No newline at end of file