Placeholder in a form 'cause of st.radio - having trouble with submit_button

Guys, I’m here again to ask about the using of placeholders in a form.

@edsaac showed a good solution for me to solve the problem of having st.radio inside a form and not rerunning everytime I select an option.

So, I adapted his solution and it worked very well. But, I have one problem: when I hit the submit button, it’s not clearing on submit, as I code to do. I think it’s because I placehold it.

let me show you all with an adapted code of what I have:

with st.sidebar.form(key='formulary1', clear_on_submit=True):
    st.header("what you wish to do?")

    placeholder_for_radio = st.empty()
    placeholder_for_company = st.empty()
    placeholder_for_num = st.empty()
    placeholder_for_name = st.empty()
    placeholder_for_card = st.empty()
    placeholder_for_pin = st.empty()
    placeholder_for_observation = st.empty()
    placeholder_for_button = st.empty()

    submit_button = st.form_submit_button('Confirm')

with placeholder_for_radio:
    add_del = st.radio('what you wish to do?', ['Add, 'Del'], label_visibility='collapsed', horizontal=True)

with placeholder_for_company:
    if add_del == 'Add':
        company = st.selectbox(
            'Company',
            options=sorted(df['Company'].unique()),
            index=None,
            placeholder='Choose a company'
        )
    else:
        company = st.selectbox(
            'Empresa',
            options=sorted(df['Company'].unique()),
            index=None,
            placeholder='Choose a company',
            disabled=True
        )

with placeholder_for_num:
    if add_del == 'Add':
        num = st.text_input(
            "Number",
            placeholder='Insert a worker number'
        )
    else:
        num = st.text_input(
            "Number",
            placeholder='Insert a worker number',
            disabled=True
        )

# ...
# ...
# ...

with placeholder_for_observation:
    if add_del == 'Add':
        observation = st.text_area(
            "Observation",
            placeholder='Insert an observation'
        )
    else:
        observation = st.text_area(
            "Observation",
            placeholder='Insert an observation'
        )


with placeholder_for_button:
    if add_del == 'Add':
        if submit_button:
            date_time_add = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
            if company and num and name and card and pin and observation:
                #whatever
                row_to_add = df[df['Card'] == card].index
                df2 = pd.concat([df2, row_to_add], ignore_index=True)

    else:
        if submit_button:
            date_time_del = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
            if card and observation:
                row_to_delete = df[df['Card'] == card].index
                df.drop(row_to_delete, inplace=True)

Just to explain, I have a formulary with some input widgets, but they are either disabled or not depending on the radio option that is selected. The only two that are not disabled are Card and Observation. And then, the submit button also has two different actions depending on the radio option.
But i think, when i placehold the button, it doesn’t allow the form to clear on submit. I don’t know what to do. Could you help me?

And I also ask for another help. The way i created all these placeholders, with lots of with’s, seems to be wrong. I don’t know if i can have 1 placeholder for the radio and another one for all the input widgets .

I thank you all in advace for your patience.

I solved this problem without putting more than 1 placeholder. I’ve just used the placeholder for the st.radio. I think, not using ‘with’, helped me. This was the way i structured my solution:

form1 = st.sidebar.form(key='formulary1', clear_on_submit=True)

form1.header("what you wish to do?")

placeholder_for_radio = form1.empty()

with placeholder_for_radio:
    add_del = st.radio('what you wish to do?', ['Add, 'Del'], label_visibility='collapsed', horizontal=True)

if add_del == 'Add':
    company = form1.selectbox(
        'company',
        options=sorted(df['Company'].unique()),
        index=None,
        placeholder='Choose the company'
    )

    num = form1.text_input(
        "Number",
        placeholder='Insert a worker number'
    )

#...
#...
#...

    observation = form1.text_area(
        "Observation",
        placeholder='Insert an observation'
    )

else:
    company = form1.selectbox(
        'company',
        options=sorted(df['Company'].unique()),
        index=None,
        placeholder='Choose the company',
        disabled=True
    )
    num = form1.text_input(
        "Number",
        placeholder='Insert a worker number',
        disabled=True
    )

#...
#...
#...   

    observation = form1.text_area(
        "Observation",
        placeholder='Insert an observation'
    )


if form1.form_submit_button('Confirm'):
    if add_del == 'Adicionar':
        if submit_button:
            date_time_add = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
            if company and num and name and card and pin and observation:
                #whatever
                row_to_add = df[df['Card'] == card].index
                df2 = pd.concat([df2, row_to_add], ignore_index=True)
    else:
        if submit_button:
            date_time_del = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
            if card and observation:
                row_to_delete = df[df['Card'] == card].index
                df.drop(row_to_delete, inplace=True)
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

Glad to hear that you’d found a solution and thanks for sharing the solution.