Hello guys. I have several ‘code’ elements on a row which I want to clear on the press of a button. Any ideas how I can do this? Thanks alot in advance
st.code does not support session_state->widget_state interaction directly, so had to improvise a solution
import streamlit as st
if 'id' not in st.session_state.keys():
st.session_state['id']='1'
if 'name' not in st.session_state.keys():
st.session_state['name'] = 'name'
cols = st.columns(3)
cols[0].code(st.session_state['id'], language="markdown")
cols[1].code(st.session_state['name'], language="markdown")
if cols[2].button('Clear'):
st.session_state['id']=''
st.session_state['name']=''
st.experimental_rerun()
Hi and thamks for the reply!
Tried your solution, but doesn’t seem to work. The fields don’t get cleared, idk why.
You say ‘st.code does not support session_state->widget_state interaction directly’. Do other st elements support that? I could try to change ‘st.code’ to ‘st.text’ / ‘st.write’ or something.
You can do this for removing the entire code elements
import streamlit as st
cols = st.columns(3)
with cols[0]:
field1 = st.code('id', language="markdown")
with cols[1]:
field2 = st.code('name', language="markdown")
if cols[2].button('Clear'):
field1.empty()
field2.empty()
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.