malcolmSQ commited on
Commit
ea9f896
·
1 Parent(s): fa68af3

Add download functionality to dashboard tables

Browse files
Files changed (1) hide show
  1. dashboard/app.py +80 -4
dashboard/app.py CHANGED
@@ -434,13 +434,21 @@ This dashboard visualizes these values annually over the project duration, provi
434
  # --- Tabbed tables section ---
435
  with gr.Tabs():
436
  with gr.Tab("Project Results (Annual)"):
437
- results_box = gr.Dataframe(label="Project Results (Annual)")
 
 
438
  with gr.Tab("Species Results (Annual)"):
439
- species_box = gr.Dataframe(label="Species Results (Annual)")
 
 
440
  with gr.Tab("Surviving Trees Table"):
441
- survival_box = gr.Dataframe(label="Surviving Trees Table")
 
 
442
  with gr.Tab("Biomass Table"):
443
- biomass_debug_table = gr.Dataframe(label="Biomass Table (inputs & outputs per year)")
 
 
444
  # --- End tabbed tables ---
445
  # Update the update_declining_increment callback to use these new inputs
446
  def update_declining_increment(y1, y2, y3, y4, y5,
@@ -491,6 +499,74 @@ This dashboard visualizes these values annually over the project duration, provi
491
  *mort_vars, mort_sub, soil_carbon],
492
  outputs=[carbon_plot, biomass_plot, annual_plot, growth_plot, summary_box, results_box, species_box, survival_box, biomass_debug_table]
493
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
  # Show initial results
495
  c, b, a, g, summary, r, s, surv, biomass_debug_df = update_declining_increment(
496
  year_1_default, year_2_default, year_3_default, year_4_default, year_5_default,
 
434
  # --- Tabbed tables section ---
435
  with gr.Tabs():
436
  with gr.Tab("Project Results (Annual)"):
437
+ with gr.Row():
438
+ results_box = gr.Dataframe(label="Project Results (Annual)")
439
+ download_results_btn = gr.Button("📥 Download CSV", variant="secondary", scale=0.2)
440
  with gr.Tab("Species Results (Annual)"):
441
+ with gr.Row():
442
+ species_box = gr.Dataframe(label="Species Results (Annual)")
443
+ download_species_btn = gr.Button("📥 Download CSV", variant="secondary", scale=0.2)
444
  with gr.Tab("Surviving Trees Table"):
445
+ with gr.Row():
446
+ survival_box = gr.Dataframe(label="Surviving Trees Table")
447
+ download_survival_btn = gr.Button("📥 Download CSV", variant="secondary", scale=0.2)
448
  with gr.Tab("Biomass Table"):
449
+ with gr.Row():
450
+ biomass_debug_table = gr.Dataframe(label="Biomass Table (inputs & outputs per year)")
451
+ download_biomass_btn = gr.Button("📥 Download CSV", variant="secondary", scale=0.2)
452
  # --- End tabbed tables ---
453
  # Update the update_declining_increment callback to use these new inputs
454
  def update_declining_increment(y1, y2, y3, y4, y5,
 
499
  *mort_vars, mort_sub, soil_carbon],
500
  outputs=[carbon_plot, biomass_plot, annual_plot, growth_plot, summary_box, results_box, species_box, survival_box, biomass_debug_table]
501
  )
502
+
503
+ # Download functionality for tables
504
+ def create_csv_from_df(df):
505
+ if isinstance(df, str):
506
+ # Convert string representation of dataframe back to dataframe
507
+ import ast
508
+ try:
509
+ data = ast.literal_eval(df)
510
+ df = pd.DataFrame(data)
511
+ except:
512
+ return None
513
+ elif hasattr(df, '_list_display') and hasattr(df, 'columns'):
514
+ # Convert gradio dataframe to pandas dataframe
515
+ df = pd.DataFrame(df._list_display, columns=df.columns)
516
+ return df
517
+
518
+ def download_results_csv(df):
519
+ df = create_csv_from_df(df)
520
+ if df is None:
521
+ return None
522
+ csv_data = df.to_csv(index=False)
523
+ return (csv_data, "project_results.csv")
524
+
525
+ def download_species_csv(df):
526
+ df = create_csv_from_df(df)
527
+ if df is None:
528
+ return None
529
+ csv_data = df.to_csv(index=False)
530
+ return (csv_data, "species_results.csv")
531
+
532
+ def download_survival_csv(df):
533
+ df = create_csv_from_df(df)
534
+ if df is None:
535
+ return None
536
+ csv_data = df.to_csv(index=False)
537
+ return (csv_data, "surviving_trees.csv")
538
+
539
+ def download_biomass_csv(df):
540
+ df = create_csv_from_df(df)
541
+ if df is None:
542
+ return None
543
+ csv_data = df.to_csv(index=False)
544
+ return (csv_data, "biomass_table.csv")
545
+
546
+ download_results_btn.click(
547
+ download_results_csv,
548
+ inputs=[results_box],
549
+ outputs=[gr.File(label="Download Project Results", file_count=1, interactive=False)]
550
+ )
551
+
552
+ download_species_btn.click(
553
+ download_species_csv,
554
+ inputs=[species_box],
555
+ outputs=[gr.File(label="Download Species Results", file_count=1, interactive=False)]
556
+ )
557
+
558
+ download_survival_btn.click(
559
+ download_survival_csv,
560
+ inputs=[survival_box],
561
+ outputs=[gr.File(label="Download Surviving Trees", file_count=1, interactive=False)]
562
+ )
563
+
564
+ download_biomass_btn.click(
565
+ download_biomass_csv,
566
+ inputs=[biomass_debug_table],
567
+ outputs=[gr.File(label="Download Biomass Table", file_count=1, interactive=False)]
568
+ )
569
+
570
  # Show initial results
571
  c, b, a, g, summary, r, s, surv, biomass_debug_df = update_declining_increment(
572
  year_1_default, year_2_default, year_3_default, year_4_default, year_5_default,