Spaces:
Runtime error
Runtime error
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() | |