Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Callback doesn't return anything

Code:

def get_tags(predicted_list, threshold, labels):
    mlb = [(i1, c1) for i1, c1 in enumerate(multilabel.classes_)]
    temp_list = sorted(
        [(i, c) for i, c in enumerate(list(predicted_list))],
        key=lambda x: x[1],
        reverse=True,
    )
    tag_list = [item1 for item1 in temp_list if item1[1] >= threshold]
    tags = [
        item[1] for item2 in tag_list[:labels] for item in mlb if item2[0] == item[0]
    ]
    return tags


app = dash.Dash(
    __name__, title="Multi Tag Prediction", external_stylesheets=[dbc.themes.BOOTSTRAP]
)
server = app.server
app.layout = html.Div(
    [
        html.H2(
            ["Multi Tag Prediction"],
            style={
                "text-align": "left",
                "margin-left": "4.5%",
                "margin-right": "2%",
                "margin-bottom": "2%",
            },
        ),
        dbc.Row(
            [
                dbc.Col(
                    html.Div(
                        [
                            dbc.FormGroup(
                                [
                                    dbc.Label(
                                        "Filter by Preprocessing Functions to Apply:"
                                    ),
                                    dbc.RadioItems(
                                        options=[
                                            {"label": "All", "value": "all"},
                                            {"label": "Customized", "value": "custom"},
                                        ],
                                        value="all",
                                        id="radio-button",
                                        inline=True,
                                        style={"padding": "10px"},
                                    ),
                                ]
                            ),
                            html.Div(
                                [
                                    dcc.Dropdown(
                                        id="drop-down",
                                        options=[
                                            {
                                                "label": "remove_digits",
                                                "value": "remove_digits",
                                            },
                                            {
                                                "label": "remove_stopwords",
                                                "value": "stop_words",
                                            },
                                            {
                                                "label": "text_lemmatization",
                                                "value": "text_lemmatization",
                                            },
                                        ],
                                        value=[
                                            "remove_digits",
                                            "remove_stopwords",
                                            "text_lemmatization",
                                        ],
                                        multi=True,
                                        placeholder="Select the preprocessing functions",
                                        searchable=True,
                                    )
                                ],
                                style={"padding": "5px", "margin-bottom": "10px",},
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Filter by threshold (probabilty of labels to include):"
                                    ),
                                    dcc.Slider(
                                        id="slider",
                                        min=0.1,
                                        max=0.9,
                                        step=0.1,
                                        value=0.5,
                                        marks={
                                            0.1: "0.1",
                                            0.2: "0.2",
                                            0.3: "0.3",
                                            0.4: "0.4",
                                            0.5: "0.5",
                                            0.6: "0.6",
                                            0.7: "0.7",
                                            0.8: "0.8",
                                            0.9: "0.9",
                                        },
                                        tooltip={"placement": "top"},
                                        included=False,
                                    ),
                                ],
                                style={"margin-bottom": "10px"},
                            ),
                            html.Div(
                                [
                                    html.H6(
                                        "Filter by label count (how many labels to include if there are more):"
                                    ),
                                    dcc.Slider(
                                        id="slider-2",
                                        min=5,
                                        max=10,
                                        step=1,
                                        value=5,
                                        marks={
                                            5: "5",
                                            6: "6",
                                            7: "7",
                                            8: "8",
                                            9: "9",
                                            10: "10",
                                        },
                                        tooltip={"placement": "top"},
                                        included=False,
                                    ),
                                ],
                                style={"margin-top": "10px"},
                            ),
                        ],
                        style={
                            "width": "100%",
                            "box-shadow": "5px 5px 5px  #cacfd2",
                            "padding": "35px",
                            "background-color": "#f9f9f9",
                        },
                    ),
                    width={"size": 4, "order": "first", "offset": 0},
                ),
                dbc.Col(
                    html.Div(
                        [
                            dbc.Textarea(
                                id="input_text",
                                value="",
                                style={
                                    "height": 300,
                                    "width": "100%",
                                    "box-shadow": "5px 5px 5px  #cacfd2",
                                    # "margin-left": "-20px",
                                    "background-color": "#f9f9f9",
                                },
                                maxLength=10000,
                                placeholder="Type the text or copy-paste from somewhere else",
                            ),
                            dbc.Button(
                                "Submit",
                                id="submit-button",
                                n_clicks=0,
                                style={"margin-left": "532px", "margin-top": "15px",},
                                outline=True,
                                color="primary",
                            ),
                            html.Div(
                                [
                                    dbc.Spinner(
                                        [
                                            dbc.Toast(
                                                id="output-state",
                                                header="Predicted Labels:",
                                                icon="info",
                                                is_open=True,
                                                duration=1000000,
                                                dismissable=False,
                                                style={
                                                    "position": "fixed",
                                                    "height": "15%",
                                                    "width": "400%",
                                                    "margin-top": "20px",
                                                },
                                                children=[],
                                            )
                                        ],
                                        type="grow",
                                        color="info",
                                        fullscreen=False,
                                    ),
                                ]
                            ),
                        ]
                    ),
                    width={"size": 6, "order": "second"},
                    # align="end",
                ),
            ],
            justify="around",
        ),
    ],
    
)


@app.callback(Output("drop-down", "value"), [Input("radio-button", "value")])
def display_status(selector):
    if selector == "all":
        return ["remove_digits", "text_lemmatization", "remove_stopwords"]
    else:
        return []


@app.callback(
    Output("output-state", "is_open"),
    [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:

        params = ["remove_digits", "remove_stopwords", "text_lemmatization"]
        dict_params = [param in preprocess_func for param in params]
        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)
        print(str(result))
        return result

       
if __name__ == "__main__":
    app.run_server(debug=True)

Are you talking about the last callback? That’s wired up to Output("output-state", "is_open") which looks like a boolean prop, but you’re sending it a list of strings?

Yes I am taking about last callback. So, how do you suggest to output the results if it is bool prop?
Becoz, i tried to output the results with simple html.Div element and still it was returning nothing.
Could you help me what I could be doing wrong?

A bool prop needs a bool value - if you need to control is_open as well as what content is displayed in output-state, then you’ll need a callback that outputs two props (presumably is_open and children) and have the function return two values - something like return (True if results else False), results, if your goal is to open that component when the results list has entries in it.

Dropping to a simpler type of component is indeed a good strategy for debugging. If you turn output-state into a html.Div then Output("output-state", "children") should happily accept and display a list of strings. You can test that by having the callback return some constant value like ["a", "b", "c"]. That will help you narrow down where the problem might be.

A final note: it’s really hard to provide troubleshooting feedback on code that’s so long. Try pruning your code down to the simplest example that shows the problem you’re having, it will make it much easier to respond to.

It is giving me error.

Output("output-state", "is_open", "children"),
TypeError: __init__() takes 3 positional arguments but 4 were given

Right, it needs to be two outputs - take a look at Basic Callbacks | Dash for Python Documentation | Plotly, specifically the example “Dash App With Multiple Outputs”

I returned 2 output from function even after that it gives me this error.

I’m not talking about what the function returns, although yes, that also needs to be two values. I’m talking about the output definition. Output("output-state", "is_open", "children") is not valid, as the error states. In the callback docs page you can see that what you need to do is define multiple outputs, like:

Output("output-state", "is_open"),
Output("output-state", "children")

Ohh…I got it what you were referring to…my bad :disappointed: