【发布时间】:2021-11-15 02:23:50
【问题描述】:
我有以下代码:
import streamlit as st
import pandas as pd
#define data
d = {'id': ['a', 'b', 'c'], 'data': [3, 4,6]}
df = pd.DataFrame(data=d)
#create sidebar input
with st.sidebar.form("my_form"):
a = st.slider('sidebar for testing', 5, 10, 9)
calculate = st.form_submit_button('Calculate')
if calculate:
df['result'] = df['data'] + a
st.write(df)
#no issues up to this point. When I move the slider in 10 the output in 16 stays on the web page
########debug############
# I am trying to select an 'id' from the dropdown and use that to filter df, but when I select a value from the dropdown,
# the script runs again and the output disappears
filter = st.selectbox('filter data', df['id'].unique())
st.write(df[df['id'] == filter])
我想使用下拉菜单过滤 Pandas 数据框以选择我感兴趣的 id,但是当我使用下拉菜单时,代码会重新运行。
知道如何解决这个问题吗?
PS 我还尝试将整个计算包含在一个函数中并添加 @st.cache 装饰器,但没有成功。如果有人能告诉我它是如何完成的,我将不胜感激。
【问题讨论】: