Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import gradio as gr | |
| # โหลดข้อมูลจากไฟล์ Excel ที่อยู่ในโค้ด | |
| def load_data(): | |
| # แทนที่ "path_to_your_file.xlsx" ด้วยพาธไฟล์ Excel ของคุณ | |
| data = pd.read_excel("./BT-Engage-ขาวคาดแดง.xlsx") | |
| return data | |
| # ฟังก์ชันสำหรับกรองข้อมูลพร้อมจัดการกับข้อผิดพลาด | |
| def filter_data(columns, budget_order, office_filter, ministry_filter, | |
| citizen_service, digital_government, data_service, ai, cloud_security, | |
| cybercrime, learning_center): | |
| # โหลดข้อมูล | |
| data = load_data() | |
| # เลือกคอลัมน์ที่จะใช้ | |
| if columns: | |
| data = data[columns] | |
| # เรียงลำดับงบประมาณ | |
| if budget_order == 'น้อยไปมาก': | |
| data = data.sort_values('งบประมาณ', ascending=True) | |
| elif budget_order == 'มากไปน้อย': | |
| data = data.sort_values('งบประมาณ', ascending=False) | |
| # กรองสำนักงาน | |
| if office_filter != "ทั้งหมด": | |
| data = data[data['สำนักงาน'] == office_filter] | |
| # กรองกระทรวง | |
| if ministry_filter != "ทั้งหมด": | |
| data = data[data['กระทรวง'] == ministry_filter] | |
| # ฟิลเตอร์สำหรับคอลัมน์ Y/N โดยตรวจสอบการมีอยู่ของคอลัมน์และค่าที่กรองได้ | |
| yn_columns = { | |
| 'Citizen Service & Doing Business': citizen_service, | |
| 'Digital Government': digital_government, | |
| 'Data Service': data_service, | |
| 'AI': ai, | |
| 'Cloud & Security': cloud_security, | |
| 'Cybercrime': cybercrime, | |
| 'Learning Center/Museum/Environmental/Forest': learning_center | |
| } | |
| for col, yn_filter in yn_columns.items(): | |
| if yn_filter != "ไม่กรอง": | |
| # ตรวจสอบว่าคอลัมน์มีอยู่ในข้อมูลหรือไม่และฟิลเตอร์ค่า Y หรือ N | |
| if col in data.columns: | |
| data = data[data[col].fillna('') == yn_filter] | |
| return data | |
| # โหลดข้อมูลสำหรับตัวเลือกในดรอปดาวน์ | |
| data = load_data() | |
| offices = ["ทั้งหมด"] + sorted(data['สำนักงาน'].dropna().unique().tolist()) | |
| ministries = ["ทั้งหมด"] + sorted(data['กระทรวง'].dropna().unique().tolist()) | |
| # การตั้งค่าอินเตอร์เฟซ Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Data Viewer and Filter") | |
| column_selector = gr.CheckboxGroup( | |
| choices=['ชื่อโครงการ', 'งบประมาณ', 'วัตถุประสงค์', 'สถานที่ดำเนินการ', | |
| 'ระยะเวลาดำเนินการ', 'สำนักงาน', 'กระทรวง', | |
| 'Citizen Service & Doing Business', 'Digital Government', | |
| 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime', | |
| 'Learning Center/Museum/Environmental/Forest'], | |
| label="เลือกคอลัมน์ที่ต้องการแสดง") | |
| budget_order = gr.Radio(["น้อยไปมาก", "มากไปน้อย", "ไม่เรียงลำดับ"], label="เรียงลำดับงบประมาณ") | |
| office_filter = gr.Dropdown(choices=offices, label="เลือกสำนักงาน") | |
| ministry_filter = gr.Dropdown(choices=ministries, label="เลือกกระทรวง") | |
| yn_filter_labels = [ | |
| 'Citizen Service & Doing Business', 'Digital Government', | |
| 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime', | |
| 'Learning Center/Museum/Environmental/Forest' | |
| ] | |
| yn_filters = {} | |
| for label in yn_filter_labels: | |
| yn_filters[label] = gr.Radio(["ไม่กรอง", "Y", "N"], label=label, value="ไม่กรอง") | |
| submit_button = gr.Button("Filter Data") | |
| output_data = gr.Dataframe(label="ผลลัพธ์ข้อมูลที่กรองแล้ว") | |
| submit_button.click( | |
| filter_data, | |
| inputs=[column_selector, budget_order, office_filter, ministry_filter] + list(yn_filters.values()), | |
| outputs=output_data | |
| ) | |
| # รันแอปพลิเคชัน Gradio | |
| demo.launch() | |