Skip to content

Commit

Permalink
Skip download - import directly from archive
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Feb 1, 2025
1 parent 25b9034 commit a760aab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
4 changes: 1 addition & 3 deletions examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"source": [
"# Import example calculations\n",
"\n",
"We provide here a set of example calculations performed with the AiiDAlab Quantum ESPRESSO app for you to import into your AiiDA instance. You can choose one or more examples do import at a time.\n",
"\n",
"If you have any questions or issues with the examples, please open an issue in the [aiidalab-qe-examples](https://github.com/aiidalab/aiidalab-qe-examples) repository."
"We provide here a set of example calculations performed with the AiiDAlab Quantum ESPRESSO app for you to import into your AiiDA instance. You can choose one or more examples do import at a time. If you have any questions or issues with the examples, please open an issue in the [aiidalab-qe-examples](https://github.com/aiidalab/aiidalab-qe-examples) repository."
]
},
{
Expand Down
43 changes: 16 additions & 27 deletions src/aiidalab_qe/common/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ def render(self):
(import_button, "disabled"),
lambda value: not value,
)
import_button.on_click(self.import_examples)
import_button.on_click(self.import_archives)

self.info = ipw.HTML()

Expand Down Expand Up @@ -1453,44 +1453,33 @@ def get_options(self) -> list[tuple[str, str]]:
self.info.value = "No archives found"
return []

def import_examples(self, _):
def import_archives(self, _):
if self.logger and self.clear_log_on_import:
self.logger.value = ""
for archive in self.selector.value:
if self.download_archive(archive):
self.import_archive(archive)
self.delete_archive(archive)

def download_archive(self, filename: str) -> bool:
self.info.value = self.INFO_TEMPLATE.format(f"Downloading {filename}")
file_url = f"{self.archives_url}/{filename}"
response: req.Response = req.get(file_url, stream=True)
if not response.ok:
self.info.value = f"Failed to download {filename}"
return False
with open(filename, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return True
for filename in self.selector.value:
self.import_archive(filename)

def import_archive(self, filename: str):
self.info.value = self.INFO_TEMPLATE.format(f"Importing {filename}")
file_url = f"{self.archives_url}/{filename}"
process = subprocess.Popen(
["verdi", "archive", "import", "-v", "critical", filename],
["verdi", "archive", "import", "-v", "critical", file_url],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
stdout, stderr = process.communicate()
if stderr:
self._report(filename, stdout, stderr)

def _report(self, filename: str, stdout: str, stderr: str):
if stderr and "Success" not in stdout:
self.info.value = f"Failed to import {filename}"
if self.logger:
self.logger.value += stdout
if stderr:
self.logger.value += f"\n[ERROR] {stderr}"
self.info.value += " -> see log for details"

def delete_archive(self, filename: str):
self.info.value = self.INFO_TEMPLATE.format("Cleaning up")
os.remove(filename)
self.info.value = ""
if "Success" not in stdout:
self.logger.value += "\n[ERROR]"
self.info.value = "Error -> see log for details"
else:
self.info.value = ""
self.logger.value += f"\n{stderr}"

0 comments on commit a760aab

Please sign in to comment.