Spaces:
Runtime error
Runtime error
karthikeyan-r
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
import plotly.express as px
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
st.set_page_config(page_title="Dashboard", layout="wide", initial_sidebar_state="expanded")
|
8 |
+
|
9 |
+
st.header("Scrapper Dashboard")
|
10 |
+
st.sidebar.subheader("Parameters Dashboard")
|
11 |
+
|
12 |
+
def create_plot(column, row, chart_type, df,brandcolumn):
|
13 |
+
fig = None
|
14 |
+
Brand=brandcolumn
|
15 |
+
if chart_type == "bar":
|
16 |
+
fig = px.bar(df, x=column, y=row, color=f"{Brand}", title=f"{row} by {column}")
|
17 |
+
elif chart_type == "scatter":
|
18 |
+
fig = px.scatter(df, x=column, y=row, color=f"{Brand}", title=f"{row} by {column}")
|
19 |
+
elif chart_type == "line":
|
20 |
+
fig = px.line(df, x=column, y=row,color=f"{Brand}", title=f"{row} by {column}")
|
21 |
+
elif chart_type == "histogram":
|
22 |
+
fig = px.histogram(df, x=column,color=f"{Brand}", title=f"Distribution of {column}")
|
23 |
+
elif chart_type == "pie":
|
24 |
+
brand_counts = df[f"{column}"].value_counts()
|
25 |
+
distribute = px.pie(brand_counts,color=f"{Brand}", values=brand_counts.values, names=brand_counts.index,title=f"Distribution of Mobile Phone {column}")
|
26 |
+
fig=distribute.update_traces(hoverinfo="label+percent", textinfo="label+percent+value", hole=.4)
|
27 |
+
elif chart_type == "series":
|
28 |
+
fig=px.line(df, x=column, y=row,color=f"{Brand}", title="Series Chart")
|
29 |
+
elif chart_type == "heatmap":
|
30 |
+
fig = px.density_heatmap(df, x=column, y=row,color=f"{Brand}", marginal_x="histogram", marginal_y="histogram", color_continuous_scale=px.colors.sequential.PuBu, title=f"Heatmap for {column} vs Price")
|
31 |
+
elif chart_type == "bubble":
|
32 |
+
fig = px.scatter(df, x=column, y=row,color=f"{Brand}", size="Battery",title=f"Bubble chart for {column}")
|
33 |
+
elif chart_type == "box":
|
34 |
+
fig = px.box(df, x=column, y=row,color=f"{Brand}", title=f"Box plot for {row} by {column}")
|
35 |
+
elif chart_type == "violin":
|
36 |
+
fig = px.violin(df, x=column, y=row,color=f"{Brand}",title=f"Violin plot for {row} by {column}")
|
37 |
+
elif chart_type == "funnel":
|
38 |
+
fig = px.funnel(df, x=column, y=row,color=f"{Brand}", title=f"Funnel chart for {column} and {row}")
|
39 |
+
else:
|
40 |
+
st.error("Invalid chart type")
|
41 |
+
return fig
|
42 |
+
|
43 |
+
|
44 |
+
def display_dataframe(df):
|
45 |
+
st.subheader("Uploaded CSV :")
|
46 |
+
st.dataframe(df)
|
47 |
+
|
48 |
+
|
49 |
+
def display_analysis(df,selectedcolumns,brandcol):
|
50 |
+
st.header("Visualization")
|
51 |
+
#columns = df.columns.tolist()
|
52 |
+
columns=selectedcolumns
|
53 |
+
chart_types = [
|
54 |
+
"bar", "scatter", "line", "histogram", "pie",
|
55 |
+
"series", "heatmap", "box", "violin", "funnel"
|
56 |
+
]
|
57 |
+
|
58 |
+
st.sidebar.subheader("Select Parameters for Plots: ")
|
59 |
+
x = st.sidebar.selectbox("Select a x", columns)
|
60 |
+
y = st.sidebar.selectbox("Select a y", columns)
|
61 |
+
chart_type = st.selectbox("Select a chart type", chart_types)
|
62 |
+
|
63 |
+
|
64 |
+
#st.subheader(f"{chart_type.title()} chart for {x} and {y}")
|
65 |
+
fig = create_plot(x, y, chart_type, df,brandcol)
|
66 |
+
st.plotly_chart(fig)
|
67 |
+
|
68 |
+
|
69 |
+
def main():
|
70 |
+
# Sidebar related code
|
71 |
+
st.sidebar.header("Select a file")
|
72 |
+
|
73 |
+
files = os.listdir(".")
|
74 |
+
csv_files = [file for file in files if file.endswith(".csv")]
|
75 |
+
|
76 |
+
if len(csv_files) == 0:
|
77 |
+
st.sidebar.write("No CSV files found in current directory. Please upload a CSV file.")
|
78 |
+
selected_file = st.sidebar.file_uploader("Upload CSV file", type=['csv'])
|
79 |
+
else:
|
80 |
+
csv_files = ["None"] + csv_files
|
81 |
+
selected_file = st.sidebar.selectbox("Select a file", csv_files)
|
82 |
+
|
83 |
+
if selected_file=='None':
|
84 |
+
st.write('Please Select your or Upload File.')
|
85 |
+
elif selected_file:
|
86 |
+
site = ["None", "Flipkart", "Amazon","Both"]
|
87 |
+
siteselect = st.sidebar.selectbox("Select options :", site)
|
88 |
+
df = pd.read_csv(selected_file)
|
89 |
+
flipkart_cols = [col for col in df.columns if col.startswith('Flipkart')]
|
90 |
+
amazon_cols = [col for col in df.columns if col.startswith('Amazon')]
|
91 |
+
if siteselect == "None":
|
92 |
+
st.write('DataFrame')
|
93 |
+
display_dataframe(df)
|
94 |
+
st.write('Select Site')
|
95 |
+
elif siteselect == 'Flipkart':
|
96 |
+
viewpoint = ["None", "Show DataFrame", "Show Analysis"]
|
97 |
+
dataset_show = st.sidebar.selectbox("Select options :", viewpoint)
|
98 |
+
if dataset_show == "Show DataFrame":
|
99 |
+
flipkart_data = df[flipkart_cols]
|
100 |
+
display_dataframe(flipkart_data)
|
101 |
+
elif dataset_show == "Show Analysis":
|
102 |
+
display_analysis(df,flipkart_cols,'FlipkartBrand')
|
103 |
+
else:
|
104 |
+
st.sidebar.write("Please select a CSV file.")
|
105 |
+
elif siteselect == 'Amazon':
|
106 |
+
viewpoints = ["None", "Show DataFrame", "Show Analysis"]
|
107 |
+
dataset_shows = st.sidebar.selectbox("Select options :", viewpoints)
|
108 |
+
if dataset_shows == "Show DataFrame":
|
109 |
+
amazon_data = df[amazon_cols]
|
110 |
+
display_dataframe(amazon_data)
|
111 |
+
elif dataset_shows == "Show Analysis":
|
112 |
+
display_analysis(df,amazon_cols,'AmazonBrand')
|
113 |
+
else:
|
114 |
+
st.sidebar.write("Please select a options.")
|
115 |
+
elif siteselect == 'Both':
|
116 |
+
viewpoin = ["None", "Show DataFrame", "Show Analysis"]
|
117 |
+
dataset_sho = st.sidebar.selectbox("Select options :", viewpoin)
|
118 |
+
if dataset_sho == "Show DataFrame":
|
119 |
+
display_dataframe(df)
|
120 |
+
elif dataset_sho == "Show Analysis":
|
121 |
+
columns = df.columns.tolist()
|
122 |
+
display_analysis(df,columns,'FlipkartBrand')
|
123 |
+
else:
|
124 |
+
st.sidebar.write("Please select a options.")
|
125 |
+
else:
|
126 |
+
st.sidebar.write("Please select a CSV file.")
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
main()
|