21
21
from pymatgen .core .periodic_table import DummySpecie
22
22
from pymatgen .core .structure import Molecule , Structure
23
23
from pymatgen .symmetry .analyzer import SpacegroupAnalyzer
24
+ from emmet .core .settings import EmmetSettings
24
25
25
26
from crystal_toolkit .core .legend import Legend
26
27
from crystal_toolkit .core .mpcomponent import MPComponent
@@ -69,6 +70,18 @@ class StructureMoleculeComponent(MPComponent):
69
70
# what to show for the title_layout if structure/molecule not loaded
70
71
default_title = "Crystal Toolkit"
71
72
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
+
72
85
def __init__ (
73
86
self ,
74
87
struct_or_mol : Optional [
@@ -440,20 +453,27 @@ def download_image(image_data_timestamp, image_data, image_request):
440
453
Output (self .id ("download-structure" ), "data" ),
441
454
Input (self .id ("download-button" ), "n_clicks" ),
442
455
[
443
- State (self .get_kwarg_id ("download_fmt " ), "value" ),
456
+ State (self .get_kwarg_id ("download_option " ), "value" ),
444
457
State (self .id (), "data" ),
445
458
],
446
459
)
447
- def download_image (n_clicks , fmt , data ):
460
+ def download_structure (n_clicks , download_option , data ):
448
461
449
462
if not n_clicks :
450
463
raise PreventUpdate
451
464
452
465
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 ]
454
474
455
475
try :
456
- contents = structure .to (fmt = fmt )
476
+ contents = structure .to (** options )
457
477
except Exception as exc :
458
478
# don't fail silently, tell user what went wrong
459
479
contents = exc
@@ -464,7 +484,7 @@ def download_image(n_clicks, fmt, data):
464
484
"content" : base64 ,
465
485
"base64" : True ,
466
486
"type" : "text/plain" ,
467
- "filename" : f"{ structure .composition .reduced_formula } .{ fmt } " ,
487
+ "filename" : f"{ structure .composition .reduced_formula } .{ extension } " ,
468
488
}
469
489
470
490
@app .callback (
@@ -832,23 +852,18 @@ def _sub_layouts(self):
832
852
]
833
853
)
834
854
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)" }
844
856
845
857
download_options = self .get_choice_input (
846
- kwarg_label = "download_fmt " ,
858
+ kwarg_label = "download_option " ,
847
859
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
+ ],
849
864
style = {
850
865
"border-radius" : "4px 0px 0px 4px" ,
851
- "width" : "10rem " ,
866
+ "width" : "12rem " ,
852
867
"height" : "1.5rem" ,
853
868
},
854
869
)
0 commit comments