manikantachary18 commited on
Commit
9937af7
·
verified ·
1 Parent(s): 4464909

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -15,7 +15,7 @@ def load_data(file):
15
  elif file.name.endswith(".json"):
16
  return pd.read_json(file.name)
17
  except Exception as e:
18
- print("Load error:", e)
19
  return pd.DataFrame()
20
 
21
  def load_url(url):
@@ -37,30 +37,39 @@ def process_inputs(file, url):
37
  df = load_url(url) if url else load_data(file)
38
  if df.empty:
39
  return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
 
40
  df_global = df
41
-
42
  all_cols = list(df.columns)
43
  num_cols = list(df.select_dtypes(include="number").columns)
44
 
 
 
 
45
  default_x = all_cols[0] if all_cols else None
46
- default_y = num_cols[0] if num_cols else None
47
 
48
  return (
49
  df.head(),
50
  gr.update(choices=all_cols, value=default_x),
51
- gr.update(choices=num_cols, value=default_y)
52
  )
53
 
54
  def smart_update(chart_type, x, y):
55
  global df_global
 
 
56
  if df_global.empty:
57
- return None, gr.Textbox.update(value="No data loaded", visible=True)
58
 
59
  try:
60
  if chart_type == "Summary":
61
  summary = df_global.describe(include="all").to_string()
62
  return None, gr.Textbox.update(value=summary, visible=True)
63
- elif chart_type == "Bar Chart":
 
 
 
 
64
  return px.bar(df_global, x=x, y=y), gr.Textbox.update(visible=False)
65
  elif chart_type == "Line Chart":
66
  return px.line(df_global, x=x, y=y), gr.Textbox.update(visible=False)
@@ -70,12 +79,15 @@ def smart_update(chart_type, x, y):
70
  return px.pie(df_global, names=x, values=y), gr.Textbox.update(visible=False)
71
  elif chart_type == "Box Plot":
72
  return px.box(df_global, x=x, y=y), gr.Textbox.update(visible=False)
 
 
 
73
  except Exception as e:
74
- print("Plot error:", e)
75
  return None, gr.Textbox.update(value=str(e), visible=True)
76
 
77
  with gr.Blocks() as demo:
78
- gr.Markdown("## 📊 Data Visualizer with Summary")
79
 
80
  with gr.Row():
81
  file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload File")
@@ -84,17 +96,20 @@ with gr.Blocks() as demo:
84
  df_preview = gr.Dataframe(label="Preview", interactive=False)
85
  x_dropdown = gr.Dropdown(label="X-axis")
86
  y_dropdown = gr.Dropdown(label="Y-axis")
87
- chart_type = gr.Dropdown(["Summary", "Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"], label="Chart Type")
88
-
 
 
89
  generate_button = gr.Button("Generate")
90
 
91
  plot_area = gr.Plot(label="Chart", visible=True)
92
  summary_box = gr.Textbox(label="Summary", visible=False, lines=15)
93
 
94
- # Auto-load on file or URL change
95
  file_input.change(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
96
  url_input.change(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
97
 
 
98
  generate_button.click(
99
  smart_update,
100
  inputs=[chart_type, x_dropdown, y_dropdown],
 
15
  elif file.name.endswith(".json"):
16
  return pd.read_json(file.name)
17
  except Exception as e:
18
+ print("File load error:", e)
19
  return pd.DataFrame()
20
 
21
  def load_url(url):
 
37
  df = load_url(url) if url else load_data(file)
38
  if df.empty:
39
  return pd.DataFrame(), gr.update(choices=[], value=None), gr.update(choices=[], value=None)
40
+
41
  df_global = df
 
42
  all_cols = list(df.columns)
43
  num_cols = list(df.select_dtypes(include="number").columns)
44
 
45
+ print("Columns in data:", all_cols)
46
+ print("Numeric columns:", num_cols)
47
+
48
  default_x = all_cols[0] if all_cols else None
49
+ default_y = num_cols[0] if num_cols else (all_cols[0] if all_cols else None)
50
 
51
  return (
52
  df.head(),
53
  gr.update(choices=all_cols, value=default_x),
54
+ gr.update(choices=num_cols if num_cols else all_cols, value=default_y)
55
  )
56
 
57
  def smart_update(chart_type, x, y):
58
  global df_global
59
+ print(f"Selected chart_type: {chart_type}, X: {x}, Y: {y}")
60
+
61
  if df_global.empty:
62
+ return None, gr.Textbox.update(value="No data loaded.", visible=True)
63
 
64
  try:
65
  if chart_type == "Summary":
66
  summary = df_global.describe(include="all").to_string()
67
  return None, gr.Textbox.update(value=summary, visible=True)
68
+
69
+ if not x or not y:
70
+ return None, gr.Textbox.update(value="Please select both X and Y axes.", visible=True)
71
+
72
+ if chart_type == "Bar Chart":
73
  return px.bar(df_global, x=x, y=y), gr.Textbox.update(visible=False)
74
  elif chart_type == "Line Chart":
75
  return px.line(df_global, x=x, y=y), gr.Textbox.update(visible=False)
 
79
  return px.pie(df_global, names=x, values=y), gr.Textbox.update(visible=False)
80
  elif chart_type == "Box Plot":
81
  return px.box(df_global, x=x, y=y), gr.Textbox.update(visible=False)
82
+ else:
83
+ return None, gr.Textbox.update(value="Unsupported chart type.", visible=True)
84
+
85
  except Exception as e:
86
+ print("Plotting error:", e)
87
  return None, gr.Textbox.update(value=str(e), visible=True)
88
 
89
  with gr.Blocks() as demo:
90
+ gr.Markdown("## 📊 Data Visualizer with Chart & Summary")
91
 
92
  with gr.Row():
93
  file_input = gr.File(file_types=[".csv", ".xlsx", ".json"], label="Upload File")
 
96
  df_preview = gr.Dataframe(label="Preview", interactive=False)
97
  x_dropdown = gr.Dropdown(label="X-axis")
98
  y_dropdown = gr.Dropdown(label="Y-axis")
99
+ chart_type = gr.Dropdown(
100
+ ["Summary", "Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Box Plot"],
101
+ label="Chart Type"
102
+ )
103
  generate_button = gr.Button("Generate")
104
 
105
  plot_area = gr.Plot(label="Chart", visible=True)
106
  summary_box = gr.Textbox(label="Summary", visible=False, lines=15)
107
 
108
+ # Automatically load and populate dropdowns on upload or URL change
109
  file_input.change(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
110
  url_input.change(process_inputs, inputs=[file_input, url_input], outputs=[df_preview, x_dropdown, y_dropdown])
111
 
112
+ # Generate plot or summary
113
  generate_button.click(
114
  smart_update,
115
  inputs=[chart_type, x_dropdown, y_dropdown],