I wrote the code as follows for a Streamlit
app. I read a DataFrame which is the output of a function. The code works in a way that I can select an item in a st.multiselect
, however when I select an item, it runs and doesn't update the DataFrame. How should I fix this issue?
#CODE is the dataframe columns that I perform filtering.
def do_something():
return df
df = do_something()
def select(df):
x= df.CODE.unique().tolist()
codes= st.columns([0.1, 0.1])
codes = codes.multiselect("Select Code", x, key={"key1"})
if codes:
df = df.query("CODE in @codes)
return df
Seems like you just need to re-assign your DataFrame after/when calling select
.
import streamlit as st
import pandas as pd
def do_something():
df = pd.DataFrame({"CODE": ["A", "B", "C", "D", "E"],
"VALUE": [1, 2, 3, 4, 5]})
return df
def select(df):
x = df.CODE.unique().tolist()
codes = st.multiselect("Select Code", x, key="key1")
if codes:
df = df.query("CODE in @codes")
return df
df = do_something()
st.write("BEFORE:")
st.write(df)
df = select(df) # <---- add this line
st.write("AFTER:")
st.write(df)
Output :