From a31b48024f2852790816cc7db59726c02949a8b0 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Wed, 16 Aug 2023 15:45:08 +0000 Subject: [PATCH] Structure download section: add extxyz and xsf formats. --- aiidalab_widgets_base/viewers.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index d998ed08d..d64017416 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -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:", ) @@ -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 @@ -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: