Python traitlets.observe() Examples

The following are 11 code examples of traitlets.observe(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module traitlets , or try the search function .
Example #1
Source File: led.py    From uchroma with GNU Lesser General Public License v3.0 6 votes vote down vote up
def get(self, led_type: LEDType) -> LED:
        """
        Fetches the requested LED interface on this device

        :param led_type: The LED type to fetch

        :return: The LED interface, if available
        """
        if led_type not in self._driver.supported_leds:
            return None

        if led_type not in self._leds:
            self._leds[led_type] = LED(self._driver, led_type)
            self._leds[led_type].observe(self._led_changed)

        return self._leds[led_type] 
Example #2
Source File: anim.py    From uchroma with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, renderer: Renderer, frame: Frame,
                 blend_mode=None, *args, **kwargs):
        super(LayerHolder, self).__init__(*args, **kwargs)

        self._renderer = renderer
        self._frame = frame
        self._blend_mode = blend_mode

        self.waiter = None
        self.active_buf = None
        self.task = None

        self.traits_changed = Signal()
        self._renderer.observe(self._traits_changed, names=['all'])

        self._renderer._flush()

        for buf in range(0, NUM_BUFFERS):
            layer = self._frame.create_layer()
            layer.blend_mode = self._blend_mode
            self._renderer._free_layer(layer) 
Example #3
Source File: templateexporter.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, config=None, **kw):
        """
        Public constructor

        Parameters
        ----------
        config : config
            User configuration instance.
        extra_loaders : list[of Jinja Loaders]
            ordered list of Jinja loader to find templates. Will be tried in order
            before the default FileSystem ones.
        template : str (optional, kw arg)
            Template to use when exporting.
        """
        super(TemplateExporter, self).__init__(config=config, **kw)

        self.observe(self._invalidate_environment_cache,
                     list(self.traits(affects_environment=True)))
        self.observe(self._invalidate_template_cache,
                     list(self.traits(affects_template=True))) 
Example #4
Source File: test_traittypes.py    From traittypes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_array_equal(self):
        notifications = []
        class Foo(HasTraits):
            bar = Array([1, 2])
            @observe('bar')
            def _(self, change):
                notifications.append(change)
        foo = Foo()
        foo.bar = [1, 2]
        self.assertFalse(len(notifications))
        foo.bar = [1, 1]
        self.assertTrue(len(notifications)) 
Example #5
Source File: test_traittypes.py    From traittypes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_df_equal(self):
        notifications = []
        class Foo(HasTraits):
            bar = DataFrame([1, 2])
            @observe('bar')
            def _(self, change):
                notifications.append(change)
        foo = Foo()
        foo.bar = [1, 2]
        self.assertEqual(notifications, [])
        foo.bar = [1, 1]
        self.assertEqual(len(notifications), 1) 
Example #6
Source File: test_traittypes.py    From traittypes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_series_equal(self):
        notifications = []
        class Foo(HasTraits):
            bar = Series([1, 2])
            @observe('bar')
            def _(self, change):
                notifications.append(change)
        foo = Foo()
        foo.bar = [1, 2]
        self.assertEqual(notifications, [])
        foo.bar = [1, 1]
        self.assertEqual(len(notifications), 1) 
Example #7
Source File: test_traittypes.py    From traittypes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_ds_equal(self):
        notifications = []
        class Foo(HasTraits):
            bar = Dataset({'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.14})
            @observe('bar')
            def _(self, change):
                notifications.append(change)
        foo = Foo()
        foo.bar = {'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.14}
        self.assertEqual(notifications, [])
        foo.bar = {'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.15}
        self.assertEqual(len(notifications), 1) 
Example #8
Source File: test_traittypes.py    From traittypes with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_ds_equal(self):
        notifications = []
        class Foo(HasTraits):
            bar = DataArray([[0, 1], [2, 3]])
            @observe('bar')
            def _(self, change):
                notifications.append(change)
        foo = Foo()
        foo.bar = [[0, 1], [2, 3]]
        self.assertEqual(notifications, [])
        foo.bar = [[0, 1], [2, 4]]
        self.assertEqual(len(notifications), 1) 
Example #9
Source File: anim.py    From uchroma with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _create_loop(self):
        if self._loop is None:
            self._loop = AnimationLoop(self._driver.frame_control)
            self._loop.observe(self._loop_running_changed, names=['running'])
            self._loop.layers_changed.connect(self._loop_layers_changed) 
Example #10
Source File: widgets.py    From SlicerJupyter with MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        self.path = None
        self.filename = None
        self.observe(self._handle_upload, names='data')
        super().__init__(**kwargs) 
Example #11
Source File: orbital_viewer.py    From notebook-molecular-visualization with Apache License 2.0 4 votes vote down vote up
def _make_ui_pane(self, hostheight):
        layout = ipy.Layout(width='325px',
                            height=str(int(hostheight.rstrip('px')) - 50) + 'px')
        #element_height = str(int(hostheight.rstrip('px')) - 125) + 'px'
        element_height = None
        # NOTE - element_height was used for the listbox-style orblist.
        #   HOWEVER ipywidgets 6.0 only displays those as a dropdown.
        #   This is therefore disabled until we can display listboxes again. -- AMV 7/16

        # Orbital set selector
        self.status_element = ipy.HTML(layout=ipy.Layout(width='inherit', height='20px'))
        orbtype_label = ipy.Label("Orbital set:")
        self.type_dropdown = ipy.Dropdown(options=list(self.wfn.orbitals.keys()))
        initialtype = 'canonical'
        if initialtype not in self.type_dropdown.options:
            initialtype = next(iter(self.type_dropdown.options.keys()))
        self.type_dropdown.value = initialtype
        self.type_dropdown.observe(self.new_orb_type, 'value')

        # List of orbitals in this set
        orblist_label = ipy.Label("Orbital:")
        self.orblist = ipy.Dropdown(options={None: None},
                                    layout=ipy.Layout(width=layout.width, height=element_height))
        traitlets.link((self.orblist, 'value'), (self, 'current_orbital'))

        # Isovalue selector
        isoval_label = ipy.Label('Isovalue:')
        self.isoval_selector = ipy.FloatSlider(min=0.0, max=0.075,
                                               value=0.01, step=0.00075,
                                               readout_format='.4f',
                                               layout=ipy.Layout(width=layout.width))
        traitlets.link((self.isoval_selector, 'value'), (self, 'isoval'))

        # Opacity selector
        opacity_label = ipy.Label('Opacity:')
        self.opacity_selector = ipy.FloatSlider(min=0.0, max=1.0,
                                               value=0.8, step=0.01,
                                               readout_format='.2f',
                                               layout=ipy.Layout(width=layout.width))
        traitlets.link((self.opacity_selector, 'value'), (self, 'orb_opacity'))

        # Resolution selector
        resolution_label = ipy.Label("Grid resolution:", layout=ipy.Layout(width=layout.width))
        self.orb_resolution = ipy.Text(layout=ipy.Layout(width='75px',
                                                         positioning='bottom'))
        self.orb_resolution.value = str(self.numpoints)
        self.resolution_button = ipy.Button(description='Update resolution')
        self.resolution_button.on_click(self.change_resolution)
        traitlets.directional_link((self, 'numpoints'), (self.orb_resolution, 'value'),
                                   transform=str)

        self.uipane = ipy.VBox([self.status_element,
                                orbtype_label, self.type_dropdown,
                                orblist_label, self.orblist,
                                isoval_label, self.isoval_selector,
                                opacity_label, self.opacity_selector,
                                resolution_label, self.orb_resolution, self.resolution_button])
        self.new_orb_type()
        self.type_dropdown.observe(self.new_orb_type, 'value')
        return self.uipane