Advertisement
lavery3

Widget Label Sizing

Nov 25th, 2014
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.83 KB | None | 0 0
  1. from IPython.html.widgets import FloatTextWidget, ButtonWidget, ContainerWidget, HTMLWidget
  2. from IPython.html.widgets.widget import CallbackDispatcher
  3. from IPython.utils.traitlets import Unicode, Instance
  4.  
  5. class VideoParameterWidget(ContainerWidget):
  6.     description = Unicode(sync=True)
  7.     value = Instance(object, sync=True)
  8.    
  9.     def __init__(
  10.         self,
  11.         description=u'',
  12.         params=None,
  13.         **kwargs
  14.     ):
  15.         super(VideoParameterWidget, self).__init__(description=description, **kwargs)
  16.         self.description = description
  17.         self.value = {
  18.             p['name']: p['value']
  19.             for p in params
  20.         }
  21.         self.children = [
  22.             FloatTextWidget(description=p['description'] + ':', value=p['value'])
  23.             for p in params
  24.         ]
  25.         self.submissionCallbacks = CallbackDispatcher()
  26.         self.cancelCallbacks = CallbackDispatcher()
  27.         # instructions widget
  28.         self.insw = HTMLWidget(value=self.toHTML(''))
  29.         # Done button
  30.         self.donew = ButtonWidget(description='Done')
  31.         self.donew.on_click(self.submit)
  32.         # Cancel button
  33.         self.cancelw = ButtonWidget(description='Cancel')
  34.         self.cancelw.on_click(self.cancel)
  35.         self.cancelw.visible = False
  36.         self.binsw = ContainerWidget()    # top row
  37.         self.binsw.on_displayed(self.sideBySide)
  38.         self.binsw.children = [self.insw, self.donew, self.cancelw]
  39.         self.children = (self.binsw,) + self.children
  40.         self.setInstructions("Set Parameter Values:")
  41.         self.on_displayed(self.onDisplay)
  42.  
  43.     def onDisplay(self, name=None):
  44.         self.sideBySide(name)
  45.  
  46.     def sideBySide(self, name=None):
  47.         """Arrange children horizontally."""
  48.         self.binsw.remove_class('vbox')
  49.         self.binsw.add_class('hbox')
  50.         # self.binsw.set_css('width', '100%')
  51.         self.binsw.add_class('center')
  52.        
  53.     def toHTML(self, text):
  54.         """Convert text to HTML."""
  55.         html = '<table><tr height=40><th width=15 />\n'
  56.         html += '<th style=\"vertical-align: center;\"><b>' + text + '</b></th>'
  57.         html += '<th width=15 /></tr></table>'
  58.         return html
  59.  
  60.     def setInstructions(self, text):
  61.         self.insw.value = self.toHTML(text)
  62.  
  63.     def submit(self, name=None):
  64.         """
  65.        Calculate and submit results when Done button clicked.
  66.  
  67.        This is more-or-less an abstract method. You should override
  68.        it, but call the superclass method AFTER you have set
  69.        self.value correctly.
  70.        """
  71.         self.send('value')
  72.         self.submissionCallbacks(self)
  73.  
  74.     def cancel(self, name=None):
  75.         """Fire cancel callbacks."""
  76.         self.cancelCallbacks(self)
  77.  
  78.     def on_submit(self, callback, remove=False):
  79.         """
  80.        (Un)Register a callback to handle result submission.
  81.  
  82.        Triggered when the user clicks the Done button.
  83.  
  84.        Parameters
  85.        ----------
  86.        callback: callable
  87.            Will be called with exactly one argument: the Widget instance
  88.        remove: bool (optional)
  89.            Whether to unregister the callback
  90.        """
  91.         self.submissionCallbacks.register_callback(callback,
  92.                                                    remove=remove)        
  93.        
  94.     def on_cancel(self, callback, remove=False):
  95.         """
  96.        (Un)Register a callback to cancel calibration.
  97.  
  98.        Triggered when the user clicks the Cancel button. (The Cancel
  99.        button will not appear until a callback is registered.)
  100.  
  101.        Parameters
  102.        ----------
  103.        callback: callable
  104.            Will be called with exactly one argument: the Widget instance
  105.        remove: bool (optional)
  106.            Whether to unregister the callback
  107.        """
  108.         self.cancelw.visible = True
  109.         self.cancelCallbacks.register_callback(callback,
  110.                                                remove=remove)
  111.  
  112. params = [{'description': 'Expected Worm Width',
  113.   'name': 'expectedWormWidth',
  114.   'value': 70},
  115.  {'description': 'Expected Worm Length',
  116.   'name': 'expectedWormLength',
  117.   'value': 1000},
  118.  {'description': 'Worm Area Threshold Lower Bound',
  119.   'name': 'wormAreaThresholdLB',
  120.   'value': 0.5},
  121.  {'description': 'Worm Area Threshold Upper Bound',
  122.   'name': 'wormAreaThresholdUB',
  123.   'value': 1.5},
  124.  {'description': 'Number of Posture Points',
  125.   'name': 'numberOfPosturePoints',
  126.   'value': 30},
  127.  {'description': 'Background Disk Radius',
  128.   'name': 'backgroundDiskRadius',
  129.   'value': 5},
  130.  {'description': 'Threshold', 'name': 'threshold', 'value': 0.9},
  131.  {'description': 'Worm Disk Radius', 'name': 'wormDiskRadius', 'value': 2}]
  132.  
  133.  
  134. vpw1 = VideoParameterWidget(
  135.     description='Analysis Parameters',
  136.     params=params
  137. )
  138. def reportCancel(self=None):
  139.     print("Cancelled.")
  140. vpw1.on_cancel(reportCancel)
  141. vpw1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement