Ramlaoui commited on
Commit
1366f26
1 Parent(s): de410eb

Search with Periodic Table

Browse files
Files changed (2) hide show
  1. app.py +47 -35
  2. requirements.txt +1 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
 
3
  import crystal_toolkit.components as ctc
4
  import dash
 
5
  from crystal_toolkit.settings import SETTINGS
6
  from dash import dcc, html
7
  from dash.dependencies import Input, Output, State
@@ -39,6 +40,7 @@ dataset = load_dataset(
39
  ],
40
  )
41
 
 
42
  # Convert the train split to a pandas DataFrame
43
  train_df = dataset.to_pandas()
44
  del dataset
@@ -50,37 +52,28 @@ server = app.server # Expose the server for deployment
50
  # Define the app layout
51
  layout = html.Div(
52
  [
53
- dcc.Markdown("## Interactive Crystal Viewer"),
54
  html.Div(
55
  [
56
  html.Div(
57
  [
58
- html.Label("Search by Chemical System (e.g., 'Ac-Cd-Ge')"),
59
- dcc.Input(
60
- id="query-input",
61
- type="text",
62
- value="Ac-Cd-Ge",
63
- placeholder="Ac-Cd-Ge",
64
- style={"width": "100%"},
 
 
65
  ),
66
  ],
67
  style={
68
- "width": "70%",
69
  "display": "inline-block",
70
  "verticalAlign": "top",
71
  },
72
  ),
73
- html.Div(
74
- [
75
- html.Button("Search", id="search-button", n_clicks=0),
76
- ],
77
- style={
78
- "width": "28%",
79
- "display": "inline-block",
80
- "paddingLeft": "2%",
81
- "verticalAlign": "top",
82
- },
83
- ),
84
  ],
85
  style={"margin-bottom": "20px"},
86
  ),
@@ -118,13 +111,32 @@ layout = html.Div(
118
  ],
119
  style={"margin-top": "20px"},
120
  ),
121
- ]
 
 
 
 
 
 
 
 
 
 
 
 
122
  )
 
 
 
 
 
 
 
123
 
124
 
125
  # Function to search for materials
126
  def search_materials(query):
127
- element_list = [el.strip() for el in query.split("-")]
128
  isubset = lambda x: set(x).issubset(element_list)
129
  isintersection = lambda x: len(set(x).intersection(element_list)) > 0
130
  entries_df = train_df[
@@ -142,19 +154,19 @@ def search_materials(query):
142
  return options
143
 
144
 
145
- # Callback to update the material dropdown based on search
146
- @app.callback(
147
- [Output("material-dropdown", "options"), Output("material-dropdown", "value")],
148
- Input("search-button", "n_clicks"),
149
- State("query-input", "value"),
150
- )
151
- def update_material_dropdown(n_clicks, query):
152
- if n_clicks is None or not query:
153
- return [], None
154
- options = search_materials(query)
155
- if not options:
156
- return [], None
157
- return options, options[0]["value"]
158
 
159
 
160
  # Callback to display the selected material
 
2
 
3
  import crystal_toolkit.components as ctc
4
  import dash
5
+ import dash_mp_components as dmp
6
  from crystal_toolkit.settings import SETTINGS
7
  from dash import dcc, html
8
  from dash.dependencies import Input, Output, State
 
40
  ],
41
  )
42
 
43
+ dataset = dataset.select(range(1000))
44
  # Convert the train split to a pandas DataFrame
45
  train_df = dataset.to_pandas()
46
  del dataset
 
52
  # Define the app layout
53
  layout = html.Div(
54
  [
55
+ html.H1("Interactive Crystal Viewer"),
56
  html.Div(
57
  [
58
  html.Div(
59
  [
60
+ html.H3("Search for materials by elements (eg. 'Ac,Cd,Ge')"),
61
+ dmp.MaterialsInput(
62
+ allowedInputTypes=["elements"],
63
+ hidePeriodicTable=False,
64
+ periodicTableMode="toggle",
65
+ showSubmitButton=True,
66
+ submitButtonText="Submit",
67
+ type="elements",
68
+ id="materials-input",
69
  ),
70
  ],
71
  style={
72
+ "width": "100%",
73
  "display": "inline-block",
74
  "verticalAlign": "top",
75
  },
76
  ),
 
 
 
 
 
 
 
 
 
 
 
77
  ],
78
  style={"margin-bottom": "20px"},
79
  ),
 
111
  ],
112
  style={"margin-top": "20px"},
113
  ),
114
+ ],
115
+ style={
116
+ "margin-left": "10px",
117
+ "margin-right": "10px",
118
+ },
119
+ )
120
+
121
+
122
+ @app.callback(
123
+ Output("material-dropdown", "options"),
124
+ Output("material-dropdown", "value"),
125
+ Input("materials-input", "submitButtonClicks"),
126
+ Input("materials-input", "value"),
127
  )
128
+ def on_submit_materials_input(n_clicks, query):
129
+ if n_clicks is None or not query:
130
+ return [], None
131
+ options = search_materials(query)
132
+ if not options:
133
+ return [], None
134
+ return options, options[0]["value"]
135
 
136
 
137
  # Function to search for materials
138
  def search_materials(query):
139
+ element_list = [el.strip() for el in query.split(",")]
140
  isubset = lambda x: set(x).issubset(element_list)
141
  isintersection = lambda x: len(set(x).intersection(element_list)) > 0
142
  entries_df = train_df[
 
154
  return options
155
 
156
 
157
+ # # Callback to update the material dropdown based on search
158
+ # @app.callback(
159
+ # [Output("material-dropdown", "options"), Output("material-dropdown", "value")],
160
+ # Input("search-button", "n_clicks"),
161
+ # State("query-input", "value"),
162
+ # )
163
+ # def update_material_dropdown(n_clicks, query):
164
+ # if n_clicks is None or not query:
165
+ # return [], None
166
+ # options = search_materials(query)
167
+ # if not options:
168
+ # return [], None
169
+ # return options, options[0]["value"]
170
 
171
 
172
  # Callback to display the selected material
requirements.txt CHANGED
@@ -8,3 +8,4 @@ plotly
8
  pandas
9
  dash-bootstrap-components
10
  datasets
 
 
8
  pandas
9
  dash-bootstrap-components
10
  datasets
11
+ dash-mp-components