使用session_state在简化中更新度量的值
我已经简化了应用程序,其中我可以根据用户输入加载数据框,并能够过滤和返回数据。我的功能可以使用df.shape [0]
返回返回的记录的数量。
我想要的是根据用户过滤器的返回结果更改显示的记录。因此,我确实用于St.Session_state
,但看来我被滥用了。
代码:
def metric_employee(df):
if "df" in st.session_state:
return df['status_type'].value_counts()["employee"]
st.session_state.df=pd.DataFrame(df,columns= df.columns)
return st.session_state.df
result = df
col1_m,col2_m,col3_m = st.columns(3)
# use session state to display number corresponding to the result table!!!!!!!!
with col1_m:
st.markdown(
f'<p class = "employee_metric"> {metric_employee(result)} employee</p>',
unsafe_allow_html= True,
)
if radioDB=="Search":
regular_search_term=st.sidebar.text_input("Enter Search Term")
if st.sidebar.button("search",key=1):
result = df[df['name'].str.contains(regular_search_term)|
df['nickname'].str.contains(regular_search_term)|
df['mother_name'].str.contains(regular_search_term)]
st.write(f"{str(result.shape[0])} Records ")
display_aggrid(result)
st.Write(f“ {str(result.shape [0])}记录”)
上面的语句显示总返回结果 我想要的指标是在列中显示记录的数量,其中cell值是员工,我想每次用户过滤dataframe时都更改此数字。
那么,我的错误是什么?如何根据返回的结果更改数字?
I have streamlit app where i load a dataframe with the ability to filter and return data based on the user input. I have a function that return the number of returned records using df.shape[0]
.
What i want is to change the displayed number of records based on the return result by the user filter. so i did use for the st.session_state
but it seems i misused.
code:
def metric_employee(df):
if "df" in st.session_state:
return df['status_type'].value_counts()["employee"]
st.session_state.df=pd.DataFrame(df,columns= df.columns)
return st.session_state.df
result = df
col1_m,col2_m,col3_m = st.columns(3)
# use session state to display number corresponding to the result table!!!!!!!!
with col1_m:
st.markdown(
f'<p class = "employee_metric"> {metric_employee(result)} employee</p>',
unsafe_allow_html= True,
)
if radioDB=="Search":
regular_search_term=st.sidebar.text_input("Enter Search Term")
if st.sidebar.button("search",key=1):
result = df[df['name'].str.contains(regular_search_term)|
df['nickname'].str.contains(regular_search_term)|
df['mother_name'].str.contains(regular_search_term)]
st.write(f"{str(result.shape[0])} Records ")
display_aggrid(result)
st.write(f"{str(result.shape[0])} Records ")
the statement above display the total returned result
what i want in my metric is to display the number of records in column status_type where the cell value is employee , and i want to change this number each time the user filter the dataframe.
so what is my mistake and how to change the number based on the returned results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此。
Use this.