Zuletzt aktiv 10 months ago

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

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

timmy hat die Gist bearbeitet 2 years ago. Zu Änderung gehen

2 files changed, 37 insertions

data.yml(Datei erstellt)

@@ -0,0 +1,3 @@
1 + age: 30
2 + is_active: true
3 + name: John Doe

edit_yaml_data.py(Datei erstellt)

@@ -0,0 +1,34 @@
1 + import streamlit as st
2 + import yaml
3 + import pandas as pd
4 + from io import StringIO
5 +
6 + # 定義讀取 YAML 文件的函數
7 + def read_yaml(file_path):
8 + with open(file_path, 'r') as file:
9 + return yaml.safe_load(file)
10 +
11 + # 定義寫入 YAML 文件的函數
12 + def 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 文件
17 + file_path = 'data.yml'
18 + data = read_yaml(file_path)
19 +
20 + # 將 YAML 資料轉換為 DataFrame
21 + data_df = pd.DataFrame([data])
22 +
23 + # 使用 st.data_editor 編輯資料
24 + edited_data_df = st.data_editor(data_df)
25 +
26 + # 如果資料有變更,更新 YAML 文件
27 + if 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 + # 顯示編輯後的資料
33 + st.write('Edited Data:', edited_data_df)
34 +
Neuer Älter