Commits

Matthew Turk committed aa5c99e

Starting to split the widgets out, abstracting them a bit to allow more general
passage of HTML and response values back and forth

Comments (0)

Files changed (3)

bottle/httprepl.html

             }
         }
     }
-    h.open("POST", "panner_widgets", true);
+    h.open("POST", "widget_action", true);
     h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     h.send("widget_id=" + escape(widget_id) + "&widget_action=" + escape(widget_action));
 }

bottle/web_widgets.py

 import abc
 import numpy as na
 import uuid
+import base64
+import tempfile
 
 from yt.utilities.amr_utils import write_png_to_file
 from yt.visualization.image_writer import map_to_colors
     def create_object_creator(cls, widgets, local_vars):
         pass
 
+    @abc.abstractmethod
+    def do_action(self, parameters):
+        pass
+
 _image_panner_source = r"""
 <form name="test_form_%(my_id)s">
 <img src="" width=512 height=512 id="pan_image_%(my_id)s" style="float:left;">
         YTWebWidget.__init__(self, my_id)
         self.panner = panner
 
-    def do_action(self, action):
+    def do_action(self, parameters):
+        action = parameters['widget_action']
         if action == "L":
             self.panner.pan_rel_x(+0.1)
         elif action == "R":
             self.panner.zoom(0.1)
         elif action == "REFRESH":
             self.panner.zoom(1.0)
-        return scale_buffer(self.panner.buffer)
+        nb = self.scale_buffer(self.panner.buffer)
+        iv = base64.encodestring(nb)
+        return dict( image_data = iv )
 
     def scale_buffer(self, buffer):
         image = na.log10(buffer)
     def create_object_creator(cls, widgets, local_vars):
         def add_panner(panner):
             unique_id = str(uuid.uuid1())
-            widget = cls(panner, unique_id)
+            widget = cls(unique_id, panner)
             widgets[unique_id] = widget
             s = cls._widget_source % dict(my_id = unique_id)
             local_vars['new_values'] = s
 import sys
 import traceback
 
-import tempfile
 import numpy as na
 
 import base64
     return result
 
 # This needs to get moved in to the base class we implement
-@preroute("/panner_widgets", method="POST")
+@preroute("/widget_action", method="POST")
 def panner_widgets():
     widget_id = request.POST['widget_id']
-    widget_action = request.POST['widget_action']
     widget = repl._widgets.get(widget_id, None)
     if not isinstance(widget, YTWebWidget):
         print widget_id, repl._widgets.keys()
         return ""
-    s = base64.encodestring(widget.do_action(widget_action))
-    return json.dumps( dict(image_data = s) )
-panner_widgets.exposed = True
+    rv = widget.do_action(request.POST)
+    return json.dumps( rv ) 
 
 if __name__ == '__main__':
     debug(mode=True)
     print "     http://localhost:8080/%s/" % token
     print
     print
+    if "-o" in sys.argv:
+        def open_browser():
+            """Start a browser after waiting for half a second."""
+            import webbrowser, threading
+            def _open_browser():
+                webbrowser.open('http://localhost:%s/%s/' % (8080, token))
+            thread = threading.Timer(0.5, _open_browser)
+            thread.start()
+        open_browser()
     run(host='localhost', port=8080)
 
-# from yt.mods import *; pf = load("/Users/matthewturk/Research/data/RD0005-mine/RedshiftOutput0005");proj = pf.h.proj(0,"Density");from yt.visualization.image_panner.api import VariableMeshPanner; vmp = VariableMeshPanner(proj, (512, 512), "Density"); add_image_panner(vmp) 
+# from yt.mods import *; pf = load("/Users/matthewturk/Research/data/RD0005-mine/RedshiftOutput0005");proj = pf.h.proj(0,"Density");from yt.visualization.image_panner.api import VariableMeshPanner; vmp = VariableMeshPanner(proj, (512, 512), "Density"); add_image_panner(vmp)
 # from yt.mods import *; pf = load("/Users/matthewturk/Research/data/RD0005-mine/RedshiftOutput0005");proj = pf.h.proj(0,"Density");from yt.visualization.image_panner.api import VariableMeshPanner; vmp = VariableMeshPanner(proj, (512, 512), "Density", add_image); vmp.zoom(1.0)