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?