def master_update_structure(search_mpid, upload_data): if not search_mpid and not upload_data: raise PreventUpdate search_mpid = search_mpid or {} upload_data = upload_data or {} time_searched = search_mpid.get("time_requested", -1) time_uploaded = upload_data.get("time_requested", -1) if time_searched > time_uploaded: if search_mpid is None or "mpid" not in search_mpid: raise PreventUpdate with MPRester() as mpr: try: struct = mpr.get_task_data(search_mpid["mpid"], "structure")[0][ "structure" ] print("Struct from task.") except: struct = mpr.get_structure_by_material_id(search_mpid["mpid"]) print("Struct from material.") else: struct = MPComponent.from_data(upload_data["data"]) return MPComponent.to_data(struct.as_dict())
def master_update_structure(search_mpid: Optional[str], upload_data: Optional[str]): """ A new structure is loaded either from the search component or from the upload component. This callback triggers the update, and uses the callback context to determine which should take precedence if there is both a search term and uploaded data present. Args: search_mpid: e.g. "mp-11358" upload_data: output of upload component, {"data": ..., "error" ...} Returns: an encoded Structure """ if not search_mpid and not upload_data: raise PreventUpdate if not dash.callback_context.triggered: raise PreventUpdate if len(dash.callback_context.triggered) > 1: # triggered by both on initial load load_by = "mpid" elif ( dash.callback_context.triggered[0]["prop_id"] == search_component.id() + ".data" ): load_by = "mpid" else: load_by = "uploaded" upload_data = upload_data or {} if load_by == "mpid": if search_mpid is None: raise PreventUpdate with MPRester() as mpr: # TODO: add comprehensive fix to this in pymatgen try: struct = mpr.get_task_data(search_mpid, "structure")[0]["structure"] print("Struct from task.") except: struct = mpr.get_structure_by_material_id(search_mpid) print("Struct from material.") else: struct = MPComponent.from_data(upload_data["data"]) return struct
], className="mpc-panel-description", ) # endregion ################################################################################ # region DEFINE MAIN LAYOUT ################################################################################ master_layout = Container( [ dcc.Location(id="url", refresh=False), MPComponent.all_app_stores(), # dcc.Store(storage_type="session", id="session_store"), banner, Section( [ Columns( [ Column( [ struct_component.title_layout, html.Div( # [ # html.A( # "Documentation", # href="https://docs.crystaltoolkit.org", # )