Source

yt_ajax / bottle / ext_widgets.py

Full commit
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

class YTExtWebWidget(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, my_id):
        self.my_id = my_id

    @abc.abstractproperty
    def _widget_name(self):
        pass

    @abc.abstractproperty
    def _widget_source(self):
        pass

    @abc.abstractmethod
    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;">
<br/>
<input type=button onClick="panner_widget_action('%(my_id)s', 'L');" value="left" title="left">
<input type=button onClick="panner_widget_action('%(my_id)s', 'R');" value="right" title="right">
<input type=button onClick="panner_widget_action('%(my_id)s', 'U');" value="up" title="up">
<input type=button onClick="panner_widget_action('%(my_id)s', 'D');" value="down" title="down">
<br/>
<input type=button onClick="panner_widget_action('%(my_id)s', 'Z2');" value="Z2" title="Z2">
<input type=button onClick="panner_widget_action('%(my_id)s', 'Z10');" value="Z10" title="Z10">
<input type=button onClick="panner_widget_action('%(my_id)s', 'ZO2');" value="ZO2" title="ZO2">
<input type=button onClick="panner_widget_action('%(my_id)s', 'ZO10');" value="ZO10" title="ZO10">
<br/>
<input type=button onClick="panner_widget_action('%(my_id)s', 'REFRESH');" value="REFRESH" title="REFRESH">
<br clear="all"/>
</form>
"""

class PannerWidget(YTExtWebWidget):

    _widget_name = "image_panner"
    _widget_source = _image_panner_source
    # Returns: image_data
    def __init__(self, my_id, panner):
        YTExtWebWidget.__init__(self, my_id)
        self.panner = panner

    def do_action(self, parameters):
        action = parameters['widget_action']
        if action == "L":
            self.panner.pan_rel_x(+0.1)
        elif action == "R":
            self.panner.pan_rel_x(-0.1)
        elif action == "D":
            self.panner.pan_rel_y(+0.1)
        elif action == "U":
            self.panner.pan_rel_y(-0.1)
        elif action == "Z2":
            self.panner.zoom(2.0)
        elif action == "ZO2":
            self.panner.zoom(0.5)
        elif action == "Z10":
            self.panner.zoom(10.0)
        elif action == "ZO10":
            self.panner.zoom(0.1)
        elif action == "REFRESH":
            self.panner.zoom(1.0)
        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)
        mi = na.nanmin(image[~na.isinf(image)])
        ma = na.nanmax(image[~na.isinf(image)])
        color_bounds = mi, ma
        image = (image - color_bounds[0])/(color_bounds[1] - color_bounds[0])
        to_plot = map_to_colors(image, "algae")
        to_plot = na.clip(to_plot, 0, 255)
        tf = tempfile.TemporaryFile()
        write_png_to_file(to_plot, tf)
        tf.seek(0)
        s = tf.read()
        tf.close()
        return s

    @classmethod
    def create_object_creator(cls, widgets, local_vars):
        def add_panner(panner):
            unique_id = str(uuid.uuid1())
            widget = cls(unique_id, panner)
            widgets[unique_id] = widget
            s = cls._widget_source % dict(my_id = unique_id)
            local_vars['new_values'] = s
            #print "Setting new values:\n\n" + s
        return add_panner

class TestingExtJS(YTExtWebWidget):
    _widget_name = "tester"
    _widget_source = _image_panner_source
    # Returns: image_data
    def __init__(self, my_id):
        YTExtWebWidget.__init__(self, my_id)

    def do_action(self, parameters):
        pass

    @classmethod
    def create_object_creator(cls, widgets, local_vars):
        def add_tester():
            unique_id = str(uuid.uuid1())
            widget = cls(unique_id)
            widgets[unique_id] = widget
            s = cls._widget_source % dict(my_id = unique_id)
            local_vars['execute'] = "Ext.Msg.alert('Hey!', 'Hi there.');"
            #print "Setting new values:\n\n" + s
        return add_tester