Skip to content

Commit f6aaa11

Browse files
committed
Improve Structure download (add Symmetrized CIF)
1 parent 8d8ec8c commit f6aaa11

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

crystal_toolkit/components/structure.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pymatgen.core.periodic_table import DummySpecie
2222
from pymatgen.core.structure import Molecule, Structure
2323
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
24+
from emmet.core.settings import EmmetSettings
2425

2526
from crystal_toolkit.core.legend import Legend
2627
from crystal_toolkit.core.mpcomponent import MPComponent
@@ -69,6 +70,18 @@ class StructureMoleculeComponent(MPComponent):
6970
# what to show for the title_layout if structure/molecule not loaded
7071
default_title = "Crystal Toolkit"
7172

73+
# human-readable label to file extension
74+
# downloading Molecules has not yet been added
75+
download_options = {
76+
"Structure": {
77+
"CIF (Symmetrized)": {"fmt": "cif", "symprec": EmmetSettings().SYMPREC},
78+
"CIF": {"fmt": "cif"},
79+
"POSCAR": {"fmt": "poscar"},
80+
"JSON": {"fmt": "json"},
81+
"Prismatic": {"fmt": "prismatic"},
82+
}
83+
}
84+
7285
def __init__(
7386
self,
7487
struct_or_mol: Optional[
@@ -440,20 +453,27 @@ def download_image(image_data_timestamp, image_data, image_request):
440453
Output(self.id("download-structure"), "data"),
441454
Input(self.id("download-button"), "n_clicks"),
442455
[
443-
State(self.get_kwarg_id("download_fmt"), "value"),
456+
State(self.get_kwarg_id("download_option"), "value"),
444457
State(self.id(), "data"),
445458
],
446459
)
447-
def download_image(n_clicks, fmt, data):
460+
def download_structure(n_clicks, download_option, data):
448461

449462
if not n_clicks:
450463
raise PreventUpdate
451464

452465
structure = self.from_data(data)
453-
fmt = fmt[0]
466+
if isinstance(structure, StructureGraph):
467+
structure = structure.structure
468+
469+
download_option = download_option[0]
470+
extension = self.download_options["Structure"][download_option][
471+
"download_options"
472+
]
473+
options = self.download_options["Structure"][download_option]
454474

455475
try:
456-
contents = structure.to(fmt=fmt)
476+
contents = structure.to(**options)
457477
except Exception as exc:
458478
# don't fail silently, tell user what went wrong
459479
contents = exc
@@ -464,7 +484,7 @@ def download_image(n_clicks, fmt, data):
464484
"content": base64,
465485
"base64": True,
466486
"type": "text/plain",
467-
"filename": f"{structure.composition.reduced_formula}.{fmt}",
487+
"filename": f"{structure.composition.reduced_formula}.{extension}",
468488
}
469489

470490
@app.callback(
@@ -832,23 +852,18 @@ def _sub_layouts(self):
832852
]
833853
)
834854

835-
# human-readable label to file extension
836-
struct_options = {
837-
"CIF": "cif",
838-
"POSCAR": "poscar",
839-
"JSON": "json",
840-
"Prismatic": "prismatic",
841-
}
842-
843-
state = {"fmt": "cif"}
855+
state = {"fmt": "CIF (Symmetrized)"}
844856

845857
download_options = self.get_choice_input(
846-
kwarg_label="download_fmt",
858+
kwarg_label="download_option",
847859
state=state,
848-
options=[{"label": k, "value": v} for k, v in struct_options.items()],
860+
options=[
861+
{"label": k, "value": k}
862+
for k, v in self.download_options["Structure"].items()
863+
],
849864
style={
850865
"border-radius": "4px 0px 0px 4px",
851-
"width": "10rem",
866+
"width": "12rem",
852867
"height": "1.5rem",
853868
},
854869
)

0 commit comments

Comments
 (0)