Clearing code elements

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 :slight_smile:

cols = st.beta_columns(3)
cols[0].code('id', language="markdown")
cols[1].code('name', language="markdown")
if cols[2].button('Clear'):
     # clear


Just to make sure, you want to clear the code only the delete the whole element?


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():
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'):
1 Like

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.

Deleting the whole element might also do the job. You have idea on how to do that?

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'):

Both of my snippets seems to work for me, you can try running them on

1 Like