Commits

Matthew Turk  committed d7068da

Adding notebooks to the Hub!

  • Participants
  • Parent commits 4491958

Comments (0)

Files changed (2)

File ythub/models.py

 from ythub.local_config import tmp_dir
 import cStringIO
 from utilities import slugify
+from yt.funcs import iterable
 
 class User(Base):
     __tablename__ = "users"
     display_name = "Simulation Output"
     def __init__(self, obj):
         for attr in ['refine_by', 'dimensionality', 'user_id']:
-            setattr(self, attr, int(obj.pop(attr)))
+            # Get rid of lists!
+            val = obj.pop(attr, "0").replace("[","").replace("]","")
+            try:
+                val = int(val)
+            except ValueError:
+                val = 0
+            val = int(val)
+            setattr(self, attr, val)
         for attr in ['current_time', 'omega_lambda', 'omega_matter',
                      'current_redshift', 'hubble_constant',
                      'cosmological_simulation', 'created']:
             url = "https://%s%s" % (ss, url[url.find(ss) + len(ss):])
         return "hg clone %s" % url
             
-
     def opt(self): return ""
 
 view_proxies['project'] = ProjectProxy
 
+class NotebookProxy(LoadableDataProxy):
+    display_name = "IPython Notebook"
+    loaded = False
+    def __init__(self, obj):
+        self.hub_id = obj['hub_id']
+        self.chunk_names = obj['chunk_names']
+        self.obj_type = obj['obj_type']
+        self.created = float(obj['created'])
+        self.user_id = obj['user_id']
+        self.title = obj['title']
+        self._user = User.query.filter_by(id = self.user_id).first()
+
+    def opt(self):
+        return ""
+
+    @property
+    def user(self):
+        return self._user
+
+    def descr(self):
+        return self.title
+
+    @property
+    def _url(self):
+        return "/notebooks/%s.ipynb" % (self.hub_id)
+
+    @property
+    def _nburl(self):
+        nb_url = "http://nbviewer.ipython.org/url/"
+        my_url = "hub.yt-project.org%s" % (self._url)
+        return "%s%s" % (nb_url, my_url)
+
+    def url(self):
+        return "<a href='%s'>Download</a> / <a href='%s'>View</a>" % (
+            self._url, self._nburl)
+
+view_proxies['notebook'] = NotebookProxy
+
 class ExpiringDataContainer(dict):
     def __init__(self, expiration = 3600):
         self.expiration = expiration

File ythub/views.py

     container['map_server_%s' % map_id] = MapTileView(map, map.field)
     return render_template("mapserver.html", map_id = map_id), 200
 
+@app.route("/notebooks/<notebook_id>.ipynb")
+def notebook_view(notebook_id = None):
+    nb = obj_query(notebook_id, "notebook")
+    if len(nb) != 1:
+        return make_response("NOTEBOOK NOT FOUND (%s)" % len(nb), 404)
+    nb = nb[0]
+    rv = make_response(nb['notebook'].tostring())
+    rv.mimetype = "text/plain"
+    return rv
+
 @app.route("/vertices/<vertices_id>/")
 def vertices_display(vertices_id):
     vertices = obj_query(vertices_id, "vertices")