graphmaker / app.py
akazmi's picture
Update app.py
9cbb15c verified
raw
history blame
2.72 kB
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
# Title of the App
st.title("Excel Data Visualization with Dual Y-Axis")
# Upload Section
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])
if uploaded_file:
try:
# Read the Excel file into a DataFrame
df = pd.read_excel(uploaded_file)
# Preview the uploaded data
st.write("Data Preview:")
st.dataframe(df)
# Ensure required columns are present
required_columns = ["Description", "Oct'24", "Nov'24", "Dec/(Inc)"]
if not all(column in df.columns for column in required_columns):
st.error(f"The uploaded file must contain the following columns: {', '.join(required_columns)}")
else:
# Plot the Graph
fig = go.Figure()
# Add Bar Traces for Oct'24 and Nov'24
fig.add_trace(go.Bar(
x=df["Description"],
y=df["Oct'24"],
name="Oct'24",
marker_color='blue'
))
fig.add_trace(go.Bar(
x=df["Description"],
y=df["Nov'24"],
name="Nov'24",
marker_color='green'
))
# Add Line Trace for Dec/(Inc) on Secondary Y-axis
fig.add_trace(go.Scatter(
x=df["Description"],
y=df["Dec/(Inc)"],
name="Dec/(Inc)",
mode="lines+markers",
marker=dict(color='red'),
yaxis="y2"
))
# Update Layout for Dual Y-axis
fig.update_layout(
title="Dual Axis Chart",
xaxis=dict(title="Description"),
yaxis=dict(
title="Values (Oct'24 and Nov'24)",
titlefont=dict(color="blue"),
tickfont=dict(color="blue"),
),
yaxis2=dict(
title="Dec/(Inc)",
titlefont=dict(color="red"),
tickfont=dict(color="red"),
overlaying="y",
side="right"
),
barmode="group",
legend=dict(x=0.5, y=1.2, orientation="h"),
template="plotly_white"
)
# Render the Plot
st.plotly_chart(fig)
except Exception as e:
st.error(f"Error processing the file: {e}")
else:
st.info("Please upload an Excel file to get started.")
# Footer
st.write("---")
st.write("Developed using [Streamlit](https://streamlit.io) and [Hugging Face Spaces](https://huggingface.co/spaces).")