我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用traitlets.observe()。
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]
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)
def __init__(self, initial_value='', default=''): if initial_value == '': try: initial_value = iris.sample_data_path('') except ValueError: initial_value = '' # Define the file system path for input files. self._path = ipywidgets.Text( description='Path:', value=initial_value, width="100%") # Observe the path. self._path.observe(self._handle_path, names='value') # Use default path value to initialise file options. options = [] if os.path.exists(self._path.value): options = glob.glob('{}/*'.format(self._path.value)) options.sort() default_list = [] for default_value in default.split(','): if default_value in options: default_list.append(default_value) default_tuple = tuple(default_list) # Defines the files selected to be loaded. self._files = ipywidgets.SelectMultiple( description='Files:', options=OrderedDict([(os.path.basename(f), f) for f in options]), value=default_tuple, width="100%" ) self.deleter = ipywidgets.Button(description='delete tab', height='32px', width='75px') hbox = ipywidgets.HBox(children=[self._files, self.deleter]) self._box = ipywidgets.Box(children=[self._path, hbox], width="100%")
def __init__(self): self.mpl_kwargs = {} # Defines the cube which is to be plotted. self.cube_picker = ipywidgets.Dropdown(description='Cubes:', options=('None', None), value=None, width='50%') # Define the type of cube browser plot required self.plot_type = ipywidgets.Dropdown( description='Plot type:', options={'pcolormesh': cube_browser.Pcolormesh, 'contour': cube_browser.Contour, 'contourf': cube_browser.Contourf}, value=cube_browser.Pcolormesh) self.x_coord = ipywidgets.Dropdown( description='X Coord', options=('None', None)) self.y_coord = ipywidgets.Dropdown( description='Y Coord', options=('None', None)) self.cmap = ipywidgets.Text( description='colour map') # Handle events: self.cube_picker.observe(self._handle_cube_selection, names='value') self.cmap.observe(self._handle_cmap, names='value') self.plot_type.observe(self._handle_plot_type, names='value') self._box = ipywidgets.Box(children=[self.cube_picker, self.plot_type, self.x_coord, self.y_coord, self.cmap])
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)
def __init__(self, url=''): self.file_pickers = [] if url: o = urlparse(url) query = parse_qs(o.query) pwd, = query.get('pwd', ['']) for fname in query.get('files', []): self.file_pickers.append(FilePicker(pwd, os.path.join(pwd, fname))) for fpath in query.get('folders', []): self.file_pickers.append(FilePicker(fpath)) if not self.file_pickers: self.file_pickers.append(FilePicker()) # Define load action. self._load_button = ipywidgets.Button(description="load these files") self._load_button.on_click(self._handle_load) self._file_tab_button = ipywidgets.Button(description="add tab") self._file_tab_button.on_click(self._handle_new_tab) self._subplots = ipywidgets.RadioButtons(description='subplots', options=[1, 2]) self._subplots.observe(self._handle_nplots, names='value') # Plot action button. self._plot_button = ipywidgets.Button(description="Plot my cube") self._plot_button.on_click(self._goplot) # Configure layout of the Explorer. self._plot_container = ipywidgets.Box() # Define a Tab container for the main controls in the browse interface. children = [fp.box for fp in self.file_pickers] self.ftabs = ipywidgets.Tab(children=children) children = [self._load_button, self._file_tab_button] self.bbox = ipywidgets.HBox(children=children) children = [self.ftabs, self.bbox] self._file_picker_tabs = ipywidgets.Box(children=children) # Define the plot controls, start with 1 (self._subplots default) self.plot_controls = [PlotControl()] pcc_children = [pc.box for pc in self.plot_controls] self._plot_control_container = ipywidgets.Tab(children=pcc_children) self._plot_control_container.set_title(0, 'Plot Axes 0') # Define an Accordian for files, subplots and plots acc_children = [self._file_picker_tabs, self._subplots, self._plot_control_container] self._accord = ipywidgets.Accordion(children=acc_children) self._accord.set_title(0, 'Files') self._accord.set_title(1, 'SubPlots') self._accord.set_title(2, 'Plots') # Initialise cubes container self._cubes = [] # Display the browse interface. IPython.display.display(self._accord) IPython.display.display(self._plot_button) IPython.display.display(self._plot_container)
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