Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import plotly.
|
| 4 |
|
| 5 |
# Title of the App
|
| 6 |
-
st.title("
|
| 7 |
|
| 8 |
-
# Upload Section
|
| 9 |
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])
|
| 10 |
|
| 11 |
if uploaded_file:
|
| 12 |
try:
|
| 13 |
-
#
|
| 14 |
df = pd.read_excel(uploaded_file)
|
| 15 |
-
|
| 16 |
-
# Preview the uploaded data
|
| 17 |
st.write("Data Preview:")
|
| 18 |
st.dataframe(df)
|
| 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 |
-
# Add Line Trace for Dec/(Inc) on Secondary Y-axis
|
| 44 |
-
fig.add_trace(go.Scatter(
|
| 45 |
-
x=df["Description"],
|
| 46 |
-
y=df["Dec/(Inc)"],
|
| 47 |
-
name="Dec/(Inc)",
|
| 48 |
-
mode="lines+markers",
|
| 49 |
-
marker=dict(color='red'),
|
| 50 |
-
yaxis="y2"
|
| 51 |
-
))
|
| 52 |
-
|
| 53 |
-
# Update Layout for Dual Y-axis
|
| 54 |
-
fig.update_layout(
|
| 55 |
-
title="Dual Axis Chart",
|
| 56 |
-
xaxis=dict(title="Description"),
|
| 57 |
-
yaxis=dict(
|
| 58 |
-
title="Values (Oct'24 and Nov'24)",
|
| 59 |
-
titlefont=dict(color="blue"),
|
| 60 |
-
tickfont=dict(color="blue"),
|
| 61 |
-
),
|
| 62 |
-
yaxis2=dict(
|
| 63 |
-
title="Dec/(Inc)",
|
| 64 |
-
titlefont=dict(color="red"),
|
| 65 |
-
tickfont=dict(color="red"),
|
| 66 |
-
overlaying="y",
|
| 67 |
-
side="right"
|
| 68 |
-
),
|
| 69 |
-
barmode="group",
|
| 70 |
-
legend=dict(x=0.5, y=1.2, orientation="h"),
|
| 71 |
-
template="plotly_white"
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
# Render the Plot
|
| 75 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
st.error(f"Error processing the file: {e}")
|
| 78 |
else:
|
| 79 |
-
st.info("
|
| 80 |
|
| 81 |
# Footer
|
| 82 |
st.write("---")
|
| 83 |
-
st.write("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
|
| 5 |
# Title of the App
|
| 6 |
+
st.title("Smart Data Visualization App")
|
| 7 |
|
| 8 |
+
# File Upload Section
|
| 9 |
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])
|
| 10 |
|
| 11 |
if uploaded_file:
|
| 12 |
try:
|
| 13 |
+
# Load the Excel file into a DataFrame
|
| 14 |
df = pd.read_excel(uploaded_file)
|
|
|
|
|
|
|
| 15 |
st.write("Data Preview:")
|
| 16 |
st.dataframe(df)
|
| 17 |
|
| 18 |
+
# User selects columns to visualize
|
| 19 |
+
x_axis = st.selectbox("Select X-axis column", df.columns)
|
| 20 |
+
y_axis = st.multiselect("Select Y-axis column(s)", [col for col in df.columns if col != x_axis])
|
| 21 |
+
|
| 22 |
+
if x_axis and y_axis:
|
| 23 |
+
# Determine the best type of graph
|
| 24 |
+
if df[x_axis].nunique() < len(df[x_axis]) * 0.5:
|
| 25 |
+
# If the X-axis is categorical, choose a bar chart
|
| 26 |
+
chart_type = "Bar Chart"
|
| 27 |
+
fig = px.bar(df, x=x_axis, y=y_axis, barmode="group")
|
| 28 |
+
elif len(y_axis) == 1:
|
| 29 |
+
# For single numerical column, plot a line chart
|
| 30 |
+
chart_type = "Line Chart"
|
| 31 |
+
fig = px.line(df, x=x_axis, y=y_axis[0])
|
| 32 |
+
else:
|
| 33 |
+
# If multiple numerical columns are selected, use a scatter plot matrix
|
| 34 |
+
chart_type = "Scatter Matrix"
|
| 35 |
+
fig = px.scatter_matrix(df, dimensions=y_axis, color=x_axis)
|
| 36 |
+
|
| 37 |
+
# Display the selected chart type and plot
|
| 38 |
+
st.write(f"Automatically Selected Chart Type: {chart_type}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
st.plotly_chart(fig)
|
| 40 |
+
else:
|
| 41 |
+
st.info("Please select columns for the X and Y axes to visualize.")
|
| 42 |
except Exception as e:
|
| 43 |
st.error(f"Error processing the file: {e}")
|
| 44 |
else:
|
| 45 |
+
st.info("Upload an Excel file to get started.")
|
| 46 |
|
| 47 |
# Footer
|
| 48 |
st.write("---")
|
| 49 |
+
st.write("Powered by [Streamlit](https://streamlit.io) and [Plotly](https://plotly.com).")
|