Kilos1 commited on
Commit
e8be228
·
verified ·
1 Parent(s): e85a183

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import supabase
3
+ import pandas as pd
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+
8
+ client = supabase.create_client(
9
+ "https://tmjhrfjckqnlvqnsspnr.supabase.co",
10
+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InRtamhyZmpja3FubHZxbnNzcG5yIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjExMjE1NTgsImV4cCI6MjAzNjY5NzU1OH0.E34R6qPWavp2uRWKinZQICgdEqRjov46VnE38F24Al8"
11
+
12
+ )
13
+
14
+ def read_data():
15
+ response = client.table('Customer_purchase_dataset').select("*").execute()
16
+ df = pd.DataFrame(response.data)
17
+ return df
18
+
19
+ df= read_data()
20
+ #print(df.head)
21
+ #print(df.dtypes)
22
+
23
+ # Convert Gender to categorical
24
+ df['Gender'] = df['Gender'].map({0: 'Female', 1: 'Male'})
25
+
26
+ # Convert LoyaltyProgram to categorical
27
+ df['LoyaltyProgram'] = df['LoyaltyProgram'].map({0: 'No', 1: 'Yes'})
28
+
29
+ # Convert PurchaseStatus to categorical
30
+ df['PurchaseStatus'] = df['PurchaseStatus'].map({0: 'Not Purchased', 1: 'Purchased'})
31
+
32
+ # Function to create histogram
33
+ def create_histogram(column):
34
+ plt.figure(figsize=(10, 6))
35
+ sns.histplot(data=df, x=column, kde=True)
36
+ plt.title(f'Histogram of {column}')
37
+ plt.xlabel(column)
38
+ plt.ylabel('Count')
39
+ return plt
40
+
41
+ # Function to create scatter plot
42
+ def create_scatter(x_column, y_column, hue_column):
43
+ plt.figure(figsize=(10, 6))
44
+ sns.scatterplot(data=df, x=x_column, y=y_column, hue=hue_column)
45
+ plt.title(f'{x_column} vs {y_column} (colored by {hue_column})')
46
+ plt.xlabel(x_column)
47
+ plt.ylabel(y_column)
48
+ return plt
49
+
50
+ # Function to create box plot
51
+ def create_boxplot(x_column, y_column):
52
+ plt.figure(figsize=(10, 6))
53
+ sns.boxplot(data=df, x=x_column, y=y_column)
54
+ plt.title(f'Box Plot of {y_column} by {x_column}')
55
+ plt.xlabel(x_column)
56
+ plt.ylabel(y_column)
57
+ return plt
58
+
59
+ # Function to create bar plot
60
+ def create_barplot(x_column, y_column):
61
+ plt.figure(figsize=(10, 6))
62
+ sns.barplot(data=df, x=x_column, y=y_column)
63
+ plt.title(f'Bar Plot of {y_column} by {x_column}')
64
+ plt.xlabel(x_column)
65
+ plt.ylabel(y_column)
66
+ plt.xticks(rotation=45)
67
+ return plt
68
+
69
+ # Gradio interface
70
+ def visualize(plot_type, x_column, y_column, hue_column):
71
+ if plot_type == "Histogram":
72
+ return create_histogram(x_column)
73
+ elif plot_type == "Scatter Plot":
74
+ return create_scatter(x_column, y_column, hue_column)
75
+ elif plot_type == "Box Plot":
76
+ return create_boxplot(x_column, y_column)
77
+ elif plot_type == "Bar Plot":
78
+ return create_barplot(x_column, y_column)
79
+
80
+ # Create Gradio interface
81
+ iface = gr.Interface(
82
+ fn=visualize,
83
+ inputs=[
84
+ gr.Dropdown(["Histogram", "Scatter Plot", "Box Plot", "Bar Plot"], label="Plot Type"),
85
+ gr.Dropdown(df.columns.tolist(), label="X-axis"),
86
+ gr.Dropdown(df.columns.tolist(), label="Y-axis"),
87
+ gr.Dropdown(df.columns.tolist(), label="Hue (for Scatter Plot)")
88
+
89
+ ],
90
+ outputs="plot",
91
+ title="Customer Purchase Data Visualization Dashboard",
92
+ description="Explore the customer purchase dataset through various visualizations."
93
+ )
94
+
95
+ # Launch the interface
96
+ iface.launch()
97
+