def _fill_folder(self): atoms = Button(description=' Fill', icon='adjust', layout=_wlo) opt = ['Ball and Stick', 'Van Der Waals Spheres', 'Covalent Spheres'] #'High Performance']#,'Stick'] fill = Select(options=opt, value=opt[0], layout=_wlo) bond_r = FloatSlider(max=1.0, description='Bond Radius') def _atoms(c): for scn in self.active(): scn.atom_3d = not scn.atom_3d def _fill(c): for scn in self.active(): scn.fill_idx = c.new bond_r.disabled = True if c.new == 1 else False def _bond_r(c): for scn in self.active(): scn.bond_r = c.new atoms.on_click(_atoms) fill.observe(_fill, names='index') bond_r.observe(_bond_r, names='value') content = _ListDict([('opt', fill), ('bond_r', bond_r)]) return Folder(atoms, content)
def make_index(self): try: from ._catalogs import catalogs except: print("To build the index page, we need some catalogs.") catalogs = [] self.selected_object = None self.title.value = "Sage Explorer" self.visualbox.children = [Title("Index Page")] self.tabs.remove_class('invisible') self.tabs.add_class('visible') self.gobutton.description = 'Go!' menus = [] for label, catalog in catalogs: menu = Select(rows=12, options=make_catalog_menu_options(catalog)) menus.append(menu) self.menus.children = menus for i, (label, _) in enumerate(catalogs): self.menus.set_title(i, label) def menu_on_change(change): self.selected_object = change.new self.display_new_value(self.selected_object.name) self.doctab.value = to_html(change.new.doc) self.gobutton.on_click(lambda b:self.set_value(self.selected_object.member)) for menu in self.menus.children: menu.observe(menu_on_change, names='value')
class SymbolsWidget(HBox): """ A list of symbols, plus a filtering search box """ def __init__(self, nl, **kwargs): self.nl = nl self.list = Select(options=self.nl.symbols) self.search_box = Text(placeholder="search") self.help = HTML() super().__init__(**kwargs) self.children = [VBox([self.search_box, self.list]), self.help] self.help.layout = Layout(flex="1 1 65%") self.list.observe(self.on_select_change, names="value") self.on_select_change(None) self.search_box.observe(self.on_search_change, names="value") def on_select_change(self, change): help = self.nl.symbols[self.list.value].help() self.help.value = _format_help_message(self.list.value, help) def on_search_change(self, change): if self.search_box.value == "": self.list.options = self.nl.symbols else: filtered_options = [ item for item in self.nl.symbols if self.search_box.value in item ] self.list.options = filtered_options
def generate_pdp_feature_selection_grid(models: list) -> GridBox: children = [] # Row 1 children.append( Label(layout=Layout(width='auto', height='auto'), value='Feature 1 for ...')) children.append( Label(layout=Layout(width='auto', height='auto'), value='(optional) Feature 2 for ...')) for model in models: # Row 2 -> Row (2 + len(models)) # if you change the description of this widget, # you have to also adjust it in the notebook function call. features = model.features_ohe.copy() features.insert(0, 'None') children.append( Select(description="... " + model.name, options=model.features_ohe, disabled=False)) children.append( Select(description="... " + model.name, options=features, value='None', disabled=False)) return GridBox(children=children, layout=Layout(width='auto', grid_template_columns="50% 50%", grid_template_rows='auto', align_items='center', grid_gap='3px 3px'))
def create(self, path="/", height="400px"): if self.running: print("dbfs browser already running. Use close() first") return self.path = path self.flist = Select(options=[], disabled=False, layout={"height": height}) self.flist.observe(self.on_click, names="value") self.refresh = Button(icon="refresh", layout={"width": "40px"}) self.refresh.on_click(self.on_refresh) self.path_view = Output() self.preview = Output( layout={ "width": "800px", "height": height, "overflow": "scroll", "border": "1px solid gray", }) self.up = Button(icon="arrow-up", layout={"width": "40px"}) self.up.on_click(self.on_up) display( VBox([ HBox([self.refresh, self.up, self.path_view]), HBox([self.flist, self.preview]), ])) self.update() self.running = True
def widget_images(self): return interact( self.show_images, size=FloatSlider(value=0.5, min=0, max=1, step=0.01, continuous_update=False, description="Size fraction"), show=Select(options={"Image": "image", "Difference": "difference"}, value="image", description='Show'), algorithm=Select(options=['None (exact)'] + list(self.ALGORITHMS.keys()), value='JPEG', description='Algorithm') )
def set_up_read_selection_widgets(self): self.widgets.update({ 'batch': Select(options=self.batch_names, continuous_update=False, layout=Layout(height='200px', width='300px')), 'sample': Select(options=[], continuous_update=False, layout=Layout(height='200px', width='300px')), }) self.populate_samples({'name': 'initial'}) self.widgets['batch'].observe(self.populate_samples, names='value') if self.by_outcome: self.populate_categories({'name': 'initial'}) self.populate_subcategories({'name': 'initial'}) self.widgets['sample'].observe(self.populate_categories, names='value') self.widgets['category'].observe(self.populate_subcategories, names='value') self.widgets['subcategory'].observe(self.populate_read_ids, names='value') selection_widget_keys = [ 'batch', 'sample', 'category', 'subcategory', 'read_id' ] else: self.widgets['sample'].observe(self.populate_read_ids, names='value') selection_widget_keys = ['batch', 'sample', 'read_id'] self.populate_read_ids({'name': 'initial'}) return selection_widget_keys
def create(self, width=120, height=200): self.height = height self.refresh = Button(icon="refresh", layout={"width": "40px"}) self.refresh.on_click(self.on_refresh) self.list = Select( options=[], disabled=False, layout={"width": _px(width), "height": _px(height)} ) self.list.observe(self.on_click, names="value") self.update()
def __init__(self, parent_widget=None): super().__init__(parent_widget=parent_widget) self.box_universe = self.db.read_boxes(scope=self.scope) self.library_list = Select( options=self.box_universe.fancy_names(), #value=' ', rows=10, description='', disabled=False) self.new_child_button = Button( description='New Child', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Create a new child for the indicated selection', # icon='check' ) self.new_child_button.on_click(self.open_new_child_interface) # self.load_box_button = Button( # description='Load '+CLUSTER, # disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # tooltip='Open the indicated selection in the Selectors editor', # # icon='check' # ) # self.load_box_button.on_click(self.load_box_action) self.next_button.on_click(self.load_box_action) self.buttons = VBox([ self.new_child_button, # self.load_box_button, ]) self.popper = PseudoPopUpTextBox() self.main_window = HBox([ self.library_list, self.buttons, ]) self.make_stack( self.popper, self.main_window, ) self.library_list.observe(self.on_change_library_list) self.popper.disappear() self.on_change_library_list(None) with self.header: display(HTML("<h1>Box Selection</h1>"))
def test_index_trigger(self): select = Select(options=[1, 2, 3]) observations = [] def f(change): observations.append(change.new) select.observe(f, 'index') assert select.index == 0 select.options = [4, 5, 6] assert select.index == 0 assert select.value == 4 assert select.label == '4' assert observations == [0]
def __init__(self, rows=20, directory_only=False, ignore_dotfiles=True): self._callback = None self._directory_only = directory_only self._ignore_dotfiles = ignore_dotfiles self._empty_selection = True self._selected_dir = os.getcwd() self._item_layout = Layout(width='auto') self._nb_rows = rows self._file_selector = Select(options=self._get_selector_options(), rows=min( len(os.listdir(self._selected_dir)), self._nb_rows), layout=self._item_layout) self._open_button = Button(description='Open', layout=Layout(flex='auto 1 auto', width='auto')) self._select_button = Button(description='Select', layout=Layout(flex='auto 1 auto', width='auto')) self._cancel_button = Button(description='Cancel', layout=Layout(flex='auto 1 auto', width='auto')) self._parent_button = Button(icon='chevron-up', layout=Layout(flex='auto 1 auto', width='auto')) self._selection = Text(value=os.path.join(self._selected_dir, self._file_selector.value), disabled=True, layout=Layout(flex='1 1 auto', width='auto')) self._filename = Text(value='', layout=Layout(flex='1 1 auto', width='auto')) self._parent_button.on_click(self._parent_button_clicked) self._open_button.on_click(self._open_button_clicked) self._select_button.on_click(self._select_button_clicked) self._cancel_button.on_click(self._cancel_button_clicked) self._file_selector.observe(self._update_path) self._widget = VBox([ HBox([ self._parent_button, HTML(value='Look in:'), self._selection, ]), self._file_selector, HBox([ HTML(value='File name'), self._filename, self._open_button, self._select_button, self._cancel_button, ]), ])
def test_duplicate(self): select = Select(options=['first', 1, 'dup', 'dup']) observations = [] def f(change): observations.append(change.new) select.observe(f, 'index') select.index = 3 assert select.index == 3 assert select.value == 'dup' assert select.label == 'dup' assert observations == [3] select.index = 2 assert select.index == 2 assert select.value == 'dup' assert select.label == 'dup' assert observations == [3, 2] select.index = 0 assert select.index == 0 assert select.value == 'first' assert select.label == 'first' assert observations == [3, 2, 0] # picks the first matching value select.value = 'dup' assert select.index == 2 assert select.value == 'dup' assert select.label == 'dup' assert observations == [3, 2, 0, 2]
def __init__(self, nl, **kwargs): self.nl = nl self.list = Select(options=self.nl.symbols) self.search_box = Text(placeholder="search") self.help = HTML() super().__init__(**kwargs) self.children = [VBox([self.search_box, self.list]), self.help] self.help.layout = Layout(flex="1 1 65%") self.list.observe(self.on_select_change, names="value") self.on_select_change(None) self.search_box.observe(self.on_search_change, names="value")
def generate_pdp_grid(models: list) -> GridBox: children = [] # Row 1 children.append( Label(layout=Layout(width='auto', height='auto'), value='Choose a PDP method')) children.append( Label(layout=Layout(width='auto', height='auto'), value='Choose one or more model(s)')) # Row 2 # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( Select(description='Type', options=[elem.name for elem in PDPType], disabled=False)) # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( SelectMultiple(description='Model(s)', options=[model.name for model in models], disabled=False)) return GridBox(children=children, layout=Layout(width='auto', grid_template_columns="50% 50%", grid_template_rows='auto', align_items='center', grid_gap='3px 3px'))
def new_child(self) -> DOMWidget: """ Widget for creating new child for this `Tier`. """ child = self.tier.child_cls options = child.get_templates() mapping = {path.name: path for path in options} selection = Select( options=mapping.keys(), description='Template') def create(name, template, description): with form.status: obj = child(*self.tier.identifiers, name) obj.setup_files(mapping[template]) obj.description = description display(widgetify_html(obj._repr_html_())) form = InputSequence(create, Text(description=f'Identifier', placeholder=f'{child.id_regex}'), selection, Textarea(description='Motivation')) return form.as_widget()
def _create_widget(self): if self.dropdown: return Dropdown(options=self.classes, layout=self.layout, disabled=self.disabled) return Select(options=self.classes, layout=self.layout, disabled=self.disabled)
def display_environment_maps(self, folder): """Display visual controls for setting environment map""" supported_extensions = ['jpg', 'jpeg', 'png'] hdri_files = list() for extension in supported_extensions: hdri_files = hdri_files + glob.glob(folder + '/*.' + extension) hdri_files.sort() base_names = list() for hdri_file in hdri_files: base_names.append(os.path.basename(hdri_file)) def update_envmap(value): filename = folder + '/' + value['new'] self._client.set_environment_map(filename) cb_names = Select(description='Maps', options=base_names) cb_names.observe(update_envmap, 'value') display(cb_names)
def generate_local_interpretation_grid(models: list) -> GridBox: children = [] # Row 1 children.append( Label(layout=Layout(width='auto', height='auto'), value='Choose an interpretation method')) children.append( Label(layout=Layout(width='auto', height='auto'), value='Choose one or more model(s)')) # Row 2 # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( Select(description='Type', options=[elem.name for elem in LocalInterpreterType], disabled=False)) # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( SelectMultiple(description='Model(s)', options=[model.name for model in models], disabled=False)) # Row 3 # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( RadioButtons(options=[elem.name for elem in ExampleType], layout={'width': 'max-content'}, description='Example(s) type:', disabled=False)) # if you change the description of this widget, # you have to also adjust it in the notebook function call. children.append( IntSlider( value=1, min=1, max=10, step=1, description='Number of examples:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d', )) return GridBox(children=children, layout=Layout(width='auto', grid_template_columns="50% 50%", grid_template_rows='auto', align_items='center', grid_gap='3px 3px'))
def __init__(self, storage, setup): self.storage = storage self.setup = setup self.nans = None self.play = Play() self.step_slider = IntSlider() self.fps_slider = IntSlider(min=100, max=1000, description="1000/fps") self.product_select = Select() self.plots_box = Box() self.slider = {} self.lines = {'x': [{}, {}], 'y': [{}, {}]} for xy in ('x', 'y'): self.slider[xy] = IntRangeSlider(min=0, max=1, description=f'spectrum_{xy}', orientation='horizontal' if xy == 'x' else 'vertical') self.reinit({})
def _make_widgets(self, rows): """ Instantiate all widgets """ # Experiment selector element self.model = Dropdown( options=(), layout={ "padding": "0px 5px", "width": "initial" }, description="", ) # Variable search self.search = Text( placeholder="Search: start typing", layout={ "padding": "0px 5px", "width": "auto", "overflow-x": "scroll" }, ) # Variable selection box self.selector = Select( options=(), # sorted(self.variables.name, key=str.casefold), rows=rows, layout=self.search.layout, ) # Variable info self.info = HTML(layout=self.search.layout) # Variable filtering elements self.filter_coords = Checkbox( value=True, indent=False, description="Hide coordinates", ) self.filter_restarts = Checkbox( value=True, indent=False, description="Hide restarts", )
class DBList: def __init__(self, spark, console): self.spark = spark self.console = console self.height = 200 self.db = None self.tbl = None self.list = None self.refresh = None def _set_status(self, status): with self.console: print(status, "...") def _clear_status(self): self.console.clear_output() def create(self, width=120, height=200): self.height = height self.refresh = Button(icon="refresh", layout={"width": "40px"}) self.refresh.on_click(self.on_refresh) self.list = Select(options=[], disabled=False, layout={ "width": _px(width), "height": _px(height) }) self.list.observe(self.on_click, names="value") self.update() def on_refresh(self, _): self.update() def on_click(self, change): self.console.clear_output() if change["old"] is not None: selected = change["new"] self.update(selected) def update(self, selected=None): pass
def create(self): """Create the sidecar view""" self.sc = Sidecar(title="DBFS-%s" % os.environ["DBJL_CLUSTER"].split("-")[-1]) self.path = "/" self.flist = Select(options=[], rows=40, disabled=False) self.flist.observe(self.on_click, names="value") self.refresh = Button(description="refresh") self.refresh.on_click(self.on_refresh) self.output = Output() self.up = Button(description="up") self.up.on_click(self.on_up) with self.sc: display( VBox([ HBox([self.up, self.refresh]), self.flist, self.output ])) self.update()
def set_up_read_selection_widgets(self): condition_options = [(', '.join(c) if isinstance(c, tuple) else c, c) for c in self.group.conditions] self.widgets.update({ 'condition': Select(options=condition_options, value=self.initial_condition, layout=Layout(height='200px', width='300px')), 'replicate': Select(options=[], layout=Layout(height='200px', width='150px')), }) self.populate_replicates({'name': 'initial'}) self.widgets['condition'].observe(self.populate_replicates, names='value') if self.by_outcome: self.populate_categories({'name': 'initial'}) self.populate_subcategories({'name': 'initial'}) self.widgets['replicate'].observe(self.populate_categories, names='value') self.widgets['category'].observe(self.populate_subcategories, names='value') self.widgets['subcategory'].observe(self.populate_read_ids, names='value') selection_widget_keys = [ 'condition', 'replicate', 'category', 'subcategory', 'read_id' ] else: self.widgets['replicate'].observe(self.populate_read_ids, names='value') selection_widget_keys = ['condition', 'replicate', 'read_id'] self.populate_read_ids({'name': 'initial'}) return selection_widget_keys
def run(self, dfa): print('run') df_select = dfa # dfから先頭末尾の年を取得し、list化 # year_list = ['2002', '2004', '2006'] year_list = [str(n) for n in list(range(2002, 2006 + 1))] y1 = year_list y2 = year_list @interact(y1=Select(options=y1, rows=1, value=year_list[0]), m1=Select(options=['04', '10'], rows=1), y2=Select(options=y2, rows=1, value=year_list[-1]), m2=Select(options=['03', '09'], rows=1)) def function(y1, m1, y2, m2): print('function') start = y1 + '-' + m1 + '-01' if m2 == '03': end = y2 + '-' + m2 + '-31' elif m2 == '09': end = y2 + '-' + m2 + '-30' else: pass print(start) print(end) df_show = df_select[start:end] if len(df_show) == 0: print('エラーメッセージ') raise MyError() print(df_show) df_show.plot() df_show.plot()
def update(self): """Update the view when an element was selected""" tables = {} for obj in self.spark.sql("show tables").rdd.collect(): db = obj[0] table = obj[1] temp = obj[2] if temp and db == "": db = "temp" if tables.get(db, None) is None: tables[db] = [] if temp: tables[db].append("%s (temp)" % table) else: tables[db].append(table) for db in sorted(tables.keys()): select = Select(options=[""] + sorted(tables[db]), disabled=False) select.observe(self.on_click(db, self), names="value") self.selects.append(select) self.accordion.children = self.selects for i, db in enumerate(sorted(tables.keys())): self.accordion.set_title(i, db)
def _make_widgets(self, selvariables, **kwargs): """ Instantiate all widgets """ layout = {"padding": "0px 5px"} # Variable selector combo-widget. Pass only unique variable names. Variable # name is the only value used for filtering, so will pick up all matches self.selector = VariableSelector(selvariables.drop_duplicates("name"), **kwargs) # Button to add variable from selector to selected self.var_filter_add = Button( tooltip="Add selected variable to filter", icon="angle-double-right", layout={"width": "auto"}, ) # Button to add variable from selector to selected self.var_filter_sub = Button( tooltip="Remove selected variable from filter", icon="angle-double-left", layout={"width": "auto"}, ) self.button_box = VBox( [self.var_filter_add, self.var_filter_sub], layout={ "padding": "100px 5px", "height": "100%" }, ) # Selected variables for filtering with header widget self.var_filter_label = HTML("Filter variables:", layout=layout) self.var_filter_selected = Select( options=[], rows=10, layout=layout, ) self.filter_box = VBox( [self.var_filter_label, self.var_filter_selected], layout=layout)
class Dashboard(VBox): """ Build the dashboard for Jupyter widgets. Requires running in a notebook/jupyterlab. """ def __init__(self, net, width="95%", height="550px", play_rate=0.5): self._ignore_layer_updates = False self.player = _Player(self, play_rate) self.player.start() self.net = net r = random.randint(1, 1000000) self.class_id = "picture-dashboard-%s-%s" % (self.net.name, r) self._width = width self._height = height ## Global widgets: style = {"description_width": "initial"} self.feature_columns = IntText(description="Detail columns:", value=self.net.config["dashboard.features.columns"], min=0, max=1024, style=style) self.feature_scale = FloatText(description="Detail scale:", value=self.net.config["dashboard.features.scale"], min=0.1, max=10, style=style) self.feature_columns.observe(self.regenerate, names='value') self.feature_scale.observe(self.regenerate, names='value') ## Hack to center SVG as justify-content is broken: self.net_svg = HTML(value="""<p style="text-align:center">%s</p>""" % ("",), layout=Layout( width=self._width, overflow_x='auto', overflow_y="auto", justify_content="center")) # Make controls first: self.output = Output() controls = self.make_controls() config = self.make_config() super().__init__([config, controls, self.net_svg, self.output]) def propagate(self, inputs): """ Propagate inputs through the dashboard view of the network. """ if dynamic_pictures_check(): return self.net.propagate(inputs, class_id=self.class_id, update_pictures=True) else: self.regenerate(inputs=input) def goto(self, position): if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0: return if self.control_select.value == "Train": length = len(self.net.dataset.train_inputs) elif self.control_select.value == "Test": length = len(self.net.dataset.test_inputs) #### Position it: if position == "begin": self.control_slider.value = 0 elif position == "end": self.control_slider.value = length - 1 elif position == "prev": if self.control_slider.value - 1 < 0: self.control_slider.value = length - 1 # wrap around else: self.control_slider.value = max(self.control_slider.value - 1, 0) elif position == "next": if self.control_slider.value + 1 > length - 1: self.control_slider.value = 0 # wrap around else: self.control_slider.value = min(self.control_slider.value + 1, length - 1) self.position_text.value = self.control_slider.value def change_select(self, change=None): """ """ self.update_control_slider(change) self.regenerate() def update_control_slider(self, change=None): self.net.config["dashboard.dataset"] = self.control_select.value if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0: self.total_text.value = "of 0" self.control_slider.value = 0 self.position_text.value = 0 self.control_slider.disabled = True self.position_text.disabled = True for child in self.control_buttons.children: if not hasattr(child, "icon") or child.icon != "refresh": child.disabled = True return if self.control_select.value == "Test": self.total_text.value = "of %s" % len(self.net.dataset.test_inputs) minmax = (0, max(len(self.net.dataset.test_inputs) - 1, 0)) if minmax[0] <= self.control_slider.value <= minmax[1]: pass # ok else: self.control_slider.value = 0 self.control_slider.min = minmax[0] self.control_slider.max = minmax[1] if len(self.net.dataset.test_inputs) == 0: disabled = True else: disabled = False elif self.control_select.value == "Train": self.total_text.value = "of %s" % len(self.net.dataset.train_inputs) minmax = (0, max(len(self.net.dataset.train_inputs) - 1, 0)) if minmax[0] <= self.control_slider.value <= minmax[1]: pass # ok else: self.control_slider.value = 0 self.control_slider.min = minmax[0] self.control_slider.max = minmax[1] if len(self.net.dataset.train_inputs) == 0: disabled = True else: disabled = False self.control_slider.disabled = disabled self.position_text.disbaled = disabled self.position_text.value = self.control_slider.value for child in self.control_buttons.children: if not hasattr(child, "icon") or child.icon != "refresh": child.disabled = disabled def update_zoom_slider(self, change): if change["name"] == "value": self.net.config["svg_scale"] = self.zoom_slider.value self.regenerate() def update_position_text(self, change): # {'name': 'value', 'old': 2, 'new': 3, 'owner': IntText(value=3, layout=Layout(width='100%')), 'type': 'change'} self.control_slider.value = change["new"] def get_current_input(self): if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0: return self.net.dataset.train_inputs[self.control_slider.value] elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0: return self.net.dataset.test_inputs[self.control_slider.value] def get_current_targets(self): if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0: return self.net.dataset.train_targets[self.control_slider.value] elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0: return self.net.dataset.test_targets[self.control_slider.value] def update_slider_control(self, change): if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0: self.total_text.value = "of 0" return if change["name"] == "value": self.position_text.value = self.control_slider.value if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0: self.total_text.value = "of %s" % len(self.net.dataset.train_inputs) if self.net.model is None: return if not dynamic_pictures_check(): self.regenerate(inputs=self.net.dataset.train_inputs[self.control_slider.value], targets=self.net.dataset.train_targets[self.control_slider.value]) return output = self.net.propagate(self.net.dataset.train_inputs[self.control_slider.value], class_id=self.class_id, update_pictures=True) if self.feature_bank.value in self.net.layer_dict.keys(): self.net.propagate_to_features(self.feature_bank.value, self.net.dataset.train_inputs[self.control_slider.value], cols=self.feature_columns.value, scale=self.feature_scale.value, html=False) if self.net.config["show_targets"]: if len(self.net.output_bank_order) == 1: ## FIXME: use minmax of output bank self.net.display_component([self.net.dataset.train_targets[self.control_slider.value]], "targets", class_id=self.class_id, minmax=(-1, 1)) else: self.net.display_component(self.net.dataset.train_targets[self.control_slider.value], "targets", class_id=self.class_id, minmax=(-1, 1)) if self.net.config["show_errors"]: ## minmax is error if len(self.net.output_bank_order) == 1: errors = np.array(output) - np.array(self.net.dataset.train_targets[self.control_slider.value]) self.net.display_component([errors.tolist()], "errors", class_id=self.class_id, minmax=(-1, 1)) else: errors = [] for bank in range(len(self.net.output_bank_order)): errors.append( np.array(output[bank]) - np.array(self.net.dataset.train_targets[self.control_slider.value][bank])) self.net.display_component(errors, "errors", class_id=self.class_id, minmax=(-1, 1)) elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0: self.total_text.value = "of %s" % len(self.net.dataset.test_inputs) if self.net.model is None: return if not dynamic_pictures_check(): self.regenerate(inputs=self.net.dataset.test_inputs[self.control_slider.value], targets=self.net.dataset.test_targets[self.control_slider.value]) return output = self.net.propagate(self.net.dataset.test_inputs[self.control_slider.value], class_id=self.class_id, update_pictures=True) if self.feature_bank.value in self.net.layer_dict.keys(): self.net.propagate_to_features(self.feature_bank.value, self.net.dataset.test_inputs[self.control_slider.value], cols=self.feature_columns.value, scale=self.feature_scale.value, html=False) if self.net.config["show_targets"]: ## FIXME: use minmax of output bank self.net.display_component([self.net.dataset.test_targets[self.control_slider.value]], "targets", class_id=self.class_id, minmax=(-1, 1)) if self.net.config["show_errors"]: ## minmax is error if len(self.net.output_bank_order) == 1: errors = np.array(output) - np.array(self.net.dataset.test_targets[self.control_slider.value]) self.net.display_component([errors.tolist()], "errors", class_id=self.class_id, minmax=(-1, 1)) else: errors = [] for bank in range(len(self.net.output_bank_order)): errors.append( np.array(output[bank]) - np.array(self.net.dataset.test_targets[self.control_slider.value][bank])) self.net.display_component(errors, "errors", class_id=self.class_id, minmax=(-1, 1)) def toggle_play(self, button): ## toggle if self.button_play.description == "Play": self.button_play.description = "Stop" self.button_play.icon = "pause" self.player.resume() else: self.button_play.description = "Play" self.button_play.icon = "play" self.player.pause() def prop_one(self, button=None): self.update_slider_control({"name": "value"}) def regenerate(self, button=None, inputs=None, targets=None): ## Protection when deleting object on shutdown: if isinstance(button, dict) and 'new' in button and button['new'] is None: return ## Update the config: self.net.config["dashboard.features.bank"] = self.feature_bank.value self.net.config["dashboard.features.columns"] = self.feature_columns.value self.net.config["dashboard.features.scale"] = self.feature_scale.value inputs = inputs if inputs is not None else self.get_current_input() targets = targets if targets is not None else self.get_current_targets() features = None if self.feature_bank.value in self.net.layer_dict.keys() and inputs is not None: if self.net.model is not None: features = self.net.propagate_to_features(self.feature_bank.value, inputs, cols=self.feature_columns.value, scale=self.feature_scale.value, display=False) svg = """<p style="text-align:center">%s</p>""" % (self.net.to_svg( inputs=inputs, targets=targets, class_id=self.class_id, highlights={self.feature_bank.value: { "border_color": "orange", "border_width": 30, }})) if inputs is not None and features is not None: html_horizontal = """ <table align="center" style="width: 100%%;"> <tr> <td valign="top" style="width: 50%%;">%s</td> <td valign="top" align="center" style="width: 50%%;"><p style="text-align:center"><b>%s</b></p>%s</td> </tr> </table>""" html_vertical = """ <table align="center" style="width: 100%%;"> <tr> <td valign="top">%s</td> </tr> <tr> <td valign="top" align="center"><p style="text-align:center"><b>%s</b></p>%s</td> </tr> </table>""" self.net_svg.value = (html_vertical if self.net.config["svg_rotate"] else html_horizontal) % ( svg, "%s details" % self.feature_bank.value, features) else: self.net_svg.value = svg def make_colormap_image(self, colormap_name): from .layers import Layer if not colormap_name: colormap_name = get_colormap() layer = Layer("Colormap", 100) minmax = layer.get_act_minmax() image = layer.make_image(np.arange(minmax[0], minmax[1], .01), colormap_name, {"pixels_per_unit": 1, "svg_rotate": self.net.config["svg_rotate"]}).resize((300, 25)) return image def set_attr(self, obj, attr, value): if value not in [{}, None]: ## value is None when shutting down if isinstance(value, dict): value = value["value"] if isinstance(obj, dict): obj[attr] = value else: setattr(obj, attr, value) ## was crashing on Widgets.__del__, if get_ipython() no longer existed self.regenerate() def make_controls(self): layout = Layout(width='100%', height="100%") button_begin = Button(icon="fast-backward", layout=layout) button_prev = Button(icon="backward", layout=layout) button_next = Button(icon="forward", layout=layout) button_end = Button(icon="fast-forward", layout=layout) #button_prop = Button(description="Propagate", layout=Layout(width='100%')) #button_train = Button(description="Train", layout=Layout(width='100%')) self.button_play = Button(icon="play", description="Play", layout=layout) step_down = Button(icon="sort-down", layout=Layout(width="95%", height="100%")) step_up = Button(icon="sort-up", layout=Layout(width="95%", height="100%")) up_down = HBox([step_down, step_up], layout=Layout(width="100%", height="100%")) refresh_button = Button(icon="refresh", layout=Layout(width="25%", height="100%")) self.position_text = IntText(value=0, layout=layout) self.control_buttons = HBox([ button_begin, button_prev, #button_train, self.position_text, button_next, button_end, self.button_play, up_down, refresh_button ], layout=Layout(width='100%', height="100%")) length = (len(self.net.dataset.train_inputs) - 1) if len(self.net.dataset.train_inputs) > 0 else 0 self.control_slider = IntSlider(description="Dataset index", continuous_update=False, min=0, max=max(length, 0), value=0, layout=Layout(width='100%')) if self.net.config["dashboard.dataset"] == "Train": length = len(self.net.dataset.train_inputs) else: length = len(self.net.dataset.test_inputs) self.total_text = Label(value="of %s" % length, layout=Layout(width="100px")) self.zoom_slider = FloatSlider(description="Zoom", continuous_update=False, min=0, max=1.0, style={"description_width": 'initial'}, layout=Layout(width="65%"), value=self.net.config["svg_scale"] if self.net.config["svg_scale"] is not None else 0.5) ## Hook them up: button_begin.on_click(lambda button: self.goto("begin")) button_end.on_click(lambda button: self.goto("end")) button_next.on_click(lambda button: self.goto("next")) button_prev.on_click(lambda button: self.goto("prev")) self.button_play.on_click(self.toggle_play) self.control_slider.observe(self.update_slider_control, names='value') refresh_button.on_click(lambda widget: (self.update_control_slider(), self.output.clear_output(), self.regenerate())) step_down.on_click(lambda widget: self.move_step("down")) step_up.on_click(lambda widget: self.move_step("up")) self.zoom_slider.observe(self.update_zoom_slider, names='value') self.position_text.observe(self.update_position_text, names='value') # Put them together: controls = VBox([HBox([self.control_slider, self.total_text], layout=Layout(height="40px")), self.control_buttons], layout=Layout(width='100%')) #net_page = VBox([control, self.net_svg], layout=Layout(width='95%')) controls.on_displayed(lambda widget: self.regenerate()) return controls def move_step(self, direction): """ Move the layer stepper up/down through network """ options = [""] + [layer.name for layer in self.net.layers] index = options.index(self.feature_bank.value) if direction == "up": new_index = (index + 1) % len(options) else: ## down new_index = (index - 1) % len(options) self.feature_bank.value = options[new_index] self.regenerate() def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion def save_config(self, widget=None): self.net.save_config() def update_layer(self, change): """ Update the layer object, and redisplay. """ if self._ignore_layer_updates: return ## The rest indicates a change to a display variable. ## We need to save the value in the layer, and regenerate ## the display. # Get the layer: layer = self.net[self.layer_select.value] # Save the changed value in the layer: layer.feature = self.layer_feature.value layer.visible = self.layer_visible_checkbox.value ## These three, dealing with colors of activations, ## can be done with a prop_one(): if "color" in change["owner"].description.lower(): ## Matches: Colormap, lefmost color, rightmost color ## overriding dynamic minmax! layer.minmax = (self.layer_mindim.value, self.layer_maxdim.value) layer.minmax = (self.layer_mindim.value, self.layer_maxdim.value) layer.colormap = self.layer_colormap.value if self.layer_colormap.value else None self.layer_colormap_image.value = """<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap)) self.prop_one() else: self.regenerate() def update_layer_selection(self, change): """ Just update the widgets; don't redraw anything. """ ## No need to redisplay anything self._ignore_layer_updates = True ## First, get the new layer selected: layer = self.net[self.layer_select.value] ## Now, let's update all of the values without updating: self.layer_visible_checkbox.value = layer.visible self.layer_colormap.value = layer.colormap if layer.colormap != "" else "" self.layer_colormap_image.value = """<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap)) minmax = layer.get_act_minmax() self.layer_mindim.value = minmax[0] self.layer_maxdim.value = minmax[1] self.layer_feature.value = layer.feature self._ignore_layer_updates = False
cmd("files-cp input.tgz agave://${STORAGE_MACHINE}/inputs/${INPUT_DIR}/") submitJob(nodes, procs[0], cur_model, jobNameText.value, machines.value, queues.value) runBtn.on_click(runfun_btn_clicked) runBox = VBox(run_items) ##################################### Run tab end ################################################# ################################# Output tab ################################### jobListBtn = Button(description='List all jobs', button_style='primary', layout= Layout(width = '115px')) jobSelect = Select(layout = Layout(height = '150px', width='100%')) jobOutputBtn = Button(description='List job output', button_style='primary', layout= Layout(width = '115px')) abortBtn = Button(description='Abort', button_style='danger', layout= Layout(width = 'auto')) outputSelect = Select(layout = Layout(height = '150px', width='100%')) downloadOpBtn = Button(description='Download', button_style='primary', layout= Layout(width = '115px')) jobHisBtn = Button(description='Job history', button_style='primary', layout= Layout(width = '115px')) jobHisSelect = Select(layout = Layout(height = '336px', width='100%')) def jobList_btn_clicked(a): with logOp:
class Dbfs(object): """Database browser implementation Args: dbutils (DBUtils): DBUtils object (for fs only) """ def __init__(self, dbutils): self.dbutils = dbutils self.running = False self.path = None self.flist = None self.refresh = None self.path_view = None self.preview = None self.up = None def create(self, path="/", height="400px"): if self.running: print("dbfs browser already running. Use close() first") return self.path = path self.flist = Select(options=[], disabled=False, layout={"height": height}) self.flist.observe(self.on_click, names="value") self.refresh = Button(icon="refresh", layout={"width": "40px"}) self.refresh.on_click(self.on_refresh) self.path_view = Output() self.preview = Output( layout={ "width": "800px", "height": height, "overflow": "scroll", "border": "1px solid gray", }) self.up = Button(icon="arrow-up", layout={"width": "40px"}) self.up.on_click(self.on_up) display( VBox([ HBox([self.refresh, self.up, self.path_view]), HBox([self.flist, self.preview]), ])) self.update() self.running = True def convertBytes(self, fsize): """Convert bytes to largest unit Args: fsize (int): Size in bytes Returns: tuple: size of largest unit, largest unit """ size = fsize unit = "B" if size > 1024 * 1024 * 1024 * 10: size = int(size / 1024.0 / 1024.0 / 1024.0) unit = "GB" elif size > 1024 * 1024 * 10: size = int(size / 1024.0 / 1024.0) unit = "MB" elif size > 1024 * 10: size = int(size / 1024.0) unit = "KB" return (size, unit) def update(self): """Update the view when an element was selected""" self.path_view.clear_output() self.preview.clear_output() with self.path_view: print("updating ...") try: fobjs = self.dbutils.fs.ls(self.path) except: # pylint: disable=bare-except with self.path_view: print("Error: Cannot access folder") return False self.show_path(self.path) dirs = sorted([fobj.name for fobj in fobjs if fobj.isDir()], key=lambda x: x.lower()) files = sorted( [ "%s (%d %s)" % ((fobj.name, ) + self.convertBytes(fobj.size)) for fobj in fobjs if not fobj.isDir() ], key=lambda x: x[0].lower(), ) self.flist.options = [""] + dirs + files return True def show_path(self, path): """Show path in output widget Args: path (str): Currently selected path """ self.path_view.clear_output() with self.path_view: print("dbfs:" + re.sub(r"\s\(.*?\)$", "", path)) def show_preview(self, path): """Show preview of csv, md or txt in output widget Args: path (str): Currently selected path """ real_path = re.sub(r"\s\(.*?\)$", "", path) parts = real_path.split(".") if len(parts) > 0 and parts[-1].lower() in [ "md", "html", "csv", "txt", "sh", "sql", "py", "scala", "json", "jpg", "jpeg", "png", "gif", ]: ext = parts[-1].lower() filename = "/dbfs" + real_path # text = self.dbutils.fs.head(real_path) self.preview.clear_output() with self.preview: if ext == "html": display(HTML(filename=filename)) elif ext in ["py", "sh", "sql", "scala"]: display(Code(filename=filename)) elif ext in ["jpg", "jpeg", "png", "gif"]: display(Image(filename=filename)) elif ext == "md": display(Markdown(filename=filename)) elif ext == "csv": df = pd.read_csv(filename) display(df) else: with open(filename, "r") as fd: print(fd.read()) def on_refresh(self, _): """Refresh handler Args: b (ipywidgets.Button): clicked button """ self.update() def on_up(self, _): """Up handler Args: b (ipywidgets.Button): clicked button """ new_path = os.path.dirname(self.path.rstrip("/")) if new_path != self.path: self.path = new_path self.update() def on_click(self, change): """Click handler providing db and parent as context Args: db (str): database name parent (object): parent object """ new_path = os.path.join(self.path, change["new"]) if change["old"] is not None: if len(change["new"]) > 0 and change["new"][-1] == "/": old_path = self.path self.path = new_path if not self.update(): self.path = old_path else: self.show_path(new_path) self.show_preview(new_path) def close(self): """Close view""" self.running = False
def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion
class ExplorerSelectionLibrary(ExplorerPane): def __init__(self, parent_widget=None): super().__init__(parent_widget=parent_widget) self.box_universe = self.db.read_boxes(scope=self.scope) self.library_list = Select( options=self.box_universe.fancy_names(), #value=' ', rows=10, description='', disabled=False) self.new_child_button = Button( description='New Child', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip='Create a new child for the indicated selection', # icon='check' ) self.new_child_button.on_click(self.open_new_child_interface) # self.load_box_button = Button( # description='Load '+CLUSTER, # disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # tooltip='Open the indicated selection in the Selectors editor', # # icon='check' # ) # self.load_box_button.on_click(self.load_box_action) self.next_button.on_click(self.load_box_action) self.buttons = VBox([ self.new_child_button, # self.load_box_button, ]) self.popper = PseudoPopUpTextBox() self.main_window = HBox([ self.library_list, self.buttons, ]) self.make_stack( self.popper, self.main_window, ) self.library_list.observe(self.on_change_library_list) self.popper.disappear() self.on_change_library_list(None) with self.header: display(HTML("<h1>Box Selection</h1>")) def on_change_library_list(self, action_content): try: chained_ = self.get_current_selection_chained() self.footer.clear_output() with self.footer: if chained_ is None: print( "Scenario Universe always contains all scenarios, there cannot be any thresholds." ) self.next_button.tooltip = f"Cannot Load Feature Selection for Scenario Universe" self.next_button.disabled = True else: print(chained_.chain_repr()) self.next_button.tooltip = f"Load Feature Selection for {chained_.chain_repr()}" self.next_button.disabled = False except: logger.exception("error on on_change_library_list") raise def show_main_window(self, z=None): # with self.footer: # print("show_main_window") self.main_window.layout.display = 'flex' self.main_window.layout.visibility = 'visible' def hide_main_window(self, z=None): # with self.footer: # print("hide_main_window") self.main_window.layout.visibility = 'hidden' self.main_window.layout.display = 'none' def open_new_child_interface(self, z): # with self.footer: # print("open_new_child_interface") # print(z) self.hide_main_window() self.popper.appear() self.popper.ok_button.on_click(self.close_new_child_interface) chained_ = self.get_current_selection_chained() self.footer.clear_output() with self.footer: print("Name a new child for:") print(chained_.names[0]) for n, c in enumerate(chained_.names[1:]): print(" " * (n + 1) + f"┗━ {c}") def close_new_child_interface(self, z): with self.footer: print("close_new_child_interface") print(z) self.popper.disappear() self.popper.ok_button.on_click(self.close_new_child_interface, remove=True) self.show_main_window() new_child_name = self.popper.text_entry.value plain_ = self.get_current_selection_plain() if new_child_name in self._parent_widget.box_universe: # ERROR logger.warn( f'A box with the name "{new_child_name}" already exists') self.footer.clear_output() with self.footer: print( f'ERROR: A box with the name "{new_child_name}" already exists' ) else: self._parent_widget.box_universe[new_child_name] = Box(plain_) logger.info("New Names = " + str(self._parent_widget.box_universe.fancy_names())) self.library_list.options = self._parent_widget.box_universe.fancy_names( ) def load_box_action(self, z=None): logger.info("load_box_action") plain_ = self.get_current_selection_plain() if plain_ is None: logger.warn( f"cannot load *Scenario Universe*, create a new child or choose another name" ) self.footer.clear_output() with self.footer: print( "cannot load *Scenario Universe*, create a new child or choose another name" ) else: logger.info(f" load_box_action:{plain_}") try: self._parent_widget.load_feature_selection(plain_) except Exception as err: logger.info(f"error {err}") with self.footer: print(err) raise def get_current_selection_plain(self): return self._parent_widget.box_universe.plain_names()[ self.library_list.index] def get_current_selection_chained(self): plain_ = self.get_current_selection_plain() if plain_ is None: return None else: return ChainedBox(self._parent_widget.box_universe, plain_)
class FileChooser(VBox): """FileChooser class.""" _LBL_TEMPLATE = '<span style="margin-left:10px; color:{1};">{0}</span>' _LBL_NOFILE = 'No file selected' def __init__(self, path=os.getcwd(), filename='', title='', select_desc='Select', change_desc='Change', show_hidden=False, select_default=False, use_dir_icons=False, **kwargs): """Initialize FileChooser object.""" self._default_path = path.rstrip(os.path.sep) self._default_filename = filename self._selected_path = None self._selected_filename = None self._show_hidden = show_hidden self._select_desc = select_desc self._change_desc = change_desc self._callback = None self._use_dir_icons = use_dir_icons # Widgets self._pathlist = Dropdown(description="", layout=Layout(width='auto', grid_area='pathlist')) self._filename = Text(placeholder='output filename', layout=Layout(width='auto', grid_area='filename')) self._dircontent = Select(rows=8, layout=Layout(width='auto', grid_area='dircontent')) self._cancel = Button(description='Cancel', layout=Layout(width='auto', display='none')) self._select = Button(description=self._select_desc, layout=Layout(width='auto')) self._title = HTML(value=title) if title == '': self._title.layout.display = 'none' # Widget observe handlers self._pathlist.observe(self._on_pathlist_select, names='value') self._dircontent.observe(self._on_dircontent_select, names='value') self._filename.observe(self._on_filename_change, names='value') self._select.on_click(self._on_select_click) self._cancel.on_click(self._on_cancel_click) # Selected file label self._label = HTML(value=self._LBL_TEMPLATE.format( self._LBL_NOFILE, 'black'), placeholder='', description='') # Layout self._gb = GridBox( children=[self._pathlist, self._filename, self._dircontent], layout=Layout(display='none', width='500px', grid_gap='0px 0px', grid_template_rows='auto auto', grid_template_columns='60% 40%', grid_template_areas=''' 'pathlist filename' 'dircontent dircontent' ''')) buttonbar = HBox(children=[self._select, self._cancel, self._label], layout=Layout(width='auto')) # Call setter to set initial form values self._set_form_values(self._default_path, self._default_filename) # Use the defaults as the selected values if select_default: self._apply_selection() # Call VBox super class __init__ super().__init__(children=[ self._title, self._gb, buttonbar, ], layout=Layout(width='auto'), **kwargs) def _set_form_values(self, path, filename): """Set the form values.""" # Disable triggers to prevent selecting an entry in the Select # box from automatically triggering a new event. self._pathlist.unobserve(self._on_pathlist_select, names='value') self._dircontent.unobserve(self._on_dircontent_select, names='value') self._filename.unobserve(self._on_filename_change, names='value') # Set form values self._pathlist.options = get_subpaths(path) self._pathlist.value = path self._filename.value = filename # file/folder real names dircontent_real_names = get_dir_contents(path, hidden=self._show_hidden, prepend_icons=False) # file/folder display names dircontent_display_names = get_dir_contents( path, hidden=self._show_hidden, prepend_icons=self._use_dir_icons) # Dict to map real names to display names self._map_name_to_disp = { real_name: disp_name for real_name, disp_name in zip(dircontent_real_names, dircontent_display_names) } # Dict to map display names to real names self._map_disp_to_name = dict( reversed(item) for item in self._map_name_to_disp.items()) # Set _dircontent form value to display names self._dircontent.options = dircontent_display_names # If the value in the filename Text box equals a value in the # Select box and the entry is a file then select the entry. if ((filename in dircontent_real_names) and os.path.isfile(os.path.join(path, filename))): self._dircontent.value = self._map_name_to_disp[filename] else: self._dircontent.value = None # Reenable triggers again self._pathlist.observe(self._on_pathlist_select, names='value') self._dircontent.observe(self._on_dircontent_select, names='value') self._filename.observe(self._on_filename_change, names='value') # Update the state of the select button if self._gb.layout.display is None: # Disable the select button if path and filename # - equal an existing folder in the current view # - equal the already selected values check1 = filename in dircontent_real_names check2 = os.path.isdir(os.path.join(path, filename)) check3 = False # Only check selected if selected is set if ((self._selected_path is not None) and (self._selected_filename is not None)): selected = os.path.join(self._selected_path, self._selected_filename) check3 = os.path.join(path, filename) == selected if (check1 and check2) or check3: self._select.disabled = True else: self._select.disabled = False def _on_pathlist_select(self, change): """Handle selecting a path entry.""" self._set_form_values(change['new'], self._filename.value) def _on_dircontent_select(self, change): """Handle selecting a folder entry.""" new_path = os.path.realpath( os.path.join(self._pathlist.value, self._map_disp_to_name[change['new']])) # Check if folder or file if os.path.isdir(new_path): path = new_path filename = self._filename.value elif os.path.isfile(new_path): path = self._pathlist.value filename = self._map_disp_to_name[change['new']] self._set_form_values(path, filename) def _on_filename_change(self, change): """Handle filename field changes.""" self._set_form_values(self._pathlist.value, change['new']) def _on_select_click(self, b): """Handle select button clicks.""" if self._gb.layout.display == 'none': # If not shown, open the dialog self._show_dialog() else: # If shown, close the dialog and apply the selection self._apply_selection() # Execute callback function if self._callback is not None: try: self._callback(self) except TypeError: # Support previous behaviour of not passing self self._callback() def _show_dialog(self): """Show the dialog.""" # Show dialog and cancel button self._gb.layout.display = None self._cancel.layout.display = None # Show the form with the correct path and filename if ((self._selected_path is not None) and (self._selected_filename is not None)): path = self._selected_path filename = self._selected_filename else: path = self._default_path filename = self._default_filename self._set_form_values(path, filename) def _apply_selection(self): """Close the dialog and apply the selection.""" self._gb.layout.display = 'none' self._cancel.layout.display = 'none' self._select.description = self._change_desc self._selected_path = self._pathlist.value self._selected_filename = self._filename.value selected = os.path.join(self._selected_path, self._selected_filename) if os.path.isfile(selected): self._label.value = self._LBL_TEMPLATE.format(selected, 'orange') else: self._label.value = self._LBL_TEMPLATE.format(selected, 'green') def _on_cancel_click(self, b): """Handle cancel button clicks.""" self._gb.layout.display = 'none' self._cancel.layout.display = 'none' self._select.disabled = False def reset(self, path=None, filename=None): """Reset the form to the default path and filename.""" self._selected_path = '' self._selected_filename = '' self._label.value = self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black') if path is not None: self._default_path = path.rstrip(os.path.sep) if filename is not None: self._default_filename = filename self._set_form_values(self._default_path, self._default_filename) def refresh(self): """Re-render the form.""" self._set_form_values(self._pathlist.value, self._filename.value) @property def show_hidden(self): """Get _show_hidden value.""" return self._show_hidden @show_hidden.setter def show_hidden(self, hidden): """Set _show_hidden value.""" self._show_hidden = hidden self.refresh() @property def use_dir_icons(self): """Get _use_dir_icons value.""" return self._use_dir_icons @use_dir_icons.setter def use_dir_icons(self, dir_icons): """Set _use_dir_icons value.""" self._use_dir_icons = dir_icons self.refresh() @property def rows(self): """Get current number of rows.""" return self._dircontent.rows @rows.setter def rows(self, rows): """Set number of rows.""" self._dircontent.rows = rows @property def title(self): """Get the title.""" return self._title.value @title.setter def title(self, title): """Set the title.""" self._title.value = title if title == '': self._title.layout.display = 'none' else: self._title.layout.display = None @property def default(self): """Get the default value.""" return os.path.join(self._default_path, self._default_filename) @property def default_path(self): """Get the default_path value.""" return self._default_path @default_path.setter def default_path(self, path): """Set the default_path.""" self._default_path = path.rstrip(os.path.sep) self._set_form_values(self._default_path, self._filename.value) @property def default_filename(self): """Get the default_filename value.""" return self._default_filename @default_filename.setter def default_filename(self, filename): """Set the default_filename.""" self._default_filename = filename self._set_form_values(self._pathlist.value, self._default_filename) @property def selected(self): """Get selected value.""" try: return os.path.join(self._selected_path, self._selected_filename) except TypeError: return None @property def selected_path(self): """Get selected_path value.""" return self._selected_path @property def selected_filename(self): """Get the selected_filename.""" return self._selected_filename def __repr__(self): """Build string representation.""" str_ = ("FileChooser(" "path='{0}', " "filename='{1}', " "show_hidden='{2}')").format(self._default_path, self._default_filename, self._show_hidden) return str_ def register_callback(self, callback): """Register a callback function.""" self._callback = callback