vinci2quick
vinci2quick

Reputation: 11

Is there a way to add a confirmation message and button after form submission in Streamlit?

I have a streamlit form with different inputs, one being a selectbox. I like to have a confirmation message that checks the value in that selectbox and then update/create the data upon confirmation.

import streamlit as st
import requests

categories = {
    "CategoryA": "categorya",
    "CategoryB": "categoryb",
    "others": "others"
}

def create_item:
    with st.form("create_item")
        item_name = st.text_input("Item Name")
        category = st.selectbox(label="Category", options=categories)
        submitted = st.form_submit_button(label="Submit")

    if not submitted:
        return

# confirmation line of code
if item_name and category == "others":
    st.warning("You are adding item to others category. Please confirm if this is correct")
    confirmation = st.button("Confirm")

    if not confirmation:
        return

    if confirmation:
        body = {
            "item_name": item_name,
            "category": categories[category]
        }
        reply = requests.post(
            url="some.url/item",
            headers={"auth": auth},
            json=body,
        )

        if reply.ok:
            st.success("Sucessfully added item!")
        elif reply.status_code == 409:
            st.error("Already exist!")
        else:
            st.error("Failed to create item")

I tried debugging where the code went wrong and saw that it stopped at if not confirmation and finished the code there.

Is there any way around this that I can create a confirmation button and message before I submit my form?

I also tried creating a new form hoping that I just need another submit button but same thing, it resulted in not being confirmed.

Upvotes: 1

Views: 5202

Answers (1)

Tony Kipkemboi
Tony Kipkemboi

Reputation: 46

The reason your code is stopping at if not confirmation is that when you click the Confirm button, the code re-executes from the top, and the value of submitted is reset to False.

You can use st.session_state to store the values across interactions on the app. I have added the session state to your code:

import streamlit as st
import requests

categories = {
  "CategoryA": "category",
  "CategoryB": "category",
  "others": "others"
}

# Initialize the session state if it doesn't exist
# for both submitted and confirmation
if "submitted" not in st.session_state:
   st.session_state.submitted = False

if "confirmation" not in st.session_state:
   st.session_state.confirmation = False

def create_item():
    with st.form("create_item"):
        item_name = st.text_input("Item Name")
        category = st.selectbox(label="Category", 
        options=categories.keys())
        submitted = st.form_submit_button(label="Submit")

    # If the form is submitted, set the session state
    if submitted:
        st.session_state.submitted = True
        st.session_state.item_name = item_name
        st.session_state.category = category

    # Check if the form was submitted and category is 'others'
    if st.session_state.submitted and st.session_state.category == 
    "others":
        st.warning("You are adding item to others category. Please 
               confirm if this is correct")
        st.session_state.confirmation = st.button("Confirm")

        # If confirmation button is not clicked, stop execution
        if not st.session_state.confirmation:
            return

    if st.session_state.submitted and st.session_state.confirmation:
        body = {
             "item_name": st.session_state.item_name,
             "category": categories[st.session_state.category]
        }
        reply = requests.post(
            url="some.url/item",
            headers={"auth": "auth"},
            json=body,
        )

        if reply.ok:
            st.success("Successfully added item!")
        elif reply.status_code == 409:
            st.error("Item already exists!")
        else:
            st.error("Failed to create item")

create_item()

Upvotes: 1

Related Questions