Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

How to use multiple dropdown values with radio buttons in custom function

$
0
0

I am trying to use dropdown with multiple values selected at once with radio buttons and use the values of dropdown in custom function like if selected, make that value == True else false in my function. I am struggling to implement this.
Here is the code of dropdown and custom function:

app.layout = html.Div(
    [
        html.H2(
            ["Multi Tag Prediction"],
            style={
                "text-align": "left",
                "margin-left": "2%",
                "margin-right": "2%",
                "margin-bottom": "1%",
            },
        ),
        html.Hr(),
        dbc.Row(
            [
                dbc.Col(
                    html.Div(
                        [
                            html.H6("Filter by Preprocessing Functions to Apply:"),
                            dcc.RadioItems(
                                id="radio-button",
                                options=[
                                    {"label": "All", "value": "NYC"},
                                    {"label": "Customized", "value": "MTL"},
                                ],
                                value="NYC",
                                labelStyle={"display": "inline-block"},
                                inputStyle={"margin-right": "2px"},
                                style={"padding": "5px"},
                            ),
                            dcc.Dropdown(
                                id="dropdown",
                                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 to apply",
                                searchable=True,
                                style={"padding": "15px", "margin-bottom": "10px"},
                            ),
                            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,
                            ),
                            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={
                            "width": "100%",
                            "box-shadow": "5px 5px 5px  #cacfd2",
                            "padding": "30px",
                            "background-color": "#f9f9f9",
                        },
                    ),
                    width={"size": 4, "order": "first", "offset": 0},
                ),
                dbc.Col(
                    html.Div(
                        [
                            dcc.Textarea(
                                id="input_text",
                                value="Type the text or copy-paste from somewhere else",
                                style={
                                    "height": 300,
                                    "width": "100%",
                                    "box-shadow": "5px 5px 5px  #cacfd2",
                                    "margin-left": "-20px",
                                    "background-color": "#f9f9f9",
                                },
                            ),
                            dcc.Loading(
                                id="spinner",
                                children=[
                                    html.Button(
                                        "Submit",
                                        id="button",
                                        n_clicks=0,
                                        style={
                                            "margin-left": "532px",
                                            "margin-top": "15px",
                                        },
                                    )
                                ],
                                type="graph",
                            ),
                            html.Div(id="output-state"),
                        ],
                    ),
                    width={"size": 6, "order": "second"},
                    # align="end",
                ),
            ],
            justify="around",
        ),
    ],
    style={"background-color": "#f2f2f2"},
)


@app.callback(
    Output("output-state", "children"),
    [Input("button", "num_clicks")],
    [
        State("input_text", "text"),
        State("slider", "threshold_value"),
        State("dropdown", "preprocess_func"),
        State("slider-2", "label_value"),
        State("radio-button", "radio_button"),
    ],
)
def label_prediction(
    num_clicks, text, threshold_value, preprocess_func, label_value, radio_button
):
    if text is None:
        raise PreventUpdate
    else:
        for item in preprocess_func:
            preprocess_text = preprocess(text, item = True)
            transformed_text = tfidf.fit_transform([preprocess_text])
            prediction = classifier.predict_proba(transformed_text)
    return f"Predicted labels:{get_tags(prediction[0],threshold_value,label_value)}"

def preprocess(
    text,
    clean_data=True,
    contraction_expansion=True,
    accented_char_removal=True,
    text_lemmatization=False,
    link_emoji=True,
    strip_html_tag=True,
    special_characters=True,
    remove_digits=False,
    stop_words=False,
):

    # cleaned data (lower & strip whitespaces & spelling mistake)
    if clean_data:
        text = data_clean(text)
    # strip html
    if strip_html_tag:
        text = strip_html_tags(text)
    # accented char removal
    if accented_char_removal:
        text = remove_accented_chars(text)

    # exapand contraction
    if contraction_expansion:
        text = contractions(text)
    if stop_words:
        text = remove_stopwords(text)
    # lemmetization
    if text_lemmatization:
        text = lemmatize_text(text)
    # punctuations and digits
    if special_characters:
        text = remove_special_characters(text, remove_digits=remove_digits)

    # remove extra whitespace
    text = re.sub(" +", " ", text)

    return text

Every parameter have it’s own function. What I am doing is giving some options from these functions to user as dropdown but I also want to tie these to radio button as to select all at a time on clicking or choose one/two among all.
See the pic below for detailed layout.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles