Última actividad 10 months ago

此程式使用 Streamlit 讀取、編輯並存儲 YAML 設定檔,透過 pandas 轉換為表格格式,提供直覺化 UI 介面,適用於設定管理與資料修改。

Revisión faf007d110b8e617ee1a4bc369d172dcc43cc7eb

data.yml Sin formato
1age: 30
2is_active: true
3name: John Doe
4
edit_yaml_data.py Sin formato
1import streamlit as st
2import yaml
3import pandas as pd
4from io import StringIO
5
6# 定義讀取 YAML 文件的函數
7def read_yaml(file_path):
8 with open(file_path, 'r') as file:
9 return yaml.safe_load(file)
10
11# 定義寫入 YAML 文件的函數
12def write_yaml(file_path, data):
13 with open(file_path, 'w') as file:
14 yaml.dump(data, file, default_flow_style=False)
15
16# 讀取 YAML 文件
17file_path = 'data.yml'
18data = read_yaml(file_path)
19
20# 將 YAML 資料轉換為 DataFrame
21data_df = pd.DataFrame([data])
22
23# 使用 st.data_editor 編輯資料
24edited_data_df = st.data_editor(data_df)
25
26# 如果資料有變更,更新 YAML 文件
27if st.button('Save'):
28 edited_data = edited_data_df.iloc[0].to_dict()
29 write_yaml(file_path, edited_data)
30 st.success('Data saved successfully!')
31
32# 顯示編輯後的資料
33st.write('Edited Data:', edited_data_df)
34
35