Skip to content

Commit

Permalink
Structure download section: add extxyz and xsf formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
yakutovicha committed Aug 16, 2023
1 parent 9a1ed90 commit a31b480
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,14 @@ def _download_tab(self):

# 1. Choose download file format.
self.file_format = ipw.Dropdown(
options=["xyz", "cif"],
label="Extended xyz",
# File extension and format may be differen. Therefore, we define both.
options=(
("xyz", {"extension": "xyz", "format": "xyz"}),
("cif", {"extension": "cif", "format": "cif"}),
("Extended xyz", {"extension": "xyz", "format": "extxyz"}),
("xsf", {"extension": "xsf", "format": "xsf"}),
),
layout={"width": "200px"},
description="File format:",
)
Expand Down Expand Up @@ -710,10 +717,13 @@ def apply_displayed_selection(self, _=None):

def download(self, change=None): # pylint: disable=unused-argument
"""Prepare a structure for downloading."""
suffix = f"-pk-{self.pk}" if self.pk else ""
payload = self._prepare_payload()
if payload is None:
return
suffix = f"pk-{self.pk}" if self.pk else "not-stored"
self._download(
payload=self._prepare_payload(),
filename=f"structure{suffix}.{self.file_format.value}",
filename=f"""structure-{suffix}.{self.file_format.value["extension"]}""",
)

@staticmethod
Expand All @@ -737,7 +747,10 @@ def _prepare_payload(self, file_format=None):
"""Prepare binary information."""
from tempfile import NamedTemporaryFile

file_format = file_format if file_format else self.file_format.value
if not self.structure:
return None

file_format = file_format if file_format else self.file_format.value["format"]
tmp = NamedTemporaryFile()
self.structure.write(tmp.name, format=file_format) # pylint: disable=no-member
with open(tmp.name, "rb") as raw:
Expand Down

0 comments on commit a31b480

Please sign in to comment.