-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtransformations.py
80 lines (66 loc) · 2.35 KB
/
transformations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations
import dash
from dash import html
from dash.dependencies import Input, Output
from dash_mp_components import JsonView
from pymatgen.core import Lattice, Structure
from pymatgen.ext.matproj import MPRester
import crystal_toolkit.components as ctc
from crystal_toolkit.settings import SETTINGS
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
# create the Structure object
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
# create an input structure as an example
structure_component = ctc.StructureMoleculeComponent(
MPRester().get_structure_by_material_id("mp-804"), id="structure_in"
)
# and a way to view the transformed structure
structure_component_transformed = ctc.StructureMoleculeComponent(
MPRester().get_structure_by_material_id("mp-804"), id="structure_out"
)
# and the transformation component itself
transformation_component = ctc.AllTransformationsComponent(
input_structure_component=structure_component,
)
# example layout to demonstrate capabilities of component
layout = html.Div(
[
html.H1("TransformationComponent Example"),
html.H2("Standard Layout"),
transformation_component.layout(),
html.H3("Example Input Structure"),
structure_component.layout(size="500px"),
html.H3("Example Transformed Structure"),
structure_component_transformed.layout(size="500px"),
html.H3("JSON View of Transformations"),
JsonView(id="json"),
]
)
# tell crystal toolkit about your app and layout
ctc.register_crystal_toolkit(app, layout=layout)
# this is here for to see the JSON representation of
# the transformations when running the example app,
# it is not necessary for running the component
app.clientside_callback(
"""
function (...args) {
return {"transformations": args}
}
""",
Output("json", "src"),
[
Input(trafo.id(), "data")
for trafo in transformation_component.transformations.values()
],
)
@app.callback(
Output(structure_component_transformed.id(), "data"),
Input(transformation_component.id(), "data"),
)
def update_structure(struct):
return struct
# run this app with "python path/to/this/file.py"
# in production, deploy behind gunicorn or similar
# see Dash docs for more info
if __name__ == "__main__":
app.run(debug=True, port=8050)