我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用ipywidgets.Dropdown()。
def __init__(self, mol): super().__init__(mol) self.selection_type = ipy.Dropdown(description='Clicks select:', value=self.viewer.selection_type, options=('Atom', 'Residue', 'Chain')) traitlets.link((self.selection_type, 'value'), (self.viewer, 'selection_type')) self.residue_listname = ipy.Label('Selected residues:', layout=ipy.Layout(width='100%')) self.residue_list = ipy.SelectMultiple(options=list(), height='150px') self.viewer.observe(self._update_reslist, 'selected_atom_indices') self.residue_list.observe(self.remove_atomlist_highlight, 'value') self.atom_list.observe(self.remove_reslist_highlight, 'value') self.subtools.children = [self.representation_buttons] self.subtools.layout.flex_flow = 'column' self.toolpane.children = [self.selection_type, HBox([self.select_all_atoms_button, self.select_none]), self.atom_listname, self.atom_list, self.residue_listname, self.residue_list]
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_shelf(self, i=0): """ Creates shelf to plot a dimension (includes buttons for data column, encoding, data type, aggregate) """ encodings = _get_encodings() cols = widgets.Dropdown(options=self.columns, description='encode') encoding = widgets.Dropdown(options=encodings, description='as', value=encodings[i]) encoding.layout.width = '20%' adv = widgets.VBox(children=[], visible=False) button = widgets.Button(description='options') button.on_click(self._show_advanced) button.layout.width = '10%' # The callbacks when the button is clicked encoding.observe(self._update, names='value') cols.observe(self._update, names='value') # Making sure we know what row we're in in the callbacks encoding.row = cols.row = button.row = adv.row = i # Have the titles so we know what button we're editing encoding.title = 'encoding' cols.title = 'field' button.title = 'button' adv.title = 'advanced' return widgets.HBox([cols, encoding, button, adv])
def _generate_controller(self, ndims): marks = _get_marks() # mark button mark_choose = widgets.Dropdown(options=marks, description='Marks') mark_choose.observe(self._update, names='value') mark_choose.layout.width = '20%' mark_choose.row = -1 mark_choose.title = 'mark' # mark options button mark_but = widgets.Button(description='options') mark_but.layout.width = '10%' mark_but.row = -1 mark_but.title = 'mark_button' # Mark options mark_opt = widgets.VBox(children=[], visible=False) mark_but.on_click(self._show_advanced) mark_opt.title = 'mark_options' mark_opt.layout.width = '300px' add_dim = widgets.Button(description='add encoding') add_dim.on_click(self._add_dim) to_altair = widgets.Button(description='chart.to_altair()') to_altair.on_click(self._to_altair) dims = [self._create_shelf(i=i) for i in range(ndims)] choices = dims + [widgets.HBox([add_dim, to_altair, mark_choose, mark_but, mark_opt])] return widgets.VBox(choices)
def _controllers_for(opt): """ Give a string representing the parameter represented, find the appropriate command. """ colors = [None, 'blue', 'red', 'green', 'black'] controllers = {'type': widgets.Dropdown(options=['auto detect'] +\ _get_types(), description='type'), 'bin': widgets.Checkbox(description='bin'), 'aggregate': widgets.Dropdown(options=[None] +\ _get_functions(), description='aggregate'), 'zero': widgets.Checkbox(description='zero'), 'text': widgets.Text(description='text value'), 'scale': widgets.Dropdown(options=['linear', 'log'], description='scale'), 'color': widgets.Dropdown(options=colors, description='main color'), 'applyColorToBackground': widgets.Checkbox(description='applyColorToBackground'), 'shortTimeLabels': widgets.Checkbox(description='shortTimeLabels') } for title, controller in controllers.items(): controller.title = title if 'Checkbox' in str(controller): # traits = dir(controller.layout) # traits = [t for t in traits if t[0] != '_'] controller.layout.max_width = '200ex' # controller.layout.min_width = '100ex' # controller.layout.width = '150ex' return controllers[opt]
def create_choices_widget(self): from ipywidgets import Dropdown dropdown = Dropdown( options=list(self.method_names), value=self._method, description="Method", ) def on_method_change(change): self._method = dropdown.value self.create_param_widgets() self.replot_peaks() dropdown.observe(on_method_change, names='value') display(dropdown)
def __init__(self, mode="text"): self.mode_widget = Dropdown( options={ 'BibTeX': 'bibtex', 'Text': 'text', '[N] author name place other year': 'citation', 'Quoted': 'quoted', }, value=mode ) self.button_widget = Button( description="Set article_list variable", disabled=mode not in ("citation", "bibtex") ) self.frompdf_widget = Textarea() self.output_widget = Textarea() self.label_widget = Label() self.frompdf_widget.observe(self.write) self.mode_widget.observe(self.select) self.button_widget.on_click(self.set_variable) self.view = VBox([ HBox([self.mode_widget, self.button_widget, self.label_widget]), HBox([self.frompdf_widget, self.output_widget]) ]) self.frompdf_widget.layout.height = "500px" self.output_widget.layout.height = "500px" self.frompdf_widget.layout.width = "50%" self.output_widget.layout.width = "50%" self.backup = "" self.ipython = get_ipython()
def __init__(self, case, *args, **kwargs): self.case = case super(CaseView, self).__init__(**kwargs) self.generator_names = ipyw.Dropdown( options=list(self.case.gen.index) ) children = [ self.generator_names, ] self.children = children
def surface_properties_time_series(self): '''Widgets for canopy properties''' self.w_PT = widgets.BoundedFloatText( value=self.max_PT, min=0, description="Max. alphaPT", width=80) self.w_LAD = widgets.BoundedFloatText( value=self.x_LAD, min=0, description="LIDF param.", width=80) self.w_LAD.visible = False self.w_leafwidth = widgets.BoundedFloatText( value=self.leaf_width, min=0.001, description="Leaf width", width=80) self.w_zsoil = widgets.BoundedFloatText( value=self.z0soil, min=0, description="soil roughness", width=80) # Landcover classes and values come from IGBP Land Cover Type Classification self.w_lc = widgets.Dropdown( options={ 'WATER': 0, 'CONIFER EVERGREEN': 1, 'BROADLEAVED EVERGREEN': 2, 'CONIFER DECIDUOUS': 3, 'BROADLEAVED DECIDUOUS': 4, 'FOREST MIXED': 5, 'SHRUB CLOSED': 6, 'SHRUB OPEN': 7, 'SAVANNA WOODY': 8, 'SAVANNA': 9, 'GRASS': 10, 'WETLAND': 11, 'CROP': 12, 'URBAN': 13, 'CROP MOSAIC': 14, 'SNOW': 15, 'BARREN': 16 }, value=self.landcover, description="Land Cover Type", width=200) lcText = widgets.HTML(value='''Land cover information is used to estimate roughness. <BR> For shrubs, conifers and broadleaves we use the model of <BR> Schaudt & Dickinson (2000) Agricultural and Forest Meteorology. <BR> For crops and grasses we use a fixed ratio of canopy heigh''', width=100) self.calc_row_options() self.veg_page = widgets.VBox([widgets.HBox([self.w_PT, self.w_LAD, self.w_leafwidth]), widgets.HBox([self.w_zsoil, self.w_lc, lcText]), widgets.HBox([self.w_row, self.w_rowaz])], background_color='#EEE')
def surface_properties_image(self): '''Widgets for canopy properties''' self.w_PT_But = widgets.Button( description='Browse Initial alphaPT Image') self.w_PT = widgets.Text(description=' ', value=str(self.max_PT), width=500) self.w_LAD_But = widgets.Button( description='Browse Leaf Angle Distribution Image') self.w_LAD = widgets.Text(description='(degrees)', value=str(self.x_LAD), width=500) self.w_leafwidth_But = widgets.Button( description='Browse Leaf Width Image') self.w_leafwidth = widgets.Text(description='(m)', value=str(self.leaf_width), width=500) self.w_zsoil_But = widgets.Button( description='Browse Soil Roughness Image') self.w_zsoil = widgets.Text(description='(m)', value=str(self.z0soil), width=500) self.w_lc_But = widgets.Button( description='Browse Land Cover Image') # Landcover classes and values come from IGBP Land Cover Type Classification self.w_lc = widgets.Dropdown( options={ 'CROP': 12, 'GRASS': 10, 'SHRUB': 6, 'CONIFER': 1, 'BROADLEAVED': 4}, value=self.landcover, description=" ", width=300) lcText = widgets.HTML(value='''Land cover information is used to estimate roughness. <BR> For shrubs, conifers and broadleaves we use the model of <BR> Schaudt & Dickinson (2000) Agricultural and Forest Meteorology. <BR> For crops and grasses we use a fixed ratio of canopy height<BR>''', width=600) self.calc_row_options() self.veg_page = widgets.VBox([widgets.HTML('Select alphaPT image or type a constant value'), widgets.HBox([self.w_PT_But, self.w_PT]), widgets.HTML('Select leaf angle distribution image or type a constant value'), widgets.HBox([self.w_LAD_But, self.w_LAD]), widgets.HTML('Select leaf width image or type a constant value'), widgets.HBox([self.w_leafwidth_But, self.w_leafwidth]), widgets.HTML('Select soil roughness image or type a constant value'), widgets.HBox([self.w_zsoil_But, self.w_zsoil]), widgets.HTML('Select landcover image or type a constant value'), widgets.HBox([self.w_lc_But, self.w_lc]), lcText, widgets.HBox([self.w_row, self.w_rowaz])], background_color='#EEE') self.w_PT_But.on_click( lambda b: self._on_input_clicked(b, 'Initial alphaPT', self.w_PT)) self.w_LAD_But.on_click( lambda b: self._on_input_clicked(b, 'Leaf Angle Distribution', self.w_LAD)) self.w_leafwidth_But.on_click( lambda b: self._on_input_clicked(b, 'Leaf Width', self.w_leafwidth)) self.w_zsoil_But.on_click( lambda b: self._on_input_clicked(b, 'Soil Roughness', self.w_zsoil)) self.w_lc_But.on_click( lambda b: self._input_dropdown_clicked(b, 'Land Cover', self.w_lc))
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
def __init__(self, paramdef): super(ParamSelector, self).__init__(layout=ipy.Layout(display='flex', flex_flow='nowrap', align_content='stretch')) self.paramdef = paramdef children = [] self.name = ipy.HTML("<p style='text-align:right'>%s:</p>" % paramdef.displayname, layout=ipy.Layout(width='200px')) children.append(self.name) if paramdef.choices: self.selector = ipy.Dropdown(options=paramdef.choices, **self.WIDGETKWARGS) elif paramdef.type == bool: self.selector = ipy.ToggleButtons(options=[True, False], **self.WIDGETKWARGS) elif paramdef.units: self.selector = UnitText(units=paramdef.units, **self.WIDGETKWARGS) elif paramdef.type == float: self.selector = ipy.FloatText(**self.WIDGETKWARGS) elif paramdef.type == int: self.selector = ipy.IntText(**self.WIDGETKWARGS) elif paramdef.type == str: self.selector = ipy.Text(**self.WIDGETKWARGS) else: self.selector = ReadOnlyRepr(**self.WIDGETKWARGS) children.append(self.selector) children = [self.name, self.selector] self.default_button = None if paramdef.default: self.default_button = ipy.Button(description='Default', tooltip='Set to default: %s' % self.paramdef.default, layout=ipy.Layout(width='75px')) self.default_button.on_click(self.default) children.append(self.default_button) self.default() self.help_link = None if paramdef.help_url: self.help_link = ipy.HTML('<a href="%s" target="_blank">?</a>' % paramdef.help_url) children.append(self.help_link) self.children = children
def assemble_widget_dicts(field,values,widget_type,dictlist): # if an empty dictionary is input for dictlist overwrites an empty list if dictlist == {}: dictlist = [] # instantiating widget for integer slider if widget_type == 'IntSlider': minslider = widgets.IntSlider(description='Min ' + str(field),min=values[0],max=values[1],continuous_update=False) maxslider = widgets.IntSlider(description='Max ' + str(field),min=values[0],max=values[1],value=values[1],continuous_update=False) dictentry = {'type':'IntSlider','field':str(field),'widget':[minslider,maxslider]} dictlist.append(dictentry) # instantiating widget for float slider elif widget_type == 'FloatSlider': # getting significant figures of delta between min and maxx magnitude = determine_delta_magnitude([values[0],values[1]]) # getting stepsize determined by the magnitude of difference # between min and max stepsize = 10 ** -(magnitude + 2) if stepsize < 10**-6: stepsize = 10 ** -6 minvalue = round(values[0]-(.5*stepsize),magnitude+1) maxvalue = round(values[1]+(.5*stepsize),magnitude+1) # setting min and max slider minslider = widgets.FloatSlider(description='Min ' + str(field),min=minvalue,max=maxvalue,step=stepsize,value=minvalue,continuous_update=False) maxslider = widgets.FloatSlider(description='Max ' + str(field),min=minvalue,max=maxvalue,step=stepsize,value=maxvalue,continuous_update=False) # adding dictentry which will be updated to the widget dictlist dictentry = {'type':'FloatSlider','field':str(field),'widget':[minslider,maxslider]} dictlist.append(dictentry) elif widget_type == 'Dropdown': # given a list of unique categorical values returns widget with dropdown # for each value given print values dropdownwidget = widgets.Dropdown(description=str(field), options=values) dropdownwidget.padding = 4 dictentry = {'type':'Dropdown','field':str(field),'widget':dropdownwidget} dictlist.append(dictentry) return dictlist #assemble_widget_dicts('GEOHASH',['dnvfp6g'],'Dropdown',{}) # filters rows between a range and a field # the range can contain either a float or an int