From 5f93f4bd34451b896c4cde8f257250a833962029 Mon Sep 17 00:00:00 2001 From: AndresOrtegaGuerrero Date: Sun, 5 Jan 2025 17:19:06 +0000 Subject: [PATCH 1/4] adding a download image button for different formats --- .../common/bands_pdos/bandpdoswidget.py | 16 +++++++- src/aiidalab_qe/common/bands_pdos/model.py | 41 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py index 123548387..d724eb3e7 100644 --- a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py +++ b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py @@ -120,6 +120,20 @@ def render(self): ) self.download_button.on_click(self._model.download_data) + self.download_image = ipw.Button( + description="Download Image", button_style="primary" + ) + self.download_image.on_click(self._model.download_image) + self.image_format = ipw.Dropdown( + description="Format:", + disabled=False, + ) + ipw.dlink((self._model, "image_format_options"), (self.image_format, "options")) + ipw.link((self._model, "image_format"), (self.image_format, "value")) + + self.download_buttons = ipw.HBox( + children=[self.download_button, self.download_image, self.image_format] + ) self.project_bands_box = ipw.Checkbox( description="Add `fat bands` projections", style={"description_width": "initial"}, @@ -240,7 +254,7 @@ def render(self): """), self.pdos_options, - self.download_button, + self.download_buttons, self.legend_interaction_description, ] diff --git a/src/aiidalab_qe/common/bands_pdos/model.py b/src/aiidalab_qe/common/bands_pdos/model.py index c9f2b72e2..cc02dc846 100644 --- a/src/aiidalab_qe/common/bands_pdos/model.py +++ b/src/aiidalab_qe/common/bands_pdos/model.py @@ -65,6 +65,12 @@ class BandsPdosModel(Model): bands_data = {} bands_projections_data = {} + # Image format options + image_format_options = tl.List( + trait=tl.Unicode(), default_value=["png", "jpeg", "svg", "pdf"] + ) + image_format = tl.Unicode("png") + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -284,6 +290,41 @@ def update_trace_color(self, color): # Update the color picker to match the updated trace self.color_picker = rgba_to_hex(self.plot.data[self.trace].line.color) + def download_image(self, _=None): + """ + Downloads the current plot as an image in the format specified by self.image_format. + """ + # Define the filename + if self.bands and self.pdos: + filename = f"bands_pdos.{self.image_format}" + else: + filename = f"{'bands' if self.bands else 'pdos'}.{self.image_format}" + + # Generate the image in the specified format + image_payload = self.plot.to_image(format=self.image_format) + image_payload_base64 = base64.b64encode(image_payload).decode("utf-8") + + self._download_image(payload=image_payload_base64, filename=filename) + + @staticmethod + def _download_image(payload, filename): + from IPython.display import Javascript + + # Safely format the JavaScript code + javas = Javascript( + """ + var link = document.createElement('a'); + link.href = 'data:image/{format};base64,{payload}'; + link.download = "{filename}"; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + """.format( + payload=payload, filename=filename, format=filename.split(".")[-1] + ) + ) + display(javas) + def download_data(self, _=None): """Function to download the data.""" if self.bands_data: From d01d44eee695441db460c55af04738fd8b7d9a77 Mon Sep 17 00:00:00 2001 From: AndresOrtegaGuerrero Date: Sun, 5 Jan 2025 17:21:39 +0000 Subject: [PATCH 2/4] adding an icon to the new button --- src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py index d724eb3e7..090708a05 100644 --- a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py +++ b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py @@ -121,7 +121,9 @@ def render(self): self.download_button.on_click(self._model.download_data) self.download_image = ipw.Button( - description="Download Image", button_style="primary" + description="Download Image", + button_style="primary", + icon="fa-image", ) self.download_image.on_click(self._model.download_image) self.image_format = ipw.Dropdown( From 910e3be998f21f01c6d1b41069985364218127ce Mon Sep 17 00:00:00 2001 From: AndresOrtegaGuerrero Date: Sun, 5 Jan 2025 17:41:22 +0000 Subject: [PATCH 3/4] removing capital letter in description --- src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py index 090708a05..a975abb9a 100644 --- a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py +++ b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py @@ -121,7 +121,7 @@ def render(self): self.download_button.on_click(self._model.download_data) self.download_image = ipw.Button( - description="Download Image", + description="Download image", button_style="primary", icon="fa-image", ) From ed3c35fb6dc04011460c6384f844ea8ba23b57ca Mon Sep 17 00:00:00 2001 From: AndresOrtegaGuerrero Date: Mon, 6 Jan 2025 08:22:50 +0000 Subject: [PATCH 4/4] removing disable and adding layout --- src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py index a975abb9a..3683a44e5 100644 --- a/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py +++ b/src/aiidalab_qe/common/bands_pdos/bandpdoswidget.py @@ -128,7 +128,7 @@ def render(self): self.download_image.on_click(self._model.download_image) self.image_format = ipw.Dropdown( description="Format:", - disabled=False, + layout=ipw.Layout(width="auto"), ) ipw.dlink((self._model, "image_format_options"), (self.image_format, "options")) ipw.link((self._model, "image_format"), (self.image_format, "value"))