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