harikrishnad1997 commited on
Commit
71fc0ff
·
verified ·
1 Parent(s): 157b60b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -16
app.py CHANGED
@@ -30,23 +30,65 @@ def plot_scatter(file_contents, x_axis, y_axis):
30
  plt.ylabel(y_axis)
31
  return plt
32
 
33
- iface_histogram = gr.Interface(
34
- fn=plot_histogram,
35
- inputs=[gr.components.File(label="Upload CSV file"), gr.components.Text()],
36
- outputs="plot",
37
- title="Histogram Plotter",
38
- description="Upload a CSV file and select a column to plot its histogram.",
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # Create the Gradio interface for scatter plot
42
- iface_scatter = gr.Interface(
43
- fn=plot_scatter,
44
- inputs=[gr.components.File(label="Upload CSV file"), gr.components.Text(), gr.components.Text()],
 
 
 
 
 
 
 
 
 
 
 
45
  outputs="plot",
46
- title="Scatter Plotter",
47
- description="Upload a CSV file and select X and Y columns to plot a scatter plot.",
48
  )
49
 
50
- # Launch the Gradio interfaces
51
- iface_histogram.launch(share=True)
52
- iface_scatter.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  plt.ylabel(y_axis)
31
  return plt
32
 
33
+ def layout_fn(inputs, outputs):
34
+ # Get the data from uploaded file
35
+ data = pd.read_csv(StringIO(inputs.file))
36
+
37
+ # Get selected columns from dropdown options
38
+ column_options = list(data.columns)
39
+ selected_column = inputs.text if inputs.text else None
40
+ selected_x_axis = inputs.text_1 if inputs.text_1 else None
41
+ selected_y_axis = inputs.text_2 if inputs.text_2 else None
42
+
43
+ # Create the figure with subplots
44
+ fig, axes = plt.subplots(1, 2, figsize=(16, 6))
45
+
46
+ # Check if data is uploaded and a column is selected for histogram
47
+ if inputs.file and selected_column:
48
+ plot_histogram(inputs.file.getvalue(), selected_column, ax=axes[0])
49
+ else:
50
+ axes[0].text(0.5, 0.5, "Upload a CSV and select a column", ha='center', va='center')
51
+
52
+ # Check if data is uploaded and both x and y columns are selected for scatter plot
53
+ if inputs.file and selected_x_axis and selected_y_axis:
54
+ plot_scatter(inputs.file.getvalue(), selected_x_axis, selected_y_axis, ax=axes[1])
55
+ else:
56
+ axes[1].text(0.5, 0.5, "Upload a CSV, select X and Y columns", ha='center', va='center')
57
 
58
+ # Adjust layout
59
+ fig.suptitle("Data Visualization")
60
+ plt.tight_layout()
61
+ return fig
62
+
63
+
64
+ # Create the Gradio interface
65
+ interface = gr.Interface(
66
+ fn=layout_fn,
67
+ inputs=[
68
+ gr.components.File(label="Upload CSV file"),
69
+ gr.components.Dropdown(label="Select Column (Histogram)", choices="infer"),
70
+ gr.components.Dropdown(label="Select X-axis (Scatter)", choices="infer"),
71
+ gr.components.Dropdown(label="Select Y-axis (Scatter)", choices="infer"),
72
+ ],
73
  outputs="plot",
74
+ title="Data Visualization Tool",
75
+ description="Upload a CSV file, select columns for histogram and scatter plots.",
76
  )
77
 
78
+ # Set dropdown options dynamically based on uploaded data
79
+ interface.component_args["text"] = {"choices": None}
80
+ interface.component_args["text_1"] = {"choices": None}
81
+ interface.component_args["text_2"] = {"choices": None}
82
+
83
+
84
+ @interface.update(enabled=False)
85
+ def update_choices(file):
86
+ if file:
87
+ data = pd.read_csv(StringIO(file.getvalue()))
88
+ choices = list(data.columns)
89
+ interface.component_args["text"] = {"choices": choices}
90
+ interface.component_args["text_1"] = {"choices": choices}
91
+ interface.component_args["text_2"] = {"choices": choices}
92
+
93
+
94
+ interface.launch(share=True)