Permalink
Browse files

Server side of file based notebook store implemented.

  • Loading branch information...
1 parent 5a8489e commit 5dc428b745bf4660b0dc909d7dd19a69eafe5030 @ellisonbg ellisonbg committed with ellisonbg Mar 25, 2011
@@ -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 @@ def __init__(self):
(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"),
@@ -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
//============================================================================
@@ -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>

0 comments on commit 5dc428b

Please sign in to comment.