-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcs.py
More file actions
executable file
·45 lines (32 loc) · 1.34 KB
/
cs.py
File metadata and controls
executable file
·45 lines (32 loc) · 1.34 KB
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
#!/usr/bin/env python3
import dash
from dash import html
# standard Dash imports for callbacks (interactivity)
from dash.dependencies import Input, Output
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure
import crystal_toolkit.components as ctc
# don't run callbacks on page load
app = dash.Dash(prevent_initial_callbacks=True)
# now we give a list of structures to pick from
structures = [
Structure(Lattice.hexagonal(5, 3), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
Structure(Lattice.cubic(5), ["K", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
]
# we show the first structure by default
structure_component = ctc.StructureMoleculeComponent(structures[0], id="my_structure")
# and we create a button for user interaction
my_button = html.Button("Swap Structure", id="change_structure_button")
# now we have two entries in our app layout,
# the structure component's layout and the button
my_layout = html.Div([structure_component.layout(), my_button])
ctc.register_crystal_toolkit(app=app, layout=my_layout)
# for the interactivity, we use a standard Dash callback
@app.callback(
Output(structure_component.id(), "data"),
[Input("change_structure_button", "n_clicks")],
)
def update_structure(n_clicks):
return structures[n_clicks % 2]
if __name__ == "__main__":
app.run_server(debug=True, port=8050)