Spaces:
Runtime error
Runtime error
harikrishnad1997
commited on
Update app.py
Browse files
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 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
outputs="plot",
|
46 |
-
title="
|
47 |
-
description="Upload a CSV file
|
48 |
)
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|