Teera commited on
Commit
3d4edd5
·
verified ·
1 Parent(s): afd8f24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -96
app.py CHANGED
@@ -1,96 +1,96 @@
1
- import pandas as pd
2
- import gradio as gr
3
-
4
- # โหลดข้อมูลจากไฟล์ Excel ที่อยู่ในโค้ด
5
- def load_data():
6
- # แทนที่ "path_to_your_file.xlsx" ด้วยพาธไฟล์ Excel ของคุณ
7
- data = pd.read_excel("./content/BT-Engage-ขาวคาดแดง.xlsx")
8
- return data
9
-
10
- # ฟังก์ชันสำหรับกรองข้อมูลพร้อมจัดการกับข้อผิดพลาด
11
- def filter_data(columns, budget_order, office_filter, ministry_filter,
12
- citizen_service, digital_government, data_service, ai, cloud_security,
13
- cybercrime, learning_center):
14
-
15
- # โหลดข้อมูล
16
- data = load_data()
17
-
18
- # เลือกคอลัมน์ที่จะใช้
19
- if columns:
20
- data = data[columns]
21
-
22
- # เรียงลำดับงบประมาณ
23
- if budget_order == 'น้อยไปมาก':
24
- data = data.sort_values('งบประมาณ', ascending=True)
25
- elif budget_order == 'มากไปน้อย':
26
- data = data.sort_values('งบประมาณ', ascending=False)
27
-
28
- # กรองสำนักงาน
29
- if office_filter != "ทั้งหมด":
30
- data = data[data['สำนักงาน'] == office_filter]
31
-
32
- # กรองกระทรวง
33
- if ministry_filter != "ทั้งหมด":
34
- data = data[data['กระทรวง'] == ministry_filter]
35
-
36
- # ฟิลเตอร์สำหรับคอลัมน์ Y/N โดยตรวจสอบการมีอยู่ของคอลัมน์และค่าที่กรองได้
37
- yn_columns = {
38
- 'Citizen Service & Doing Business': citizen_service,
39
- 'Digital Government': digital_government,
40
- 'Data Service': data_service,
41
- 'AI': ai,
42
- 'Cloud & Security': cloud_security,
43
- 'Cybercrime': cybercrime,
44
- 'Learning Center/Museum/Environmental/Forest': learning_center
45
- }
46
-
47
- for col, yn_filter in yn_columns.items():
48
- if yn_filter != "ไม่กรอง":
49
- # ตรวจสอบว่าคอลัมน์มีอยู่ในข้อมูลหรือไม่และฟิลเตอร์ค่า Y หรือ N
50
- if col in data.columns:
51
- data = data[data[col].fillna('') == yn_filter]
52
-
53
- return data
54
-
55
- # โหลดข้อมูลสำหรับตัวเลือกในดรอปดาวน์
56
- data = load_data()
57
- offices = ["ทั้งหมด"] + sorted(data['สำนักงาน'].dropna().unique().tolist())
58
- ministries = ["ทั้งหมด"] + sorted(data['กระทรวง'].dropna().unique().tolist())
59
-
60
- # การตั้งค่าอินเตอร์เฟซ Gradio
61
- with gr.Blocks() as demo:
62
- gr.Markdown("# Data Viewer and Filter")
63
-
64
- column_selector = gr.CheckboxGroup(
65
- choices=['ชื่อโครงการ', 'งบประมาณ', 'วัตถุประสงค์', 'สถานที่ดำเนินการ',
66
- 'ระยะเวลาดำเนินการ', 'สำนักงาน', 'กระทรวง',
67
- 'Citizen Service & Doing Business', 'Digital Government',
68
- 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime',
69
- 'Learning Center/Museum/Environmental/Forest'],
70
- label="เลือกคอลัมน์ที่ต้องการแสดง")
71
-
72
- budget_order = gr.Radio(["น้อยไปมาก", "มากไปน้อย", "ไม่เรียงลำดับ"], label="เรียงลำดับงบประมาณ")
73
-
74
- office_filter = gr.Dropdown(choices=offices, label="เลือกสำนักงาน")
75
- ministry_filter = gr.Dropdown(choices=ministries, label="เลือกกระทรวง")
76
-
77
- yn_filter_labels = [
78
- 'Citizen Service & Doing Business', 'Digital Government',
79
- 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime',
80
- 'Learning Center/Museum/Environmental/Forest'
81
- ]
82
- yn_filters = {}
83
- for label in yn_filter_labels:
84
- yn_filters[label] = gr.Radio(["ไม่กรอง", "Y", "N"], label=label, value="ไม่กรอง")
85
-
86
- submit_button = gr.Button("Filter Data")
87
- output_data = gr.Dataframe(label="ผลลัพธ์ข้อมูลที่กรองแล้ว")
88
-
89
- submit_button.click(
90
- filter_data,
91
- inputs=[column_selector, budget_order, office_filter, ministry_filter] + list(yn_filters.values()),
92
- outputs=output_data
93
- )
94
-
95
- # รันแอปพลิเคชัน Gradio
96
- demo.launch()
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+
4
+ # โหลดข้อมูลจากไฟล์ Excel ที่อยู่ในโค้ด
5
+ def load_data():
6
+ # แทนที่ "path_to_your_file.xlsx" ด้วยพาธไฟล์ Excel ของคุณ
7
+ data = pd.read_excel("./BT-Engage-ขาวคาดแดง.xlsx")
8
+ return data
9
+
10
+ # ฟังก์ชันสำหรับกรองข้อมูลพร้อมจัดการกับข้อผิดพลาด
11
+ def filter_data(columns, budget_order, office_filter, ministry_filter,
12
+ citizen_service, digital_government, data_service, ai, cloud_security,
13
+ cybercrime, learning_center):
14
+
15
+ # โหลดข้อมูล
16
+ data = load_data()
17
+
18
+ # เลือกคอลัมน์ที่จะใช้
19
+ if columns:
20
+ data = data[columns]
21
+
22
+ # เรียงลำดับงบประมาณ
23
+ if budget_order == 'น้อยไปมาก':
24
+ data = data.sort_values('งบประมาณ', ascending=True)
25
+ elif budget_order == 'มากไปน้อย':
26
+ data = data.sort_values('งบประมาณ', ascending=False)
27
+
28
+ # กรองสำนักงาน
29
+ if office_filter != "ทั้งหมด":
30
+ data = data[data['สำนักงาน'] == office_filter]
31
+
32
+ # กรองกระทรวง
33
+ if ministry_filter != "ทั้งหมด":
34
+ data = data[data['กระทรวง'] == ministry_filter]
35
+
36
+ # ฟิลเตอร์สำหรับคอลัมน์ Y/N โดยตรวจสอบการมีอยู่ของคอลัมน์และค่าที่กรองได้
37
+ yn_columns = {
38
+ 'Citizen Service & Doing Business': citizen_service,
39
+ 'Digital Government': digital_government,
40
+ 'Data Service': data_service,
41
+ 'AI': ai,
42
+ 'Cloud & Security': cloud_security,
43
+ 'Cybercrime': cybercrime,
44
+ 'Learning Center/Museum/Environmental/Forest': learning_center
45
+ }
46
+
47
+ for col, yn_filter in yn_columns.items():
48
+ if yn_filter != "ไม่กรอง":
49
+ # ตรวจสอบว่าคอลัมน์มีอยู่ในข้อมูลหรือไม่และฟิลเตอร์ค่า Y หรือ N
50
+ if col in data.columns:
51
+ data = data[data[col].fillna('') == yn_filter]
52
+
53
+ return data
54
+
55
+ # โหลดข้อมูลสำหรับตัวเลือกในดรอปดาวน์
56
+ data = load_data()
57
+ offices = ["ทั้งหมด"] + sorted(data['สำนักงาน'].dropna().unique().tolist())
58
+ ministries = ["ทั้งหมด"] + sorted(data['กระทรวง'].dropna().unique().tolist())
59
+
60
+ # การตั้งค่าอินเตอร์เฟซ Gradio
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("# Data Viewer and Filter")
63
+
64
+ column_selector = gr.CheckboxGroup(
65
+ choices=['ชื่อโครงการ', 'งบประมาณ', 'วัตถุประสงค์', 'สถานที่ดำเนินการ',
66
+ 'ระยะเวลาดำเนินการ', 'สำนักงาน', 'กระทรวง',
67
+ 'Citizen Service & Doing Business', 'Digital Government',
68
+ 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime',
69
+ 'Learning Center/Museum/Environmental/Forest'],
70
+ label="เลือกคอลัมน์ที่ต้องการแสดง")
71
+
72
+ budget_order = gr.Radio(["น้อยไปมาก", "มากไปน้อย", "ไม่เรียงลำดับ"], label="เรียงลำดับงบประมาณ")
73
+
74
+ office_filter = gr.Dropdown(choices=offices, label="เลือกสำนักงาน")
75
+ ministry_filter = gr.Dropdown(choices=ministries, label="เลือกกระทรวง")
76
+
77
+ yn_filter_labels = [
78
+ 'Citizen Service & Doing Business', 'Digital Government',
79
+ 'Data Service', 'AI', 'Cloud & Security', 'Cybercrime',
80
+ 'Learning Center/Museum/Environmental/Forest'
81
+ ]
82
+ yn_filters = {}
83
+ for label in yn_filter_labels:
84
+ yn_filters[label] = gr.Radio(["ไม่กรอง", "Y", "N"], label=label, value="ไม่กรอง")
85
+
86
+ submit_button = gr.Button("Filter Data")
87
+ output_data = gr.Dataframe(label="ผลลัพธ์ข้อมูลที่กรองแล้ว")
88
+
89
+ submit_button.click(
90
+ filter_data,
91
+ inputs=[column_selector, budget_order, office_filter, ministry_filter] + list(yn_filters.values()),
92
+ outputs=output_data
93
+ )
94
+
95
+ # รันแอปพลิเคชัน Gradio
96
+ demo.launch()