我在 Pycharm 中为 Streamlit Data 应用程序输入了以下脚本:
import pandas as pd
import plotly.express as px
import streamlit as st
st.set_page_config(page_title='Matching Application Number',
layout='wide')
df = pd.read_csv('Analysis_1.csv')
st.sidebar.header("Filter Data:")
MeetingFileType = st.sidebar.multiselect(
"Select File Type:",
options=df['MEETING_FILE_TYPE'].unique(),
default=df['MEETING_FILE_TYPE'].unique()
)
df_selection = df.query(
'MEETING_FILE_TYPE == @MeetingFileType'
)
st.dataframe(df_selection)
streamlit 上的输出是:
Application_ID MEETING_FILE_TYPE
BBC#:1010 1
NBA#:1111 2
BRC#:1212 1
SAC#:1412 4
QRD#:1912 2
BBA#:1092 4
如何返回 1 和 2 的匹配 Application_ID 结果,如下所示:
Filter Data: Application_ID MEETING_FILE_TYPE
select type: BBC#:1010 1
1 2 NBA#:1111 2
BRC#:1212 1
QRD#:1912 2
那么,如何将Streamlit上面的数据结果下载到csv文件中呢?谢谢。
看看代码注释。
...
st.dataframe(df_selection)
# Show download button for the selected frame.
# Ref.: https://docs.streamlit.io/library/api-reference/widgets/st.download_button
csv = df_selection.to_csv(index=False).encode('utf-8')
st.download_button(
label="Download data as CSV",
data=csv,
file_name='selected_df.csv',
mime='text/csv',
)
我正在使用你的代码。
import pandas as pd
import plotly.express as px
import streamlit as st
data = {
"Application_ID": ['BBC#:1010', 'NBA#:1111', 'BRC#:1212',
'SAC#:1412', 'QRD#:1912', 'BBA#:1092'],
"MEETING_FILE_TYPE": [1, 2, 1, 4, 2, 4]
}
st.set_page_config(page_title='Matching Application Number',
layout='wide')
# df = pd.read_csv('Analysis_1.csv')
df = pd.DataFrame(data)
st.sidebar.header("Filter Data:")
MeetingFileType = st.sidebar.multiselect(
"Select File Type:",
options=df['MEETING_FILE_TYPE'].unique(),
default=df['MEETING_FILE_TYPE'].unique()
)
df_selection = df.query(
'MEETING_FILE_TYPE == @MeetingFileType'
)
st.dataframe(df_selection)
# Show download button for the selected frame.
# Ref.: https://docs.streamlit.io/library/api-reference/widgets/st.download_button
csv = df_selection.to_csv(index=False).encode('utf-8')
st.download_button(
label="Download data as CSV",
data=csv,
file_name='selected_df.csv',
mime='text/csv',
)
我认为您可以使用 csv 模块将输出导出为 csv 文件
import csv
with open('filename.csv') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(df_selection)
这是一个工作版本:
import pandas as pd
import numpy as np
import streamlit as st
st.set_page_config(page_title='Matching Application Number', layout='wide')
if "df" not in st.session_state:
# Generate some mocked data
num_rows = 100
meeting_file_types = [1, 2, 3, 4]
data = {
'MEETING_FILE_TYPE': np.random.choice(meeting_file_types, num_rows),
'Value': np.random.rand(num_rows) * 100
}
st.session_state.df = pd.DataFrame(data)
st.sidebar.header("Filter Data:")
MeetingFileType = st.sidebar.multiselect(
"Select File Type:",
options=st.session_state.df['MEETING_FILE_TYPE'].unique(),
default=[1,2]
)
# Filter DataFrame based on selection
if MeetingFileType:
df_selection = st.session_state.df.query('MEETING_FILE_TYPE in @MeetingFileType')
else:
df_selection = st.session_state.df
st.write("Filtered Data:")
st.dataframe(df_selection)
# Download functionality
def convert_df_to_csv(df):
return df.to_csv(index=False).encode('utf-8')
csv = convert_df_to_csv(df_selection)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='filtered_data.csv',
mime='text/csv'
)