Skip to content

Commit

Permalink
json uploader for verification
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Mar 30, 2023
1 parent 75105e4 commit aa93d90
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions aiidalab_sssp/inspect/subwidgets/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PeriodicTable(ipw.VBox):
"""Wrapper-widget for PTableWidget, select the element and update the dict of pseudos"""

# (output) dict of pseudos for selected element
pseudos = traitlets.Dict()
pseudos = traitlets.Dict(allow_none=True)

def __init__(self, cache_folder, **kwargs):
self._disabled = kwargs.get("disabled", False)
Expand All @@ -42,6 +42,9 @@ def __init__(self, cache_folder, **kwargs):
self.ptable = PTableWidget(states=1, selected_colors=["green"], **kwargs)
self._last_selected = None
self.ptable.observe(self._on_element_select)
self._element = None # selected element, for record the last selected element

self.elements = set() # elements that have json file in the db folder

# if cache empty run update: first time
self.db_version = None
Expand All @@ -59,6 +62,11 @@ def __init__(self, cache_folder, **kwargs):
)
db_update.on_click(self._update_db)

self.json_upload = ipw.FileUpload(
accept=".json", multiple=False, description="Upload json file"
)
self.json_upload.observe(self._on_json_upload, names="value")

super().__init__(
children=(
self.ptable,
Expand All @@ -68,10 +76,35 @@ def __init__(self, cache_folder, **kwargs):
ipw.HTML(f"The SSSP Database version: {self.db_version}"),
]
),
self.json_upload,
),
layout=kwargs.get("layout", {}),
)

def _on_json_upload(self, change):
if change["name"] == "value" and change["type"] == "change":
if change["new"]:
# get the first file
file_name = list(change["new"].keys())[0]
content = change["new"][file_name]["content"]
pseudos = json.loads(content.decode("utf-8"))

# check if the pseudos are valid by check the element name from the key of pseudos
for key in pseudos:
if self._element is not None and self._element not in key:
raise ValueError(
f"The element name in the json file is not {self._element}, please check the json file."
)

if self.pseudos is None or len(self.pseudos) == 0:
self.pseudos = pseudos
else:
self.pseudos = self.pseudos.update(pseudos)

# reset the upload widget to empty so that the same file can be uploaded again
# self.json_upload.value = {}
self.json_upload._counter = 0

def _on_element_select(self, event):
if event["name"] == "selected_elements" and event["type"] == "change":
if tuple(event["new"].keys()) == ("Du",):
Expand All @@ -84,17 +117,17 @@ def _on_element_select(self, event):
# If this is empty it's ok, unselect all
# If there is more than one, that's weird... to avoid problems, anyway, I pick one of the two
if newly_selected:
element = list(newly_selected)[0]
self.ptable.selected_elements = {element: 0}
self.update_pseudos(element)
self._element = list(newly_selected)[0]
self.ptable.selected_elements = {self._element: 0}
self.update_pseudos(self._element)
else:
self.reset()
# To have the correct 'last' value for next calls
self._last_selected = self.ptable.selected_elements
else:
# first time set: len(event['new']) -> 1
element = list(event["new"])[0]
self.update_pseudos(element)
self._element = list(event["new"])[0]
self.update_pseudos(self._element)

def update_pseudos(self, element):
self.pseudos = _load_pseudos(element)
Expand All @@ -116,10 +149,10 @@ def _update_db(self, _=None, download=True):

@staticmethod
def _get_enabled_elements(cache_folder):
elements = []
elements = set()
for fn in os.listdir(os.path.join(cache_folder, _DB_FOLDER)):
if "band" not in fn:
elements.append(fn.split(".")[0])
elements.add(fn.split(".")[0])

return elements

Expand Down

0 comments on commit aa93d90

Please sign in to comment.