How do I clear number input after i have save data

Hi, I have created a dependency droplist to filter values in an excel sheet using st.selectbox and the final two lines of code is to allow number input. this number input are used to calculate the final total emissions by multiplying the amount with the emission factor.

  1. is there a way to capture all the drop list into a form and clear on submission? i tried using with st.form() but it did not allow the dependency drop list to work.

  2. how can i clear the number input when the "Save Data " is initiated?



### Steps to reproduce

**Code snippet:**
import streamlit as st 
import pandas as pd 
from streamlit_option_menu import option_menu
#------- PAGE SETTINGS------------
page_title = "GHG Emission Calculator"
Page_icon = "🌳"
layout = "centered"

#-----------------------------------
st.set_page_config(page_title=page_title,page_icon=Page_icon,layout=layout)
st.title(page_title + " " + Page_icon)

# --- HIDE STREAMLIT STYLE ---
hide_st_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            header {visibility: hidden;}
            </style>
            """
st.markdown(hide_st_style, unsafe_allow_html=True)

# --- NAVIGATION MENU ---
selected = option_menu(
    menu_title=None,
    options=["Data Entry", "Data Visualization"],
    icons=["pencil-fill", "bar-chart-fill"],  # https://icons.getbootstrap.com/
    orientation="horizontal",
)


@st.cache_resource
def get_data ():
    path = "Emissions.xlsx"
    return pd.read_excel(path,sheet_name="Sheet2",usecols="A:I")

#----remember to remove duplicates
data = get_data()
data_na = data.dropna()

#----select dropdown------- 

options1 = data_na.iloc[:,0].unique()
selected_option1 = st.selectbox("Select Scope:",options1, key= "Scope")

#----filtering scope-------
filtered_data = data_na[data_na.iloc[:,0]==selected_option1]

#----get unique values for option 2-----
option2 = filtered_data.iloc[:,1].unique()
selected_option2 = st.selectbox("Select Category:",option2, key = "Category")

#-----filter based on option 2-----
filter_2 = filtered_data[filtered_data.iloc[:,1]==selected_option2]
option3 = filter_2.iloc[:,2].unique()
selected_option3 = st.selectbox("Select Sub Category:",option3, key= "Sub_category")

#----filter based on option 3----
filter_3 = filter_2[filter_2.iloc[:,2]== selected_option3]
option4 = filter_3.iloc[:,3].unique()
selected_option4 = st.selectbox("Select Material:",option4, key = "Material")

#-----filter based on option 4----
filter_4 = filter_3[filter_3.iloc[:,3]==selected_option4]
option5 = filter_4["UOM"].unique()
selected_option5 = st.selectbox("Select Unit of Measure:",option5)

#----filter based on option 5-------
filter_5 = filter_4[filter_4["UOM"]== selected_option5]
option6 = filter_5["GHG/Unit"].unique()
selected_option6 = st.selectbox("Select Unit:",option6)

#-----filter based on last option-----
filter_6 = filter_5[filter_5["GHG/Unit"]== selected_option6]
option_7 = filter_6["GHG Conversion Factor 2022"].unique()
selected_option7 = st.selectbox("Emission Factor:",option_7)
#option7_int = int(selected_option7)

#----create an input field-------
values = st.number_input("Enter Amount",format="%i",min_value=0, key= "Amount")
values_int = int(values)

#----multiplying the two columns together to find total emission----

emission = int(selected_option7 * values_int)

total = st.number_input("Total Emissions:",emission, key="Total Emission")

#---Creating the submit button------------- 
submitted = st.button("Save Data")
if submitted:
    st.write("Scope:",selected_option1)
    st.write("selected option :",selected_option2)
    st.write("Sub Category :",selected_option3)
    st.write("Material :",selected_option4)
    st.write("Total Quantity:",values)
    st.write("Total :",emission)
    st.success("Data Saved Successfully!")

Thank you

Hey @sf558,

Thanks for sharing your question!

  1. is there a way to capture all the drop list into a form and clear on submission? i tried using with st.form() but it did not allow the dependency drop list to work.

What do you mean by the drop list (maybe options)?

  1. how can i clear the number input when the "Save Data " is initiated?

You can reset a st.number_input when a button is clicked by changing the session state value associated with the st.number_input’s key. (Shoutout to @mathcatsand for the awesome example and video in this thread).

import streamlit as st

if 'number_input' not in st.session_state:
    st.session_state.number_input = 0.0

def reset_number_input():
    st.session_state.number_input = 0.0

number_input = st.number_input("Number input",key='number_input')
button = st.button("Button", on_click=reset_number_input)
3 Likes

Thank you Caroline,

and yes for my question i meant multi-dependency options.

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