Skip to content

Commit

Permalink
Fix style unknown prop error on mpc.Switch (#335)
Browse files Browse the repository at this point in the history
* fix TypeError: The dash_mp_components.Switch component received an unexpected keyword argument: style

* add ruff rule sets: ICN, ISC, PD, PIE, PT, PYI, RET, RSE

* fix all ruff errors
  • Loading branch information
janosh committed May 9, 2023
1 parent bf7aedd commit fb2f185
Show file tree
Hide file tree
Showing 33 changed files with 217 additions and 303 deletions.
3 changes: 1 addition & 2 deletions crystal_toolkit/apps/examples/diffraction_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
@app.callback(Output(xrd_component.id(), "data"), Input(load_btn, "n_clicks"))
def load_structure(n_clicks: int) -> Structure:
"""Load a cubic structure on button click."""
structure = Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])
return structure
return Structure(Lattice.cubic(4.2), ["Na", "K"], [[0, 0, 0], [0.5, 0.5, 0.5]])


# run this app with "python path/to/this/file.py"
Expand Down
4 changes: 1 addition & 3 deletions crystal_toolkit/apps/examples/kwarg_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@ def add_inputs(n_clicks):
raise PreventUpdate

element = random.choice(["Li", "Na", "K"])
slider_input = your_component.get_slider_input(
return your_component.get_slider_input(
kwarg_label=f"slider_{element}",
default=random.uniform(0, 1),
label=f"{element} Slider Input",
help_str="This can explain to the user what this slider input controls.",
)

return slider_input


# tell Crystal Toolkit about the app and layout we want to display
ctc.register_crystal_toolkit(app=app, layout=my_layout, cache=None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def update_structure(active_cell: dict[str, int | str]) -> Structure:
point.
"""
row_idx = active_cell["row"]
structure = df_diel.structure[row_idx]

return structure
return df_diel.structure[row_idx]


if __name__ == "__main__":
Expand Down
14 changes: 4 additions & 10 deletions crystal_toolkit/apps/examples/mpcontribs/catalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ def get_plot_data(smile: str) -> pd.DataFrame:
}
for resp in contributions["data"]
]
df = pd.DataFrame(records)
return df
return pd.DataFrame(records)

@app.callback(
Output(self.id("heat_map"), "figure"),
Expand All @@ -239,8 +238,7 @@ def update_figure(smile, mid_E, range_E, active_tab):
smile = smile[0]
df = get_plot_data(smile)
df_full, df_min_E = self.modify_df(df)
plot = self.get_plot(df_full, df_min_E, mid_E, range_E, 1)
return plot
return self.get_plot(df_full, df_min_E, mid_E, range_E, 1)

@app.callback(
Output(self.id("display_table"), "children"),
Expand Down Expand Up @@ -300,7 +298,7 @@ def get_visualization(self, structure):
).layout()

def get_layout(self, payload=None):
tabs = dcc.Tabs(
return dcc.Tabs(
[
dcc.Tab(
children=[html.Br(), self.get_search_layout(payload=payload)],
Expand All @@ -322,8 +320,6 @@ def get_layout(self, payload=None):
id=self.id("tabs"),
)

return tabs

def get_visualization_layout(self, payload=None):
state = {"smiles": "*C", "targetE": 0.2, "range_E": 1}
smiles_input = self.get_choice_input(
Expand Down Expand Up @@ -366,7 +362,7 @@ def get_visualization_layout(self, payload=None):
[description, html.Br(), html.Br(), smiles_input, E_input, range_input]
)

viz_div = html.Div(
return html.Div(
ctl.Columns(
[
ctl.Column(
Expand Down Expand Up @@ -407,8 +403,6 @@ def get_visualization_layout(self, payload=None):
)
)

return viz_div

def get_search_layout(self, payload=None):
if not payload:
return self.get_catalysis_explorer()
Expand Down
8 changes: 3 additions & 5 deletions crystal_toolkit/apps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,9 @@ def update_search_term_on_page_load(href: str) -> str:
pathname = str(parse.urlparse(href).path).split("/")
if len(pathname) <= 1:
raise PreventUpdate
elif not pathname[1]:
if not pathname[1]:
return choice(DEFAULT_MPIDS)
else:
return pathname[1].replace("+", " ")
return pathname[1].replace("+", " ")


@app.callback(
Expand All @@ -468,8 +467,7 @@ def perform_search_on_page_load(
# TODO: could be a client side callback
if n_submit is None:
return 1, int(round(time() * 1000))
else:
raise PreventUpdate
raise PreventUpdate


@app.callback(Output("url", "pathname"), Input(search_component.id(), "data"))
Expand Down
4 changes: 2 additions & 2 deletions crystal_toolkit/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
BandstructureAndDosPanelComponent,
)
from crystal_toolkit.components.diffraction import (
XRayDiffractionComponent as XRayDiffractionComponent,
XRayDiffractionComponent,
)
from crystal_toolkit.components.diffraction_tem import (
TEMDiffractionComponent as TEMDiffractionComponent,
TEMDiffractionComponent,
)
from crystal_toolkit.components.fermi_surface import FermiSurfaceComponent
from crystal_toolkit.components.localenv import LocalEnvironmentPanel
Expand Down
46 changes: 22 additions & 24 deletions crystal_toolkit/components/bandstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,10 @@ def update_graph(traces):
def update_label_select(mpid, path_convention):
if not mpid:
raise PreventUpdate
else:
label_value = path_convention
label_style = {"maxWidth": "200"}
label_value = path_convention
label_style = {"maxWidth": "200"}

return label_value, label_style
return label_value, label_style

@app.callback(
Output(self.id("dos-select"), "options"),
Expand All @@ -820,7 +819,7 @@ def update_label_select(mpid, path_convention):
def update_select(elements, mpid):
if elements is None:
raise PreventUpdate
elif not mpid:
if not mpid:
dos_options = (
[{"label": "Element Projected", "value": "ap"}]
+ [{"label": "Orbital Projected - Total", "value": "op"}]
Expand All @@ -837,28 +836,27 @@ def update_select(elements, mpid):
path_style = {"maxWidth": "200", "display": "none"}

return [dos_options, path_options, path_style]
else:
dos_options = (
[{"label": "Element Projected", "value": "ap"}]
+ [{"label": "Orbital Projected - Total", "value": "op"}]
+ [
{
"label": f"Orbital Projected - {ele_label}",
"value": f"orb{ele_label}",
}
for ele_label in elements
]
)

path_options = [
{"label": "Setyawan-Curtarolo", "value": "sc"},
{"label": "Latimer-Munro", "value": "lm"},
{"label": "Hinuma et al.", "value": "hin"},
dos_options = (
[{"label": "Element Projected", "value": "ap"}]
+ [{"label": "Orbital Projected - Total", "value": "op"}]
+ [
{
"label": f"Orbital Projected - {ele_label}",
"value": f"orb{ele_label}",
}
for ele_label in elements
]
)

path_options = [
{"label": "Setyawan-Curtarolo", "value": "sc"},
{"label": "Latimer-Munro", "value": "lm"},
{"label": "Hinuma et al.", "value": "hin"},
]

path_style = {"maxWidth": "200"}
path_style = {"maxWidth": "200"}

return dos_options, path_options, path_style
return dos_options, path_options, path_style

@app.callback(
Output(self.id("traces"), "data"),
Expand Down
21 changes: 8 additions & 13 deletions crystal_toolkit/components/diffraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,7 @@ def get_figure(
if broadening:
plotdata.append(go.Scatter(x=x, y=y, hoverinfo="none"))

plot = go.Figure(data=plotdata, layout=layout)

return plot
return go.Figure(data=plotdata, layout=layout)

def generate_callbacks(self, app, cache) -> None:
@app.callback(
Expand Down Expand Up @@ -449,7 +447,7 @@ def update_graph(data, logsize, rad_source, peak_profile, K, x_axis, structure):
# suppress broadening for larger cell
broadening = len(self.from_data(structure).sites) < SITES_LIMIT

plot = self.get_figure(
return self.get_figure(
peak_profile,
K,
rad_source,
Expand All @@ -462,8 +460,6 @@ def update_graph(data, logsize, rad_source, peak_profile, K, x_axis, structure):
broadening,
)

return plot

@app.callback(
Output(self.id(), "data"),
[
Expand Down Expand Up @@ -500,14 +496,13 @@ def pattern_from_struct(struct, rad_source):
def update_message(structure):
if len(self.from_data(structure).sites) < SITES_LIMIT:
return html.Div([])
else:
return MessageContainer(
MessageBody(
"Peak broadening is currently disabled for materials with "
f"more than {SITES_LIMIT} sites due to long compute time. Please contact "
"feedback@materialsproject.org if you need assistance with the graph."
)
return MessageContainer(
MessageBody(
"Peak broadening is currently disabled for materials with "
f"more than {SITES_LIMIT} sites due to long compute time. Please contact "
"feedback@materialsproject.org if you need assistance with the graph."
)
)

# @app.callback(
# Output(self.id("static-image"), "src"), Input(self.id("xrd-plot"), "figure")
Expand Down
3 changes: 1 addition & 2 deletions crystal_toolkit/components/diffraction_tem.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,4 @@ def pointlist_to_spots(self, pattern, beam_direction, gamma):
paper_bgcolor="white",
plot_bgcolor="white",
)
fig = go.Figure(data=data, layout=layout)
return fig
return go.Figure(data=data, layout=layout)
13 changes: 6 additions & 7 deletions crystal_toolkit/components/localenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def _get_local_order_parameters(structure_graph, n):
for i, lostop in enumerate(lostop_vals):
d[names[i]] = lostop
return d
else:
return None
return None


class LocalEnvironmentPanel(PanelComponent):
Expand Down Expand Up @@ -262,7 +261,7 @@ def run_algorithm(algorithm):
]
)

elif algorithm == "localenv":
if algorithm == "localenv":
description = (
"The LocalEnv algorithm is developed by Nils Zimmerman et al. whereby "
"an 'order parameter' is calculated that measures how well that "
Expand All @@ -283,7 +282,7 @@ def run_algorithm(algorithm):
]
)

elif algorithm == "bondinggraph":
if algorithm == "bondinggraph":
description = (
"This is an alternative way to display the same bonds present in the "
"visualizer. Here, the bonding is displayed as a crystal graph, with "
Expand All @@ -299,7 +298,7 @@ def run_algorithm(algorithm):
]
)

elif algorithm == "soap":
if algorithm == "soap":
state = {
"rcut": 5.0,
"nmax": 2,
Expand Down Expand Up @@ -487,6 +486,7 @@ def run_algorithm(algorithm):
Loading(id=self.id("soap_similarities")),
]
)
return None

def _get_soap_graph(feature, label):
spectrum = {
Expand Down Expand Up @@ -766,8 +766,7 @@ def get_valences(struct):
valences = [v for v in valences if v is not None]
if len(valences) == len(struct):
return valences
else:
return "undefined"
return "undefined"

# decide which indices to present to user
sga = SpacegroupAnalyzer(struct)
Expand Down
45 changes: 19 additions & 26 deletions crystal_toolkit/components/phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ def figure_layout(self, plotter, pd):
if dim == 4:
if not entry.composition.is_element:
continue
else:
z = coords[2]
z = coords[2]

formula = list(entry.composition.reduced_formula)

Expand Down Expand Up @@ -333,7 +332,7 @@ def create_unstable_markers(self, plotter, pd):

energy = round(pd.get_form_energy_per_atom(unstable_entry), 3)
text_list.append(
f"{clean_formula} ({mpid})<br>" f"{energy} eV (+{e_above_hull} eV)"
f"{clean_formula} ({mpid})<br>{energy} eV (+{e_above_hull} eV)"
)

if dim == 2 or dim == 3:
Expand Down Expand Up @@ -403,9 +402,7 @@ def clean_formula(formula):
else:
s.append(char)

clean_formula = "".join(s)

return clean_formula
return "".join(s)

@staticmethod
def ternary_plot(plot_data):
Expand Down Expand Up @@ -548,29 +545,25 @@ def generate_callbacks(self, app, cache) -> None:
def update_graph(figure):
if figure is None:
raise PreventUpdate
elif figure == "error":
search_error = (
MessageContainer(
[
MessageBody(
dcc.Markdown(
"Plotting is only available for phase diagrams containing 2-4 components."
)
if figure == "error":
search_error = MessageContainer(
[
MessageBody(
dcc.Markdown(
"Plotting is only available for phase diagrams containing 2-4 components."
)
],
kind="warning",
),
)
],
kind="warning",
)
return search_error

else:
plot = [
dcc.Graph(
figure=figure,
config={"displayModeBar": False, "displaylogo": False},
)
]
return plot
return [
dcc.Graph(
figure=figure,
config={"displayModeBar": False, "displaylogo": False},
)
]

@app.callback(Output(self.id("figure"), "data"), Input(self.id(), "data"))
def make_figure(pd):
Expand Down Expand Up @@ -687,7 +680,7 @@ def create_table(chemsys, pd_time, n_clicks, pd, rows):
if trigger["prop_id"] == f"{self.id()}.modified_timestamp":
table_content = self.create_table_content(self.from_data(pd))
return table_content
elif (
if (
trigger["prop_id"] == f"{self.id('editing-rows-button')}.n_clicks"
and n_clicks > 0
and rows
Expand Down

0 comments on commit fb2f185

Please sign in to comment.