Search code examples
pythondrop-down-menusession-statestreamlit

Deleted column in streamlit dropdown menu not updated immediately


I am trying to implement a "delete column" button for a csv file by means of a dropdownmenu form in streamlit. It works fine and removes the column, only the dropdownmenu list will not get updated until I refresh the page. I would like it to be done automatically.

File input.csv:

col1,col2,col3
x,x,x
x,x,x
x,x,x
x,x,x

Code application.py:

import streamlit as st
import pandas as pd

with st.form('Form1', clear_on_submit = True):
     st.session_state.df = pd.read_csv(r'./input.csv')
     column_name = st.selectbox('Delete column:', st.session_state.df.columns.tolist())         
     delete_column = st.form_submit_button('Delete this column') 
                        
     if delete_column:                                        
          st.session_state.df.drop(str(column_name), axis=1, inplace=True)   
          st.session_state.df.to_csv(r'./input.csv', index = False)

Example: after deleting "col3", it will still be visible here until I refresh the page:

enter image description here


Solution

  • Just rerun it after col deletion.

    st.session_state.df.to_csv(r'./input.csv', index = False)
    st.experimental_rerun()  # <======= here