Skip to content

Commit 2275002

Browse files
committed
Auto stash before merge of "MGComponent" and "origin/main"
1 parent 9f04e08 commit 2275002

File tree

102 files changed

+17872
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+17872
-0
lines changed

build/lib/crystal_toolkit/__init__.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import json
2+
import os as _os
3+
from collections import defaultdict
4+
from pathlib import Path
5+
6+
# pleasant hack to support MSONable objects in Dash callbacks natively
7+
from monty.json import MSONable
8+
9+
from crystal_toolkit.renderables import *
10+
11+
__version__ = "2021.04.29"
12+
13+
MODULE_PATH = Path(__file__).parents[0]
14+
15+
16+
def to_plotly_json(self):
17+
return self.as_dict()
18+
19+
20+
MSONable.to_plotly_json = to_plotly_json
21+
22+
23+
# Populate the default values from the JSON file
24+
_DEFAULTS = defaultdict(lambda: None)
25+
default_js = _os.path.join(
26+
_os.path.join(_os.path.dirname(_os.path.abspath(__file__))), "./", "defaults.json"
27+
)
28+
29+
with open(default_js) as handle:
30+
_DEFAULTS.update(json.loads(handle.read()))
31+
32+
33+
def _repr_mimebundle_(self, include=None, exclude=None):
34+
"""
35+
Render Scenes using crystaltoolkit-extension for Jupyter Lab.
36+
"""
37+
# TODO: add Plotly, application/vnd.plotly.v1+json
38+
39+
help_text_ct = """If you see this text, the Crystal Toolkit Jupyter Lab \n
40+
extension is not installed. You can install it by running \n
41+
\"pip install crystaltoolkit-extension\" \n
42+
from the same environment you run \"jupyter lab\". \n
43+
This only works in Jupyter Lab 3.x or above.\n\n
44+
"""
45+
46+
help_text_plotly = """If you see this text, the Plotly Jupyter Lab extension
47+
is not installed, please consult Plotly documentation for information on how to
48+
install.
49+
"""
50+
51+
# TODO: to be strict here, we could use inspect.signature
52+
# and .return_annotation is either a Scene or a go.Figure respectively
53+
# and also check all .parameters .kind.name have no POSITIONAL_ONLY
54+
# in practice, fairly unlikely this will cause issues without strict checking
55+
56+
if hasattr(self, "get_scene"):
57+
return {
58+
"application/vnd.mp.ctk+json": self.get_scene().to_json(),
59+
"text/plain": help_text_ct + self.__repr__(),
60+
}
61+
elif hasattr(self, "get_plot"):
62+
return {
63+
"application/vnd.plotly.v1+json": self.get_plot().to_plotly_json(),
64+
"text/plain": help_text_plotly + self.__repr__(),
65+
}
66+
else:
67+
return {"application/json": self.as_dict(), "text/plain": self.__repr__()}
68+
69+
70+
MSONable._repr_mimebundle_ = _repr_mimebundle_
71+
72+
73+
def show_json(self):
74+
from IPython.display import display_json
75+
76+
return display_json(self.as_dict(), raw=True)
77+
78+
79+
MSONable.show_json = show_json
80+
81+
82+
def _ipython_display_(self):
83+
"""
84+
Render Scenes using crystaltoolkit-extension for Jupyter Lab.
85+
86+
This function ensures that objects are also printed in string format
87+
as previously.
88+
"""
89+
from IPython.display import publish_display_data
90+
91+
publish_display_data(self._repr_mimebundle_())
92+
93+
94+
MSONable._ipython_display_ = _ipython_display_

build/lib/crystal_toolkit/apps/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
4+
body, html, .body {
5+
background: #f3f3f3 !important;
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"10.26434/chemrxiv.11294480.v1": "D. Waroquiers, J. George, M. Horton, S. Schenk, K. Persson, G.-M. Rignanese, X. Gonze, and G. Hautier, \u201cChemEnv\u202f: A Fast and Robust Coordination Environment Identification Tool,\u201d Dec. 2019.\n", "10.3389/fmats.2017.00034": "N. E. R. Zimmermann, M. K. Horton, A. Jain, and M. Haranczyk, \u201cAssessing Local Structure Motifs Using Order Parameters for Motif Recognition, Interstitial Identification, and Diffusion Path Characterization,\u201d Frontiers in Materials, vol. 4, Nov. 2017.\n"}
Binary file not shown.
Loading
Binary file not shown.

build/lib/crystal_toolkit/apps/assets/fonts/fa-brands-400.svg

+1,260
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

build/lib/crystal_toolkit/apps/assets/fonts/fa-regular-400.svg

+471
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

build/lib/crystal_toolkit/apps/assets/fonts/fa-solid-900.svg

+2,763
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.

build/lib/crystal_toolkit/apps/assets/main.ecd1960277cfe37b135f.css

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/lib/crystal_toolkit/apps/assets/task_ids_on_load.json

+1
Large diffs are not rendered by default.

build/lib/crystal_toolkit/apps/examples/GaN_bs.json

+1
Large diffs are not rendered by default.

build/lib/crystal_toolkit/apps/examples/GaN_dos.json

+1
Large diffs are not rendered by default.

build/lib/crystal_toolkit/apps/examples/Si_bs.json

+1
Large diffs are not rendered by default.

build/lib/crystal_toolkit/apps/examples/Si_dos.json

+1
Large diffs are not rendered by default.

build/lib/crystal_toolkit/apps/examples/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# standard Dash imports
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
6+
# standard Crystal Toolkit import
7+
import crystal_toolkit.components as ctc
8+
from crystal_toolkit.settings import SETTINGS
9+
from crystal_toolkit.helpers.layouts import H1, H2, Container
10+
11+
# dos and bs data from local jsons
12+
from monty.serialization import loadfn
13+
import os
14+
15+
16+
# create Dash app as normal, assets folder set for visual styles only
17+
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
18+
19+
# If callbacks created dynamically they cannot be statically checked at app startup.
20+
# For this simple example this IS a problem and,
21+
# nested layout this will need to be enabled -- consult Dash documentation
22+
# for more information.
23+
# app.config["suppress_callback_exceptions"] = True
24+
25+
path = os.path.dirname(os.path.realpath(__file__))
26+
bandstructure_symm_line = loadfn(path + "/GaN_bs.json")
27+
density_of_states = loadfn(path + "/GaN_dos.json")
28+
29+
# # create the Crystal Toolkit component
30+
bsdos_component = ctc.BandstructureAndDosComponent(
31+
bandstructure_symm_line=bandstructure_symm_line,
32+
density_of_states=density_of_states,
33+
id="bs_dos",
34+
)
35+
36+
# example layout to demonstrate capabilities of component
37+
my_layout = Container(
38+
[H1("Band Structure and Density of States Example"), bsdos_component.layout(),]
39+
)
40+
41+
# wrap your app.layout with crystal_toolkit_layout()
42+
# to ensure all necessary components are loaded into layout
43+
ctc.register_crystal_toolkit(app, layout=my_layout)
44+
45+
46+
# allow app to be run using "python structure.py"
47+
# in production, deploy behind gunicorn or similar
48+
# see Dash documentation for more information
49+
if __name__ == "__main__":
50+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# as explained in "preamble" section in documentation
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
import crystal_toolkit.components as ctc
6+
7+
app = dash.Dash()
8+
9+
# create our crystal structure using pymatgen
10+
from pymatgen.core.structure import Structure
11+
from pymatgen.core.lattice import Lattice
12+
13+
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
14+
15+
# create the Crystal Toolkit component
16+
structure_component = ctc.StructureMoleculeComponent(structure)
17+
18+
# add the component's layout to our app's layout
19+
my_layout = html.Div([structure_component.layout()])
20+
21+
# as explained in "preamble" section in documentation
22+
ctc.register_crystal_toolkit(app=app, layout=my_layout)
23+
if __name__ == "__main__":
24+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# as above
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
import crystal_toolkit.components as ctc
6+
7+
# 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
12+
from pymatgen.core.lattice import Lattice
13+
14+
app = dash.Dash()
15+
16+
# now we give a list of structures to pick from
17+
structures = [
18+
Structure(Lattice.cubic(4), ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
19+
Structure(Lattice.cubic(5), ["K", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]),
20+
]
21+
22+
# we show the first structure by default
23+
structure_component = ctc.StructureMoleculeComponent(structures[0])
24+
25+
# and we create a button for user interaction
26+
my_button = html.Button("Swap Structure", id="change_structure_button")
27+
28+
# now we have two entries in our app layout,
29+
# the structure component's layout and the button
30+
my_layout = html.Div([structure_component.layout(), my_button])
31+
32+
ctc.register_crystal_toolkit(app=app, layout=my_layout, cache=None)
33+
34+
# for the interactivity, we use a standard Dash callback
35+
@app.callback(
36+
Output(structure_component.id(), "data"),
37+
[Input("change_structure_button", "n_clicks")],
38+
)
39+
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
44+
return structures[n_clicks % 2]
45+
46+
47+
# as above
48+
if __name__ == "__main__":
49+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# standard Dash imports
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
6+
# standard Crystal Toolkit import
7+
import crystal_toolkit.components as ctc
8+
9+
# create Dash app as normal
10+
app = dash.Dash()
11+
12+
# create your layout
13+
my_layout = html.Div(["Hello scientist!"])
14+
15+
# tell Crystal Toolkit about the app and layout we want to display
16+
ctc.register_crystal_toolkit(app=app, layout=my_layout, cache=None)
17+
18+
# allow app to be run using "python app.py"
19+
# in production, deploy behind gunicorn or similar
20+
# see Dash documentation for more information
21+
if __name__ == "__main__":
22+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# standard Dash imports
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
6+
# standard Crystal Toolkit import
7+
import crystal_toolkit.components as ctc
8+
from crystal_toolkit.settings import SETTINGS
9+
from crystal_toolkit.helpers.layouts import H1, H3, Container
10+
11+
# import for this example
12+
from pymatgen.ext.matproj import MPRester
13+
from pymatgen.analysis.phase_diagram import PhaseDiagram
14+
from pymatgen.analysis.diffraction.xrd import XRDCalculator
15+
16+
# create Dash app as normal
17+
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
18+
19+
# create our crystal structure using pymatgen
20+
from pymatgen.core.structure import Structure
21+
from pymatgen.core.lattice import Lattice
22+
23+
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
24+
25+
xrd_component = ctc.XRayDiffractionComponent(initial_structure=structure)
26+
27+
# example layout to demonstrate capabilities of component
28+
my_layout = Container(
29+
[
30+
H1("XRDComponent Example"),
31+
H3("Generated from Structure object"),
32+
xrd_component.layout(),
33+
]
34+
)
35+
36+
# as explained in "preamble" section in documentation
37+
ctc.register_crystal_toolkit(app=app, layout=my_layout)
38+
39+
# allow app to be run using "python structure.py"
40+
# in production, deploy behind gunicorn or similar
41+
# see Dash documentation for more information
42+
if __name__ == "__main__":
43+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# standard Dash imports
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
from dash.dependencies import Input, Output
6+
7+
# standard Crystal Toolkit import
8+
import crystal_toolkit.components as ctc
9+
from crystal_toolkit.settings import SETTINGS
10+
from crystal_toolkit.helpers.layouts import H1, H2, Container, Button
11+
12+
# create Dash app as normal
13+
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
14+
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()
20+
21+
# 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+
)
29+
30+
# as explained in "preamble" section in documentation
31+
ctc.register_crystal_toolkit(app=app, layout=my_layout)
32+
33+
34+
@app.callback(
35+
Output(xrd_component.id(), "data"), [Input("load-xrd-button", "n_clicks")]
36+
)
37+
def load_structure(n_clicks):
38+
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
39+
return structure
40+
41+
42+
# allow app to be run using "python structure.py"
43+
# in production, deploy behind gunicorn or similar
44+
# see Dash documentation for more information
45+
if __name__ == "__main__":
46+
app.run_server(debug=True, port=8050)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# standard Dash imports
2+
import dash
3+
import dash_html_components as html
4+
import dash_core_components as dcc
5+
6+
# standard Crystal Toolkit import
7+
import crystal_toolkit.components as ctc
8+
from crystal_toolkit.settings import SETTINGS
9+
from crystal_toolkit.helpers.layouts import H1, H2, Container
10+
11+
# import for this example
12+
from pymatgen.core.structure import Structure
13+
from pymatgen.core.lattice import Lattice
14+
from pymatgen.ext.matproj import MPRester
15+
from pymatgen.analysis.phase_diagram import PhaseDiagram
16+
from pymatgen.analysis.diffraction.xrd import XRDCalculator
17+
18+
# create Dash app as normal
19+
app = dash.Dash(assets_folder=SETTINGS.ASSETS_PATH)
20+
21+
# create our crystal structure using pymatgen
22+
from pymatgen.core.structure import Structure
23+
from pymatgen.core.lattice import Lattice
24+
25+
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
26+
27+
xrd_component = ctc.XRayDiffractionComponent()
28+
29+
# example layout to demonstrate capabilities of component
30+
my_layout = Container(
31+
[H1("XRDComponent Example (Empty, No Structure Defined)"), xrd_component.layout(),]
32+
)
33+
34+
# as explained in "preamble" section in documentation
35+
ctc.register_crystal_toolkit(app=app, layout=my_layout)
36+
37+
# allow app to be run using "python structure.py"
38+
# in production, deploy behind gunicorn or similar
39+
# see Dash documentation for more information
40+
if __name__ == "__main__":
41+
app.run_server(debug=True, port=8050)

0 commit comments

Comments
 (0)