def carousel(children=(), **layout): "A horizontally scrolling carousel" def_layout = dict(overflow='scroll hidden', flex_flow='row', display='flex') res = Box([], layout=merge(def_layout, layout)) res.observe(_update_children, names='children') res.children = children return res
def make_box_for_grid(image_widget, title, size=(250, 250)): """ Make a VBox to hold caption/image for demonstrating option_fit values. """ modes = ['oblique', 'X', 'Y', 'Z'] # make layout box_layout = Layout() box_layout.width = f'{size[0]}px' box_layout.height = f'{size[1]}px' box_layout.border = '0px' # Make the caption if title is not None: title_str = "'{}'".format(title) else: title_str = str(title) h = HTML(value='' + str(title_str) + '') # Make the box with the image widget inside it boxb = Box() boxb.layout = box_layout boxb.children = [image_widget] # Compose into a vertical box vb = VBox() vb.layout.align_items = 'center' vb.children = [h, boxb] return vb
def carousel( children: (tuple, list) = (), # `Box` objects to display in a carousel **layout ) -> Box: # An `ipywidget`'s carousel "A horizontally scrolling carousel" def_layout = dict(overflow='scroll hidden', flex_flow='row', display='flex') res = Box([], layout=merge(def_layout, layout)) res.observe(_update_children, names='children') res.children = children return res
def on_use_button_clicked(current: widgets.Button, results: List[Dict[str, str]], layout: widgets.Box) -> None: molecule_sheet = layout.children[LayoutPosition.SEARCH_OUTPUT.value] for i, button in enumerate(molecule_sheet.cells[0].value): if button == current: clear_search_output(layout) add_row_with_inchi(results[i]["name"], results[i]["inchi"]) return row_display = widgets.HTML(value="Could not located clicked button!") layout.children = swap_layout(layout.children, LayoutPosition.SEARCH_OUTPUT.value, row_display)
def extract_all(layout: widgets.Box) -> None: """launch msms refs extraction and export""" with get_new_log_box(layout): if is_valid_input_sheet(): logger.info("Extracting MSMS reference spectrums....") input_sheet = ipysheet.sheet("input") new_refs_df = generate_msms_refs_df(input_sheet) if new_refs_df.empty: return sheet = create_refs_sheet(new_refs_df) layout.children = swap_layout(layout.children, LayoutPosition.SHEET_OUTPUT.value, sheet)
def is_valid_num_results(num: int, input_value: str, layout: widgets.Box, max_valid: int = 100) -> bool: if 0 < num <= max_valid: return True if num == 0: message = f"""<b>No molecule names containing '{input_value}' and with a MW within the search range were found in the database.</b>""" else: message = f"""<b>Too many matches (>{max_valid}). {num} matches of '{input_value}' within the MW range were found in the database.</b>""" message_widget = widgets.HTML(value=message) layout.children = swap_layout(layout.children, LayoutPosition.SEARCH_OUTPUT.value, message_widget) return False
def _interactive(interact_f, kwargs_widgets): # this is a modified version of interactive() in ipywidget.widgets.interaction container = Box(_dom_classes=['widget-interact']) container.children = [w for w in kwargs_widgets if isinstance(w, DOMWidget)] def call_f(name=None, old=None, new=None): kwargs = dict((widget._kwarg, widget.value) for widget in kwargs_widgets) try: interact_f(**kwargs) except Exception as e: container.log.warn("Exception in interact callback: %s", e, exc_info=True) for widget in kwargs_widgets: widget.on_trait_change(call_f, 'value') container.on_displayed(lambda _: call_f(None, None, None)) return container
def search(query: str, min_mw: float, max_mw: float, layout: widgets.Box) -> None: with get_new_log_box(layout): clear_search_output(layout) results = get_synonym_matches(query) for cur in results: RDLogger.DisableLog("rdApp.*") # hide rdkit warnings cur["mol"] = cheminfo.normalize_molecule( Chem.inchi.MolFromInchi(cur["inchi"])) cur["norm_inchi"] = Chem.inchi.MolToInchi(cur["mol"]) RDLogger.EnableLog("rdApp.*") cur["MW"] = ExactMolWt(cur["mol"]) filtered = filter_by_mw(filter_to_norm_inchi_in_db(results), min_mw, max_mw) logger.debug("Found %d matches to %s.", len(filtered), query) if not is_valid_num_results(len(filtered), query, layout): return final = sorted(filtered, key=lambda x: x["MW"]) logger.debug("Num mols: %d", len(final)) column_names = ["", "Name", "MW", "Structure"] sheet = ipysheet.sheet( rows=len(final), columns=len(column_names), column_headers=column_names, column_resizing=False, column_width=[1, 4, 2, 10], ) buttons = [ widgets.Button(description="use", layout=widgets.Layout(width="100%")) for x in final ] for button in buttons: button.on_click( lambda current: on_use_button_clicked(current, final, layout)) ipysheet.column(0, buttons) ipysheet.column(1, [x["name"] for x in final]) ipysheet.column(2, [ExactMolWt(x["mol"]) for x in final]) ipysheet.column(3, [cheminfo.mol_to_image(x["mol"]) for x in final]) layout.children = swap_layout(layout.children, LayoutPosition.SEARCH_OUTPUT.value, sheet)
def clear_search_output(layout: widgets.Box) -> None: blank = widgets.HTML(value="") layout.children = swap_layout(layout.children, LayoutPosition.SEARCH_OUTPUT.value, blank)