Spaces:
Sleeping
Sleeping
Update app.py
Browse files496 unmodified lines49749849950050150250350412 unmodified lines5175185195205215225231 unmodified line5255265275285295305315 unmodified lines5375385395405415425435 unmodified lines54955055155255355455511 unmodified lines56756856957057157257322 unmodified lines596597598599600601602603102 unmodified lines706707708709710711102 unmodified lines81481581681781881982010 unmodified lines831832833834835836837838839840841842843496 unmodified lineshover_data={'變化率': ':.1f%', '差異': ':,.0f'})fig.update_geos(fitbounds="locations", visible=False)# 修改處:增加 height=1000fig.update_layout(margin={"r":0,"t":50,"l":0,"b":0}, height=1000)return figexcept Exception as e: raise gr.Error(f"差異地圖產生失敗: {e}")12 unmodified linesmerged = merged.merge(data2, left_on='name', right_on='region_normalized', how='left', suffixes=('', '_2'))merged[col1] = merged[col1].fillna(0); merged[col2] = merged[col2].fillna(0)# 修改:使用分開的兩個子圖fig = make_subplots(rows=1, cols=2,specs=[[{"type": "choropleth"}, {"type": "choropleth"}]],1 unmodified linehorizontal_spacing=0.02)# 左側地圖fig.add_trace(go.Choropleth(geojson=eval(merged.geometry.to_json()),locations=merged.index,5 unmodified linesgeo="geo"), row=1, col=1)# 右側地圖fig.add_trace(go.Choropleth(geojson=eval(merged.geometry.to_json()),locations=merged.index,5 unmodified linesgeo="geo2"), row=1, col=2)# 分別設定兩個地圖的顯示區域 - 使用固定的台灣中心點和範圍fig.update_layout(geo=dict(visible=False,11 unmodified lines),title_text=f"🆚 雙指標地理分布對比 ({time})",margin={"r":50,"t":100,"l":50,"b":50},height=1200,showlegend=False)return fig22 unmodified linestitle=f"🗺️ {col} - 地理分布圖 ({time})", hover_name='name')fig.update_geos(fitbounds="locations", visible=False)# 修改處:增加 height=1000fig.update_layout(margin={"r":0,"t":50,"l":0,"b":0}, height=1000)return figexcept Exception as e: raise gr.Error(f"繪圖失敗: {e}")102 unmodified lines# 4. Gradio 介面# ===============================================custom_css = """body {font-family: 'Microsoft JhengHei', 'Noto Sans TC', sans-serif;102 unmodified lineswith gr.Column(scale=5):stats_output = gr.HTML(label="數據總覽", value="<div style='text-align: center; padding: 40px; color: #9ca3af;'>📊 上傳資料後將顯示統計摘要</div>")# 修改處:加入 id 供跳轉使用with gr.Tabs() as tabs:with gr.Tab("🗺️ 互動地圖", id="tab_map"): map_output = gr.Plot()with gr.Tab("🆚 雙指標對比", id="tab_dual"): dual_map_output = gr.Plot()10 unmodified linesfile_input.change(fn=process_uploaded_file, inputs=file_input, outputs=[current_df, status_box, col_selector, time_selector, trend_region, data_preview, gif_time_col, start_time, end_time, map_level_state, stats_year_selector, stats_output, comp_time1, comp_time2, col_selector_2, all_numeric_cols_state])stats_year_selector.change(fn=generate_stats_summary, inputs=[current_df, all_numeric_cols_state, stats_year_selector], outputs=stats_output)# 修改處:加入 .then() 進行分頁跳轉btn_map.click(fn=generate_map, inputs=[current_df, col_selector, time_selector, agg_method, color_scheme, map_level_state], outputs=map_output).then(lambda: gr.Tabs(selected="tab_map"), outputs=tabs)btn_export_map.click(fn=export_static_map, inputs=[current_df, col_selector, time_selector, agg_method, color_scheme, map_level_state], outputs=map_file_output)btn_trend.click(fn=generate_trend_chart, inputs=[current_df, col_selector, trend_region, line_metric], outputs=trend_output).then(lambda: gr.Tabs(selected="tab_trend"), outputs=tabs)btn_ranking.click(fn=generate_ranking, inputs=[current_df, col_selector, time_selector, top_n_slider, map_level_state], outputs=ranking_output).then(lambda: gr.Tabs(selected="tab_ranking"), outputs=tabs)btn_compare.click(fn=generate_comparison_map, inputs=[current_df, col_selector, comp_time1, comp_time2, map_level_state], outputs=compare_output).then(lambda: gr.Tabs(selected="tab_compare"), outputs=tabs)btn_dual.click(fn=generate_dual_map, inputs=[current_df, col_selector, col_selector_2, time_selector, map_level_state], outputs=dual_map_output).then(lambda: gr.Tabs(selected="tab_dual"), outputs=tabs)btn_gif.click(fn=generate_map_gif, inputs=[current_df, col_selector, color_scheme, fps_slider, gif_time_col, start_time, end_time, sort_order, map_level_state], outputs=gif_output).then(lambda: gr.Tabs(selected="tab_gif"), outputs=tabs)btn_export_csv.click(fn=export_csv, inputs=current_df, outputs=csv_output)496 unmodified lines49749849950050150250350412 unmodified lines5175185195205215221 unmodified line5245255265275285295 unmodified lines5355365375385395405 unmodified lines54654754854955055111 unmodified lines56356456556656756856957022 unmodified lines593594595596597598599600102 unmodified lines703704705706707708709710711712713714715716717102 unmodified lines82082182282382482510 unmodified lines836837838839840841842843844845846847848496 unmodified lineshover_data={'變化率': ':.1f%', '差異': ':,.0f'})fig.update_geos(fitbounds="locations", visible=False)# 修改處:增加 height=1000,並加入 autosize=Truefig.update_layout(margin={"r":0,"t":50,"l":0,"b":0}, height=1000, autosize=True)return figexcept Exception as e: raise gr.Error(f"差異地圖產生失敗: {e}")12 unmodified linesmerged = merged.merge(data2, left_on='name', right_on='region_normalized', how='left', suffixes=('', '_2'))merged[col1] = merged[col1].fillna(0); merged[col2] = merged[col2].fillna(0)fig = make_subplots(rows=1, cols=2,specs=[[{"type": "choropleth"}, {"type": "choropleth"}]],1 unmodified linehorizontal_spacing=0.02)fig.add_trace(go.Choropleth(geojson=eval(merged.geometry.to_json()),locations=merged.index,5 unmodified linesgeo="geo"), row=1, col=1)fig.add_trace(go.Choropleth(geojson=eval(merged.geometry.to_json()),locations=merged.index,5 unmodified linesgeo="geo2"), row=1, col=2)fig.update_layout(geo=dict(visible=False,11 unmodified lines),title_text=f"🆚 雙指標地理分布對比 ({time})",margin={"r":50,"t":100,"l":50,"b":50},height=1200, # 修改處:增加 height=1200autosize=True, # 修改處:加入 autosize=Trueshowlegend=False)return fig22 unmodified linestitle=f"🗺️ {col} - 地理分布圖 ({time})", hover_name='name')fig.update_geos(fitbounds="locations", visible=False)# 修改處:增加 height=1000,並加入 autosize=Truefig.update_layout(margin={"r":0,"t":50,"l":0,"b":0}, height=1000, autosize=True)return figexcept Exception as e: raise gr.Error(f"繪圖失敗: {e}")102 unmodified lines# 4. Gradio 介面# ===============================================# JavaScript: 強制觸發瀏覽器重繪,解決 Plotly 在隱藏分頁渲染時的大小問題resize_js = """() => {setTimeout(() => {window.dispatchEvent(new Event('resize'));}, 300);}"""custom_css = """body {font-family: 'Microsoft JhengHei', 'Noto Sans TC', sans-serif;102 unmodified lineswith gr.Column(scale=5):stats_output = gr.HTML(label="數據總覽", value="<div style='text-align: center; padding: 40px; color: #9ca3af;'>📊 上傳資料後將顯示統計摘要</div>")with gr.Tabs() as tabs:with gr.Tab("🗺️ 互動地圖", id="tab_map"): map_output = gr.Plot()with gr.Tab("🆚 雙指標對比", id="tab_dual"): dual_map_output = gr.Plot()10 unmodified linesfile_input.change(fn=process_uploaded_file, inputs=file_input, outputs=[current_df, status_box, col_selector, time_selector, trend_region, data_preview, gif_time_col, start_time, end_time, map_level_state, stats_year_selector, stats_output, comp_time1, comp_time2, col_selector_2, all_numeric_cols_state])stats_year_selector.change(fn=generate_stats_summary, inputs=[current_df, all_numeric_cols_state, stats_year_selector], outputs=stats_output)# 修改處:加入 js=resize_js 參數btn_map.click(fn=generate_map, inputs=[current_df, col_selector, time_selector, agg_method, color_scheme, map_level_state], outputs=map_output).then(lambda: gr.Tabs(selected="tab_map"), outputs=tabs, js=resize_js)btn_export_map.click(fn=export_static_map, inputs=[current_df, col_selector, time_selector, agg_method, color_scheme, map_level_state], outputs=map_file_output)btn_trend.click(fn=generate_trend_chart, inputs=[current_df, col_selector, trend_region, line_metric], outputs=trend_output).then(lambda: gr.Tabs(selected="tab_trend"), outputs=tabs, js=resize_js)btn_ranking.click(fn=generate_ranking, inputs=[current_df, col_selector, time_selector, top_n_slider, map_level_state], outputs=ranking_output).then(lambda: gr.Tabs(selected="tab_ranking"), outputs=tabs, js=resize_js)btn_compare.click(fn=generate_comparison_map, inputs=[current_df, col_selector, comp_time1, comp_time2, map_level_state], outputs=compare_output).then(lambda: gr.Tabs(selected="tab_compare"), outputs=tabs, js=resize_js)btn_dual.click(fn=generate_dual_map, inputs=[current_df, col_selector, col_selector_2, time_selector, map_level_state], outputs=dual_map_output).then(lambda: gr.Tabs(selected="tab_dual"), outputs=tabs, js=resize_js)btn_gif.click(fn=generate_map_gif, inputs=[current_df, col_selector, color_scheme, fps_slider, gif_time_col, start_time, end_time, sort_order, map_level_state], outputs=gif_output).then(lambda: gr.Tabs(selected="tab_gif"), outputs=tabs)btn_export_csv.click(fn=export_csv, inputs=current_df, outputs=csv_output)
app.py+21-16