Skip to content

Commit d5cd41b

Browse files
authored
Merge pull request #266 from janosh/small-example-app-fixes
Small example app fixes
2 parents 2ad33cc + 6b47c55 commit d5cd41b

File tree

11 files changed

+115
-126
lines changed

11 files changed

+115
-126
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
repos:
22
- repo: https://github.com/ambv/black
3-
rev: stable
3+
rev: 22.6.0
44
hooks:
55
- id: black

crystal_toolkit/apps/examples/basic_hello_structure.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# as explained in "preamble" section in documentation
22
import dash
33
from dash import html
4-
from dash import dcc
4+
from pymatgen.core.lattice import Lattice
5+
from pymatgen.core.structure import Structure
6+
57
import crystal_toolkit.components as ctc
68

79
# app = dash.Dash(external_stylesheets=['https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css'])
810
app = dash.Dash()
911

1012
# create our crystal structure using pymatgen
11-
from pymatgen.core.structure import Structure
12-
from pymatgen.core.lattice import Lattice
13-
1413
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
1514

1615
# create the Crystal Toolkit component

crystal_toolkit/apps/examples/basic_hello_structure_interactive.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# as above
22
import dash
33
from dash import html
4-
from dash import dcc
5-
import crystal_toolkit.components as ctc
64

75
# standard Dash imports for callbacks (interactivity)
8-
from dash.dependencies import Input, Output, State
9-
from dash.exceptions import PreventUpdate
10-
11-
from pymatgen.core.structure import Structure
6+
from dash.dependencies import Input, Output
127
from pymatgen.core.lattice import Lattice
8+
from pymatgen.core.structure import Structure
139

14-
app = dash.Dash()
10+
import crystal_toolkit.components as ctc
11+
12+
app = dash.Dash(
13+
prevent_initial_callbacks=True, # don't run callbacks on page load
14+
)
1515

1616
# now we give a list of structures to pick from
1717
structures = [
18-
Structure(Lattice.cubic(4), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
18+
Structure(Lattice.hexagonal(5, 3), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
1919
Structure(Lattice.cubic(5), ["K", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
2020
]
2121

@@ -31,16 +31,13 @@
3131

3232
ctc.register_crystal_toolkit(app=app, layout=my_layout)
3333

34+
3435
# for the interactivity, we use a standard Dash callback
3536
@app.callback(
3637
Output(structure_component.id(), "data"),
3738
[Input("change_structure_button", "n_clicks")],
3839
)
3940
def update_structure(n_clicks):
40-
# on load, n_clicks will be None, and no update is required
41-
# after clicking on the button, n_clicks will be an int and incremented
42-
if not n_clicks:
43-
raise PreventUpdate
4441
return structures[n_clicks % 2]
4542

4643

crystal_toolkit/apps/examples/diffraction_dynamic.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
# standard Dash imports
22
import dash
3-
from dash import html
4-
from dash import dcc
53
from dash.dependencies import Input, Output
64

5+
# create our crystal structure using pymatgen
6+
from pymatgen.core.lattice import Lattice
7+
from pymatgen.core.structure import Structure
8+
79
# standard Crystal Toolkit import
810
import crystal_toolkit.components as ctc
11+
from crystal_toolkit.helpers.layouts import H1, Button, Container
912
from crystal_toolkit.settings import SETTINGS
10-
from crystal_toolkit.helpers.layouts import H1, H2, Container, Button
1113

1214
# create Dash app as normal
1315
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
1416

15-
# create our crystal structure using pymatgen
16-
from pymatgen.core.structure import Structure
17-
from pymatgen.core.lattice import Lattice
18-
19-
xrd_component = ctc.XRayDiffractionComponent()
17+
xrd_component = ctc.XRayDiffractionComponent(id="xrd-diffraction")
2018

2119
# example layout to demonstrate capabilities of component
22-
my_layout = Container(
23-
[
24-
H1("XRDComponent Example (Structure Added After Loading)"),
25-
xrd_component.layout(),
26-
Button("Load XRD", id="load-xrd-button"),
27-
]
28-
)
20+
page_title = H1("XRDComponent Example (Structure Added After Loading)")
21+
load_btn = Button("Load XRD", id="load-xrd-button")
22+
my_layout = Container([page_title, xrd_component.layout(), load_btn])
2923

3024
# as explained in "preamble" section in documentation
3125
ctc.register_crystal_toolkit(app=app, layout=my_layout)
3226

3327

34-
@app.callback(
35-
Output(xrd_component.id(), "data"), [Input("load-xrd-button", "n_clicks")]
36-
)
37-
def load_structure(n_clicks):
28+
@app.callback(Output(xrd_component.id(), "data"), Input(load_btn.id, "n_clicks"))
29+
def load_structure(n_clicks: int) -> Structure:
3830
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
3931
return structure
4032

crystal_toolkit/apps/examples/structure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# standard Dash imports
22
import dash
3-
from dash import dcc
43
from dash import html
54

65
# import for this example
7-
from pymatgen.core.structure import Structure
86
from pymatgen.core.lattice import Lattice
7+
from pymatgen.core.structure import Structure
98

109
# standard Crystal Toolkit import
1110
import crystal_toolkit.components as ctc
11+
from crystal_toolkit.settings import SETTINGS
1212

1313
# create Dash app as normal
14-
app = dash.Dash()
14+
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
1515

1616
# create the Structure object
1717
structure = Structure(

crystal_toolkit/apps/main.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import os
3-
from ast import literal_eval
3+
import warnings
44
from random import choice
55
from time import time
66
from typing import Optional
@@ -9,17 +9,28 @@
99

1010
import dash
1111
import sentry_sdk
12+
from dash import dcc, html
1213
from dash.dependencies import Input, Output, State
1314
from dash.exceptions import PreventUpdate
1415
from flask_caching import Cache
1516
from monty.serialization import loadfn
1617
from pymatgen.core import __version__ as pmg_version
17-
from pymatgen.ext.matproj import MPRester
18+
from pymatgen.ext.matproj import MPRester, MPRestError
1819

1920
import crystal_toolkit.components as ctc
2021
from crystal_toolkit import __file__ as module_path
2122
from crystal_toolkit.core.mpcomponent import MPComponent
22-
from crystal_toolkit.helpers.layouts import *
23+
from crystal_toolkit.helpers.layouts import (
24+
Box,
25+
Column,
26+
Columns,
27+
Container,
28+
Loading,
29+
MessageBody,
30+
MessageContainer,
31+
MessageHeader,
32+
Reveal,
33+
)
2334
from crystal_toolkit.settings import SETTINGS
2435

2536
# choose a default structure on load
@@ -196,17 +207,17 @@
196207
mp_panels = []
197208

198209
mp_section = (
199-
H3("Materials Project"),
210+
html.H3("Materials Project"),
200211
html.Div([panel.panel_layout() for panel in mp_panels], id="mp_panels"),
201212
)
202213

203214

204215
body_layout = [
205216
html.Br(),
206-
H3("Transform"),
217+
html.H3("Transform"),
207218
html.Div([transformation_component.layout()]),
208219
html.Br(),
209-
H3("Analyze"),
220+
html.H3("Analyze"),
210221
html.Div([panel.panel_layout() for panel in panels], id="panels"),
211222
# html.Br(),
212223
# *mp_section,
@@ -270,7 +281,7 @@
270281
################################################################################
271282

272283

273-
footer = Footer(
284+
footer = html.Footer(
274285
html.Div(
275286
[
276287
dcc.Markdown(
@@ -317,7 +328,7 @@
317328
[
318329
dcc.Location(id="url", refresh=False),
319330
banner,
320-
Section(
331+
html.Section(
321332
[
322333
Columns(
323334
[
@@ -398,7 +409,7 @@
398409
Columns([Column(body_layout)]),
399410
]
400411
),
401-
Section(footer),
412+
html.Section(footer),
402413
]
403414
)
404415

@@ -524,7 +535,7 @@ def master_update_structure(search_mpid: Optional[str], upload_data: Optional[st
524535
try:
525536
struct = mpr.get_task_data(search_mpid, "structure")[0]["structure"]
526537
print("Struct from task.")
527-
except:
538+
except MPRestError:
528539
struct = mpr.get_structure_by_material_id(search_mpid)
529540
print("Struct from material.")
530541
else:
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
1-
from crystal_toolkit.core.mpcomponent import MPComponent
2-
3-
register_app = MPComponent.register_app
4-
register_cache = MPComponent.register_cache
5-
register_crystal_toolkit = MPComponent.register_crystal_toolkit
6-
crystal_toolkit_layout = MPComponent.crystal_toolkit_layout
7-
8-
from crystal_toolkit.components.search import SearchComponent
9-
from crystal_toolkit.components.structure import StructureMoleculeComponent
10-
11-
from crystal_toolkit.components.robocrys import RobocrysComponent
12-
13-
from crystal_toolkit.components.diffraction import (
14-
XRayDiffractionComponent,
15-
TEMDiffractionComponent,
16-
)
17-
18-
# from crystal_toolkit.components.xas import XASComponent, XASPanelComponent
19-
20-
# from crystal_toolkit.components.submit_snl import SubmitSNLPanel
21-
from crystal_toolkit.components.symmetry import SymmetryPanel
22-
from crystal_toolkit.components.upload import StructureMoleculeUploadComponent
23-
241
from crystal_toolkit.components.bandstructure import (
252
BandstructureAndDosComponent,
263
BandstructureAndDosPanelComponent,
274
)
5+
from crystal_toolkit.components.diffraction import (
6+
TEMDiffractionComponent,
7+
XRayDiffractionComponent,
8+
)
9+
from crystal_toolkit.components.localenv import LocalEnvironmentPanel
2810

2911
# from crystal_toolkit.components.phase_diagram import (
3012
# PhaseDiagramComponent,
3113
# PhaseDiagramPanelComponent,
3214
# )
3315
from crystal_toolkit.components.pourbaix import PourbaixDiagramComponent
34-
from crystal_toolkit.components.localenv import LocalEnvironmentPanel
16+
from crystal_toolkit.components.robocrys import RobocrysComponent
17+
from crystal_toolkit.components.search import SearchComponent
18+
from crystal_toolkit.components.structure import StructureMoleculeComponent
3519

36-
from crystal_toolkit.components.transformations.core import AllTransformationsComponent
20+
# from crystal_toolkit.components.submit_snl import SubmitSNLPanel
21+
from crystal_toolkit.components.symmetry import SymmetryPanel
3722
from crystal_toolkit.components.transformations.autooxistatedecoration import (
3823
AutoOxiStateDecorationTransformationComponent,
3924
)
25+
from crystal_toolkit.components.transformations.core import AllTransformationsComponent
4026

4127
# from crystal_toolkit.components.transformations.cubic import (
4228
# CubicSupercellTransformationComponent,
@@ -51,7 +37,18 @@
5137
from crystal_toolkit.components.transformations.supercell import (
5238
SupercellTransformationComponent,
5339
)
40+
from crystal_toolkit.components.upload import StructureMoleculeUploadComponent
41+
from crystal_toolkit.core.mpcomponent import MPComponent
42+
43+
# from crystal_toolkit.components.xas import XASComponent, XASPanelComponent
44+
5445

5546
# from crystal_toolkit.components.transformations.rattle import (
5647
# MonteCarloRattleTransformationComponent,
5748
# )
49+
50+
51+
register_app = MPComponent.register_app
52+
register_cache = MPComponent.register_cache
53+
register_crystal_toolkit = MPComponent.register_crystal_toolkit
54+
crystal_toolkit_layout = MPComponent.crystal_toolkit_layout

crystal_toolkit/components/phase_diagram.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
from typing import Optional
2-
31
import dash
4-
from dash import dcc
5-
from dash import html
62
import dash_table
73
import plotly.graph_objs as go
4+
from dash import dcc, html
85
from dash.dependencies import Input, Output, State
96
from dash.exceptions import PreventUpdate
10-
from pymatgen.ext.matproj import MPRester
117
from pymatgen.analysis.phase_diagram import PDEntry, PDPlotter, PhaseDiagram
128
from pymatgen.core.composition import Composition
9+
from pymatgen.ext.matproj import MPRester
1310

1411
from crystal_toolkit.core.mpcomponent import MPComponent
1512
from crystal_toolkit.core.panelcomponent import PanelComponent
@@ -758,7 +755,7 @@ def title(self):
758755
def description(self):
759756
return (
760757
"Display the compositional phase diagram for the"
761-
" chemical system containing this structure (between 24 species)."
758+
" chemical system containing this structure (between 2-4 species)."
762759
)
763760

764761
def update_contents(self, new_store_contents, *args):

0 commit comments

Comments
 (0)