harikrishnad1997 commited on
Commit
03371ed
1 Parent(s): fb3a99d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -44
app.py CHANGED
@@ -5,53 +5,40 @@ import matplotlib.pyplot as plt
5
  from io import StringIO
6
 
7
  # Function to plot histogram
8
- def plot_histogram(file_contents, column):
9
  # Read the CSV file
10
  custom_df = pd.read_csv(StringIO(file_contents))
11
 
12
  # Plot histogram
13
- plt.figure(figsize=(8, 6))
14
- sns.histplot(custom_df[column])
15
- plt.title(f'Histogram for {column}')
16
- plt.xlabel(column)
17
- plt.ylabel('Frequency')
18
- return plt
19
 
20
  # Function to plot scatter plot
21
- def plot_scatter(file_contents, x_axis, y_axis):
22
  # Read the CSV file
23
  custom_df = pd.read_csv(StringIO(file_contents))
24
 
25
  # Plot scatter plot
26
- plt.figure(figsize=(8, 6))
27
- sns.scatterplot(x=x_axis, y=y_axis, data=custom_df)
28
- plt.title(f'Scatter Plot ({x_axis} vs {y_axis})')
29
- plt.xlabel(x_axis)
30
- plt.ylabel(y_axis)
31
- return plt
32
-
33
- def layout_fn(inputs):
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
 
@@ -60,32 +47,31 @@ def layout_fn(inputs):
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
-
79
- @interface.update # Use update method instead of component_args
80
- def update_choices(inputs):
81
- if inputs.file:
82
- data = pd.read_csv(StringIO(inputs.file.getvalue()))
83
  choices = list(data.columns)
84
- interface.update( # Update interface options using update
85
- text=gr.components.Dropdown(choices=choices),
86
- text_1=gr.components.Dropdown(choices=choices),
87
- text_2=gr.components.Dropdown(choices=choices),
 
 
 
88
  )
89
 
90
-
91
- interface.launch(share=True,fn_change=update_choices)
 
5
  from io import StringIO
6
 
7
  # Function to plot histogram
8
+ def plot_histogram(file_contents, column, ax=None):
9
  # Read the CSV file
10
  custom_df = pd.read_csv(StringIO(file_contents))
11
 
12
  # Plot histogram
13
+ sns.histplot(custom_df[column], ax=ax)
14
+ ax.set_title(f'Histogram for {column}')
15
+ ax.set_xlabel(column)
16
+ ax.set_ylabel('Frequency')
 
 
17
 
18
  # Function to plot scatter plot
19
+ def plot_scatter(file_contents, x_axis, y_axis, ax=None):
20
  # Read the CSV file
21
  custom_df = pd.read_csv(StringIO(file_contents))
22
 
23
  # Plot scatter plot
24
+ sns.scatterplot(x=x_axis, y=y_axis, data=custom_df, ax=ax)
25
+ ax.set_title(f'Scatter Plot ({x_axis} vs {y_axis})')
26
+ ax.set_xlabel(x_axis)
27
+ ax.set_ylabel(y_axis)
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ def layout_fn(file, text, text_1, text_2):
30
  # Create the figure with subplots
31
  fig, axes = plt.subplots(1, 2, figsize=(16, 6))
32
 
33
  # Check if data is uploaded and a column is selected for histogram
34
+ if file and text:
35
+ plot_histogram(file.getvalue(), text, ax=axes[0])
36
  else:
37
  axes[0].text(0.5, 0.5, "Upload a CSV and select a column", ha='center', va='center')
38
 
39
  # Check if data is uploaded and both x and y columns are selected for scatter plot
40
+ if file and text_1 and text_2:
41
+ plot_scatter(file.getvalue(), text_1, text_2, ax=axes[1])
42
  else:
43
  axes[1].text(0.5, 0.5, "Upload a CSV, select X and Y columns", ha='center', va='center')
44
 
 
47
  plt.tight_layout()
48
  return fig
49
 
 
50
  # Create the Gradio interface
51
  interface = gr.Interface(
52
  fn=layout_fn,
53
  inputs=[
54
+ gr.inputs.File(label="Upload CSV file"),
55
+ gr.inputs.Dropdown(label="Select Column (Histogram)", choices=[]),
56
+ gr.inputs.Dropdown(label="Select X-axis (Scatter)", choices=[]),
57
+ gr.inputs.Dropdown(label="Select Y-axis (Scatter)", choices=[]),
58
  ],
59
  outputs="plot",
60
  title="Data Visualization Tool",
61
  description="Upload a CSV file, select columns for histogram and scatter plots.",
62
  )
63
 
64
+ def update_choices(file):
65
+ if file:
66
+ data = pd.read_csv(StringIO(file.getvalue()))
 
 
67
  choices = list(data.columns)
68
+ interface.set_config(
69
+ inputs=[
70
+ gr.inputs.File(label="Upload CSV file"),
71
+ gr.inputs.Dropdown(label="Select Column (Histogram)", choices=choices),
72
+ gr.inputs.Dropdown(label="Select X-axis (Scatter)", choices=choices),
73
+ gr.inputs.Dropdown(label="Select Y-axis (Scatter)", choices=choices),
74
+ ]
75
  )
76
 
77
+ interface.run(share=True,fn_change=update_choices)