Teoman21 commited on
Commit
cd4ad46
·
1 Parent(s): 138d311

feat: Add plot export functinality

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -50,6 +50,7 @@ from visualizations import (
50
  create_distribution_plot,
51
  create_scatter_plot,
52
  create_time_series_plot,
 
53
  )
54
 
55
 
@@ -79,6 +80,7 @@ DEFAULT_STATE = {
79
  "column_types": None,
80
  "filter_meta": None,
81
  "source_name": None,
 
82
  }
83
 
84
 
@@ -360,13 +362,14 @@ def _generate_chart(
360
  scatter_x: Optional[str],
361
  scatter_y: Optional[str],
362
  scatter_color: Optional[str],
363
- ) -> Tuple[Any, Any, str]:
364
  """Create a visualization based on user selections."""
365
  state = _ensure_state(state)
366
  try:
367
  df = _current_dataframe(state, filtered=True)
368
  except ValueError as exc:
369
- return None, None, f"⚠️ {exc}"
 
370
 
371
  try:
372
  if chart_type == "Time Series":
@@ -390,9 +393,11 @@ def _generate_chart(
390
  else:
391
  raise ValueError("Unsupported chart type.")
392
  except ValueError as exc:
393
- return None, None, f"⚠️ {exc}"
 
394
 
395
- return fig, "Visualization generated."
 
396
 
397
 
398
  def _download_filtered(state) -> str:
@@ -408,8 +413,10 @@ def _download_filtered(state) -> str:
408
  return temp.name
409
 
410
 
411
- def _download_chart(fig: Any) -> str:
412
  """Export the most recent chart to PNG."""
 
 
413
  if fig is None:
414
  raise ValueError("Generate a visualization before exporting.")
415
  buffer = figure_to_png_bytes(fig)
@@ -551,8 +558,11 @@ def create_dashboard():
551
  scatter_y_column = gr.Dropdown(label="Y Axis", choices=[])
552
  scatter_color_column = gr.Dropdown(label="Color (optional)", choices=[])
553
 
554
- generate_chart_button = gr.Button("Generate Visualization", variant="primary")
 
 
555
  chart_output = gr.Plot(label="Visualization")
 
556
 
557
 
558
  with gr.Tab("Insights"):
@@ -744,7 +754,13 @@ def create_dashboard():
744
  scatter_y_column,
745
  scatter_color_column,
746
  ],
747
- outputs=[chart_output, viz_status],
 
 
 
 
 
 
748
  )
749
 
750
 
 
50
  create_distribution_plot,
51
  create_scatter_plot,
52
  create_time_series_plot,
53
+ figure_to_png_bytes,
54
  )
55
 
56
 
 
80
  "column_types": None,
81
  "filter_meta": None,
82
  "source_name": None,
83
+ "current_figure": None,
84
  }
85
 
86
 
 
362
  scatter_x: Optional[str],
363
  scatter_y: Optional[str],
364
  scatter_color: Optional[str],
365
+ ) -> Tuple[Dict[str, Any], Any, str]:
366
  """Create a visualization based on user selections."""
367
  state = _ensure_state(state)
368
  try:
369
  df = _current_dataframe(state, filtered=True)
370
  except ValueError as exc:
371
+ state["current_figure"] = None
372
+ return state, None, f"⚠️ {exc}"
373
 
374
  try:
375
  if chart_type == "Time Series":
 
393
  else:
394
  raise ValueError("Unsupported chart type.")
395
  except ValueError as exc:
396
+ state["current_figure"] = None
397
+ return state, None, f"⚠️ {exc}"
398
 
399
+ state["current_figure"] = fig
400
+ return state, fig, "✅ Visualization generated. Use 'Export Chart' to download."
401
 
402
 
403
  def _download_filtered(state) -> str:
 
413
  return temp.name
414
 
415
 
416
+ def _download_chart(state) -> str:
417
  """Export the most recent chart to PNG."""
418
+ state = _ensure_state(state)
419
+ fig = state.get("current_figure")
420
  if fig is None:
421
  raise ValueError("Generate a visualization before exporting.")
422
  buffer = figure_to_png_bytes(fig)
 
558
  scatter_y_column = gr.Dropdown(label="Y Axis", choices=[])
559
  scatter_color_column = gr.Dropdown(label="Color (optional)", choices=[])
560
 
561
+ with gr.Row():
562
+ generate_chart_button = gr.Button("Generate Visualization", variant="primary")
563
+ export_chart_button = gr.Button("Export Chart (PNG)", variant="secondary")
564
  chart_output = gr.Plot(label="Visualization")
565
+ export_chart_file = gr.File(label="Exported Chart", interactive=False)
566
 
567
 
568
  with gr.Tab("Insights"):
 
754
  scatter_y_column,
755
  scatter_color_column,
756
  ],
757
+ outputs=[dataset_state, chart_output, viz_status],
758
+ )
759
+
760
+ export_chart_button.click(
761
+ fn=_download_chart,
762
+ inputs=[dataset_state],
763
+ outputs=[export_chart_file],
764
  )
765
 
766