I am trying to build a simple app where user will enter data & give feedback based on predicted result.
Code:
import streamlit as st
def main():
st.title("Data Categorization")
txt = st.text_area("Paste your data here..")
if st.button("Find Category"):
try:
if txt != '':
value = st.selectbox("Agree with result", ["Yes, I agree..", "Nah!! I don't"])
if st.button("Submit report"):
if value == "Yes, I agree..":
#st.write(value)
print('Do this ...')
elif value != "Nah!! I don't agree":
print('Do that ...')
st.write("Thank You..")
else:
st.write('No content found')
except:
st.write('Looks like I am having problem connecting my backend')
if __name__ == '__main__':
main()
What happens here is when you press the second button "Submit report" the script is rerun, and the first button hit "Find category" will go back to None since you hit the second one. So Streamlit wonât go inside the code block from the first button. You need to maintain the info that you pressed the first button somewhere to force Streamlit to go into the code block.
Thereâs a way to preserve this information inside a state that is preserved between runs, with the State hack. So if you put the State script near your file, you can then do the following :
import streamlit as st
import st_state_patch
def main():
st.title("Data Categorization")
txt = st.text_area("Paste your data here..")
s = st.State()
if not s:
s.pressed_first_button = False
if st.button("Find Category") or s.pressed_first_button:
s.pressed_first_button = True # preserve the info that you hit a button between runs
try:
if txt != '':
value = st.selectbox("Agree with result", ["Yes, I agree..", "Nah!! I don't"])
if st.button("Submit report"):
if value == "Yes, I agree..":
st.write('Do this ...')
elif value != "Nah!! I don't agree":
st.write('Do that ...')
else:
st.write('No content found')
except:
st.write('Looks like I am having problem connecting my backend')
if __name__ == '__main__':
main()
Hi @andfanilo Thanks a lotâŠ!! for such a quick response. It worked like a magic spell, I guess I need to get hang of your implementation approach better. Also can you plz help me on how can I set the textarea -> txt blank again after âSubmit reportâ button click & in case I have to reset the state of page to init again?
Hey!! I did it using empty() & session run id. Thanks!! But how can I do a reset of all components means the app on each run. Do, I need to change the session key for all components?
import streamlit as st
import st_state_patch
import SessionState
def main():
st.title("Data Categorization")
session = SessionState.get(txt = '',run_id=0)
controler = st.empty()
session.txt = controler.text_area("Paste your data here..")
print(controler)
print(session.txt)
s = st.State()
if not s:
s.pressed_first_button = False
if st.button("Find Category") or s.pressed_first_button:
session.run_id += 1
s.pressed_first_button = True # preserve the info that you hit a button between runs
try:
if session.txt != '':
value = st.selectbox("Agree with result", ["Yes, I agree..", "Nah!! I don't"])
if st.button("Submit report"):
if value == "Yes, I agree..":
st.write('Do this ...')
elif value != "Nah!! I don't agree":
st.write('Do that ...')
controler.text_area("Paste your data here..", key=session.run_id)
else:
st.write('No content found')
except Exception as ex:
st.write('Looks like I am having problem connecting my backend')
print(ex)
if __name__ == '__main__':
main()
Thank you for the reference. I am still unable to make this work, the error I am getting:
AttributeError: module âstreamlitâ has no attribute âStateâ
I am trying to pip install st_state_patch or SessionState, and I am getting:
pip install st_state_patch
ERROR: Could not find a version that satisfies the requirement st_state_patch (from versions: none)
ERROR: No matching distribution found for st_state_patch
I appreciate the help!
Hello,
If use the above mentioned solution, it is giving me an attribute error. (module âstreamlitâ has no attribute âStateâ). Can you please help me?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking âAccept allâ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.