I want to prevent callback to happen if there is no input in text area and user clicked the submit button because I tried use condition if text is None raise PreventUpdate
but looks like it is not working.
Attaching the gif:
Code:
@app.callback(
[Output("output-state", "children"), Output("token", "children")],
[Input("submit-button", "n_clicks")],
[
State("input_text", "value"),
State("slider", "value"),
State("drop-down", "value"),
State("slider-2", "value"),
],
prevent_initial_call=True,
)
def label_prediction(num_clicks, text, threshold_value, preprocess_func, label_value):
if text is None or num_clicks is None:
raise PreventUpdate
else:
dict_params = {param: True for param in preprocess_func}
preprocess_text = preprocess(text, **dict_params)
transformed_text = tfidf.fit_transform([preprocess_text])
prediction = classifier.predict_proba(transformed_text)
result = get_tags(prediction[0], threshold_value, label_value)
final_result = ", ".join(e for e in result)
tokens = preprocess_text.split(" ")
final_tokens = ", ".join(f for f in tokens)
# print(preprocess_text)
print(final_result)
if len(result) < 1:
return (
dbc.Alert(
dcc.Markdown(
"**😔 Looks like I can't find any Tags...Try decreasing the Threshold value**"
),
color="warning",
duration=120000,
style={
"position": "fixed",
"height": "15%",
"width": "24.5%",
"margin-left": "32%",
"margin-top": "20px",
"box-shadow": "5px 5px 5px #cacfd2",
"margin-right": "5%",
},
),
final_tokens,
)
else:
return (
dbc.Alert(
[html.H6("Predicted Tags:"), html.Hr(), final_result],
color="success",
duration=120000,
style={
"position": "fixed",
"height": "15%",
"width": "24.5%",
"margin-left": "32%",
"margin-top": "20px",
"box-shadow": "5px 5px 5px #cacfd2",
"margin-right": "5%",
"overflow-y": "scroll",
},
),
final_tokens,
)
1 post - 1 participant