-
Notifications
You must be signed in to change notification settings - Fork 0
/
baojia2_streamlit.py
47 lines (39 loc) · 1.54 KB
/
baojia2_streamlit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import streamlit as st
import pandas as pd
import io
import baojia2
# 将数据处理和 DataFrame 创建分离到一个函数中
def process_and_display_data(data):
try:
df = baojia2.process_data(data)
if not df.empty:
st.success('数据转换成功!')
st.dataframe(df)
# 添加下载Excel文件功能
output = io.BytesIO()
df.to_excel(output, index=False, engine='xlsxwriter')
excel_data = output.getvalue()
st.download_button(
label="下载 Excel 文件",
data=excel_data,
file_name='transformed_data.xlsx',
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
else:
st.error('数据转换失败,请检查输入数据是否正确。')
except Exception as e:
st.error(f'数据转换过程中发生错误:{e}')
def main():
st.title('数据转换工具')
st.subheader('低压电流互感器参数标准化转换器V1.22')
# 添加上传文件功能
uploaded_file = st.file_uploader("上传 data.txt 文件", type="txt")
if uploaded_file is not None:
data = uploaded_file.getvalue().decode("utf-8").splitlines()
else:
data = st.text_area("输入数据", height=200).splitlines()
# 使用按钮来触发数据处理和显示
if st.button('转换'):
process_and_display_data(data)
if __name__ == "__main__":
main()