Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from IPython.html.widgets import FloatTextWidget, ButtonWidget, ContainerWidget, HTMLWidget
- from IPython.html.widgets.widget import CallbackDispatcher
- from IPython.utils.traitlets import Unicode, Instance
- class VideoParameterWidget(ContainerWidget):
- description = Unicode(sync=True)
- value = Instance(object, sync=True)
- def __init__(
- self,
- description=u'',
- params=None,
- **kwargs
- ):
- super(VideoParameterWidget, self).__init__(description=description, **kwargs)
- self.description = description
- self.value = {
- p['name']: p['value']
- for p in params
- }
- self.children = [
- FloatTextWidget(description=p['description'] + ':', value=p['value'])
- for p in params
- ]
- self.submissionCallbacks = CallbackDispatcher()
- self.cancelCallbacks = CallbackDispatcher()
- # instructions widget
- self.insw = HTMLWidget(value=self.toHTML(''))
- # Done button
- self.donew = ButtonWidget(description='Done')
- self.donew.on_click(self.submit)
- # Cancel button
- self.cancelw = ButtonWidget(description='Cancel')
- self.cancelw.on_click(self.cancel)
- self.cancelw.visible = False
- self.binsw = ContainerWidget() # top row
- self.binsw.on_displayed(self.sideBySide)
- self.binsw.children = [self.insw, self.donew, self.cancelw]
- self.children = (self.binsw,) + self.children
- self.setInstructions("Set Parameter Values:")
- self.on_displayed(self.onDisplay)
- def onDisplay(self, name=None):
- self.sideBySide(name)
- def sideBySide(self, name=None):
- """Arrange children horizontally."""
- self.binsw.remove_class('vbox')
- self.binsw.add_class('hbox')
- # self.binsw.set_css('width', '100%')
- self.binsw.add_class('center')
- def toHTML(self, text):
- """Convert text to HTML."""
- html = '<table><tr height=40><th width=15 />\n'
- html += '<th style=\"vertical-align: center;\"><b>' + text + '</b></th>'
- html += '<th width=15 /></tr></table>'
- return html
- def setInstructions(self, text):
- self.insw.value = self.toHTML(text)
- def submit(self, name=None):
- """
- Calculate and submit results when Done button clicked.
- This is more-or-less an abstract method. You should override
- it, but call the superclass method AFTER you have set
- self.value correctly.
- """
- self.send('value')
- self.submissionCallbacks(self)
- def cancel(self, name=None):
- """Fire cancel callbacks."""
- self.cancelCallbacks(self)
- def on_submit(self, callback, remove=False):
- """
- (Un)Register a callback to handle result submission.
- Triggered when the user clicks the Done button.
- Parameters
- ----------
- callback: callable
- Will be called with exactly one argument: the Widget instance
- remove: bool (optional)
- Whether to unregister the callback
- """
- self.submissionCallbacks.register_callback(callback,
- remove=remove)
- def on_cancel(self, callback, remove=False):
- """
- (Un)Register a callback to cancel calibration.
- Triggered when the user clicks the Cancel button. (The Cancel
- button will not appear until a callback is registered.)
- Parameters
- ----------
- callback: callable
- Will be called with exactly one argument: the Widget instance
- remove: bool (optional)
- Whether to unregister the callback
- """
- self.cancelw.visible = True
- self.cancelCallbacks.register_callback(callback,
- remove=remove)
- params = [{'description': 'Expected Worm Width',
- 'name': 'expectedWormWidth',
- 'value': 70},
- {'description': 'Expected Worm Length',
- 'name': 'expectedWormLength',
- 'value': 1000},
- {'description': 'Worm Area Threshold Lower Bound',
- 'name': 'wormAreaThresholdLB',
- 'value': 0.5},
- {'description': 'Worm Area Threshold Upper Bound',
- 'name': 'wormAreaThresholdUB',
- 'value': 1.5},
- {'description': 'Number of Posture Points',
- 'name': 'numberOfPosturePoints',
- 'value': 30},
- {'description': 'Background Disk Radius',
- 'name': 'backgroundDiskRadius',
- 'value': 5},
- {'description': 'Threshold', 'name': 'threshold', 'value': 0.9},
- {'description': 'Worm Disk Radius', 'name': 'wormDiskRadius', 'value': 2}]
- vpw1 = VideoParameterWidget(
- description='Analysis Parameters',
- params=params
- )
- def reportCancel(self=None):
- print("Cancelled.")
- vpw1.on_cancel(reportCancel)
- vpw1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement