Source code for abipy.tools.notebooks

"""Tools for ipython notebooks."""











[docs]def ipw_listdir(top=".", recurse=True, widget_type="dropdown"): # pragma: no cover """ Return an ipython widget listing all the files located within the directory ``top`` that can be inspected with :ref:`abiopen.py`. The user can select the file in the widget and print info on the corresponding file inside the notebook. Args: top: Initial directory. recurse: False to ignore directories within ``top``. widget_type: Specify the widget to create. Possible values in: ["tooglebuttons", "dropdown", "radiobuttons"] """ from abipy import abilab from IPython.display import display, clear_output import ipywidgets as ipw # Select the widget class from widget_type d = dict( tooglebuttons=ipw.ToggleButtons, dropdown=ipw.Dropdown, radiobuttons=ipw.RadioButtons, ) try: widget_class = d[widget_type] except KeyError: raise KeyError("Invalid `widget_type`: %s, Choose among: %s" % (widget_type, str(list(d.keys())))) def on_value_change(change): """Callback""" clear_output() path = change["new"] #print(change) with abilab.abiopen(path) as abifile: print(abifile) #display(abifile) # Get dict: dirname --> list_of_files supported by abiopen. dir2files = abilab.dir2abifiles(top, recurse=recurse) children = [] for dirname, files in dir2files.items(): w = widget_class(options=files, description="%s:" % dirname) # TODO: Should register the callback of "selected" but I didn't find the event type! w.observe(on_value_change, names='value', type="change") children.append(w) box = ipw.VBox(children=children) return display(box)