Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import plotly.express as px | |
# Title of the App | |
st.title("Smart Data Visualization App") | |
# File Upload Section | |
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"]) | |
if uploaded_file: | |
try: | |
# Load the Excel file into a DataFrame | |
df = pd.read_excel(uploaded_file) | |
st.write("Data Preview:") | |
st.dataframe(df) | |
# User selects columns to visualize | |
x_axis = st.selectbox("Select X-axis column", df.columns) | |
y_axis = st.multiselect("Select Y-axis column(s)", [col for col in df.columns if col != x_axis]) | |
if x_axis and y_axis: | |
# Determine the best type of graph | |
if df[x_axis].nunique() < len(df[x_axis]) * 0.5: | |
# If the X-axis is categorical, choose a bar chart | |
chart_type = "Bar Chart" | |
fig = px.bar(df, x=x_axis, y=y_axis, barmode="group") | |
elif len(y_axis) == 1: | |
# For single numerical column, plot a line chart | |
chart_type = "Line Chart" | |
fig = px.line(df, x=x_axis, y=y_axis[0]) | |
else: | |
# If multiple numerical columns are selected, use a scatter plot matrix | |
chart_type = "Scatter Matrix" | |
fig = px.scatter_matrix(df, dimensions=y_axis, color=x_axis) | |
# Display the selected chart type and plot | |
st.write(f"Automatically Selected Chart Type: {chart_type}") | |
st.plotly_chart(fig) | |
else: | |
st.info("Please select columns for the X and Y axes to visualize.") | |
except Exception as e: | |
st.error(f"Error processing the file: {e}") | |
else: | |
st.info("Upload an Excel file to get started.") | |
# Footer | |
st.write("---") | |
st.write("Powered by [Streamlit](https://streamlit.io) and [Plotly](https://plotly.com).") | |