Spaces:
Running
Running
Commit
·
0266b2a
1
Parent(s):
11289e4
價格顯示優化2
Browse files
app.py
CHANGED
@@ -3,6 +3,14 @@ import requests
|
|
3 |
import datetime
|
4 |
import pandas as pd
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Function to fetch data from the API
|
7 |
def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
8 |
base_url = "https://pcc.mlwmlw.org/api/date/tender/"
|
@@ -12,19 +20,20 @@ def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
|
12 |
date = datetime.datetime.now().strftime("%Y-%m-%d") # Default to today's date
|
13 |
else:
|
14 |
datetime.datetime.strptime(date, "%Y-%m-%d") # Ensure correct format
|
|
|
15 |
url = f"{base_url}{date}"
|
16 |
-
|
17 |
response = requests.get(url)
|
18 |
response.raise_for_status()
|
19 |
data = response.json()
|
20 |
-
|
|
|
21 |
filtered_data = [
|
22 |
{
|
23 |
"標案名稱": f'<a href="{item.get("url", "#")}" target="_blank">{item.get("name", "N/A")}</a>',
|
24 |
"機關名稱": item.get("unit", "N/A"),
|
25 |
"類別": item.get("category", "N/A"),
|
26 |
"招標方式": item.get("type", "N/A"),
|
27 |
-
"價格":
|
28 |
"日期": date,
|
29 |
"連結": item.get("url", "N/A")
|
30 |
}
|
@@ -38,14 +47,13 @@ def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
|
38 |
(not name or name in item.get("name", ""))
|
39 |
)
|
40 |
]
|
|
|
41 |
return pd.DataFrame(filtered_data) if filtered_data else pd.DataFrame([{"查無資料": ""}])
|
42 |
-
|
43 |
except ValueError:
|
44 |
return pd.DataFrame([{"Error": "日期格式錯誤,請使用 YYYY-MM-DD 格式"}])
|
45 |
except requests.exceptions.RequestException as e:
|
46 |
return pd.DataFrame([{"Error": f"無法取得資料: {str(e)}"}])
|
47 |
|
48 |
-
|
49 |
# Gradio Interface
|
50 |
def create_interface():
|
51 |
with gr.Blocks() as demo:
|
@@ -60,46 +68,48 @@ def create_interface():
|
|
60 |
)
|
61 |
type_dropdown = gr.Dropdown(
|
62 |
choices=[
|
63 |
-
"不限", "公開取得報價單或企劃書更正公告", "公開招標公告", "公開招標更正公告",
|
|
|
|
|
64 |
],
|
65 |
label="招標方式",
|
66 |
value="不限"
|
67 |
)
|
68 |
-
|
69 |
with gr.Row():
|
70 |
unit_name_input = gr.Text(label="機關名稱", placeholder="輸入機關名稱")
|
71 |
unit_id_input = gr.Text(label="機關代碼", placeholder="輸入機關代碼")
|
72 |
job_number_input = gr.Text(label="標案案號", placeholder="輸入標案案號")
|
73 |
name_input = gr.Text(label="標案名稱", placeholder="輸入標案名稱")
|
74 |
-
|
75 |
submit_button = gr.Button("查詢")
|
76 |
download_button = gr.Button("導出 CSV")
|
77 |
-
|
78 |
# Output for displaying results
|
79 |
csv_data = gr.State() # Temporary state to store DataFrame
|
80 |
output = gr.HTML(label="查詢結果")
|
81 |
-
|
82 |
-
|
83 |
# Handle fetch and display
|
84 |
def handle_query(date, category, type_, unit_name, unit_id, job_number, name):
|
85 |
df = fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name)
|
86 |
return df.to_html(escape=False, index=False), df
|
87 |
-
|
88 |
# Handle CSV export
|
89 |
def export_csv(df):
|
90 |
file_path = "/tmp/tender_results.csv"
|
91 |
df.to_csv(file_path, index=False)
|
92 |
return file_path
|
93 |
-
|
94 |
# Button actions
|
95 |
submit_button.click(
|
96 |
handle_query,
|
97 |
inputs=[date_input, category_dropdown, type_dropdown,
|
98 |
-
|
99 |
outputs=[output, csv_data]
|
100 |
)
|
|
|
101 |
download_button.click(export_csv, inputs=[csv_data], outputs=gr.File())
|
102 |
-
|
103 |
demo.launch()
|
104 |
|
105 |
if __name__ == "__main__":
|
|
|
3 |
import datetime
|
4 |
import pandas as pd
|
5 |
|
6 |
+
def format_price(price):
|
7 |
+
try:
|
8 |
+
if isinstance(price, (int, float)):
|
9 |
+
return "{:,}".format(int(price))
|
10 |
+
return price
|
11 |
+
except:
|
12 |
+
return price
|
13 |
+
|
14 |
# Function to fetch data from the API
|
15 |
def fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name):
|
16 |
base_url = "https://pcc.mlwmlw.org/api/date/tender/"
|
|
|
20 |
date = datetime.datetime.now().strftime("%Y-%m-%d") # Default to today's date
|
21 |
else:
|
22 |
datetime.datetime.strptime(date, "%Y-%m-%d") # Ensure correct format
|
23 |
+
|
24 |
url = f"{base_url}{date}"
|
|
|
25 |
response = requests.get(url)
|
26 |
response.raise_for_status()
|
27 |
data = response.json()
|
28 |
+
|
29 |
+
# Filter data based on inputs
|
30 |
filtered_data = [
|
31 |
{
|
32 |
"標案名稱": f'<a href="{item.get("url", "#")}" target="_blank">{item.get("name", "N/A")}</a>',
|
33 |
"機關名稱": item.get("unit", "N/A"),
|
34 |
"類別": item.get("category", "N/A"),
|
35 |
"招標方式": item.get("type", "N/A"),
|
36 |
+
"價格": format_price(item.get("price", "N/A")),
|
37 |
"日期": date,
|
38 |
"連結": item.get("url", "N/A")
|
39 |
}
|
|
|
47 |
(not name or name in item.get("name", ""))
|
48 |
)
|
49 |
]
|
50 |
+
|
51 |
return pd.DataFrame(filtered_data) if filtered_data else pd.DataFrame([{"查無資料": ""}])
|
|
|
52 |
except ValueError:
|
53 |
return pd.DataFrame([{"Error": "日期格式錯誤,請使用 YYYY-MM-DD 格式"}])
|
54 |
except requests.exceptions.RequestException as e:
|
55 |
return pd.DataFrame([{"Error": f"無法取得資料: {str(e)}"}])
|
56 |
|
|
|
57 |
# Gradio Interface
|
58 |
def create_interface():
|
59 |
with gr.Blocks() as demo:
|
|
|
68 |
)
|
69 |
type_dropdown = gr.Dropdown(
|
70 |
choices=[
|
71 |
+
"不限", "公開取得報價單或企劃書更正公告", "公開招標公告", "公開招標更正公告",
|
72 |
+
"限制性招標(經公開評選或公開徵求)公告", "限制性招標(經公開評選或公開徵求)更正公告",
|
73 |
+
"無法決標更正公告", "N/A"
|
74 |
],
|
75 |
label="招標方式",
|
76 |
value="不限"
|
77 |
)
|
78 |
+
|
79 |
with gr.Row():
|
80 |
unit_name_input = gr.Text(label="機關名稱", placeholder="輸入機關名稱")
|
81 |
unit_id_input = gr.Text(label="機關代碼", placeholder="輸入機關代碼")
|
82 |
job_number_input = gr.Text(label="標案案號", placeholder="輸入標案案號")
|
83 |
name_input = gr.Text(label="標案名稱", placeholder="輸入標案名稱")
|
84 |
+
|
85 |
submit_button = gr.Button("查詢")
|
86 |
download_button = gr.Button("導出 CSV")
|
87 |
+
|
88 |
# Output for displaying results
|
89 |
csv_data = gr.State() # Temporary state to store DataFrame
|
90 |
output = gr.HTML(label="查詢結果")
|
91 |
+
|
|
|
92 |
# Handle fetch and display
|
93 |
def handle_query(date, category, type_, unit_name, unit_id, job_number, name):
|
94 |
df = fetch_tenders(date, category, type_, unit_name, unit_id, job_number, name)
|
95 |
return df.to_html(escape=False, index=False), df
|
96 |
+
|
97 |
# Handle CSV export
|
98 |
def export_csv(df):
|
99 |
file_path = "/tmp/tender_results.csv"
|
100 |
df.to_csv(file_path, index=False)
|
101 |
return file_path
|
102 |
+
|
103 |
# Button actions
|
104 |
submit_button.click(
|
105 |
handle_query,
|
106 |
inputs=[date_input, category_dropdown, type_dropdown,
|
107 |
+
unit_name_input, unit_id_input, job_number_input, name_input],
|
108 |
outputs=[output, csv_data]
|
109 |
)
|
110 |
+
|
111 |
download_button.click(export_csv, inputs=[csv_data], outputs=gr.File())
|
112 |
+
|
113 |
demo.launch()
|
114 |
|
115 |
if __name__ == "__main__":
|