-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (58 loc) · 2.56 KB
/
app.py
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
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import base64
from io import StringIO, BytesIO
# Function to generate Excel download link
def generate_excel_download_link(df):
towrite = BytesIO()
df.to_excel(towrite, index=False, header=True) # Removed the encoding argument
towrite.seek(0)
b64 = base64.b64encode(towrite.read()).decode()
href = f'<a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}" download="data_download.xlsx">📥 Download Excel File 📊</a>'
return st.markdown(href, unsafe_allow_html=True)
# Function to generate HTML download link for the plot
def generate_html_download_link(fig):
towrite = StringIO()
fig.write_html(towrite, include_plotlyjs="cdn")
towrite = BytesIO(towrite.getvalue().encode())
b64 = base64.b64encode(towrite.read()).decode()
href = f'<a href="data:text/html;charset=utf-8;base64, {b64}" download="plot.html">📥 Download Plot 📉</a>'
return st.markdown(href, unsafe_allow_html=True)
# Set page configuration
st.set_page_config(page_title='Enhanced Excel Plotter', layout='wide', initial_sidebar_state='expanded')
# Sidebar
st.sidebar.title("About 📘")
st.sidebar.info("This app allows you to upload an Excel file, visualize its data, and download the visualized data. 🚀")
# Main content
st.title('Enhanced Excel Plotter 📈')
st.subheader('Upload your Excel file and let the magic happen! ✨')
uploaded_file = st.file_uploader('Choose a XLSX file 📁', type='xlsx')
if uploaded_file:
st.markdown('---')
df = pd.read_excel(uploaded_file, engine='openpyxl')
st.dataframe(df.style.highlight_max(axis=0)) # Highlight max values for better visualization
st.markdown('### Data Analysis 🔍')
groupby_column = st.selectbox(
'Select a column for analysis 📊:',
('Ship Mode', 'Segment', 'Category', 'Sub-Category'),
)
# Group DataFrame
output_columns = ['Sales', 'Profit']
df_grouped = df.groupby(by=[groupby_column], as_index=False)[output_columns].sum()
# Plot DataFrame
fig = px.bar(
df_grouped,
x=groupby_column,
y='Sales',
color='Profit',
color_continuous_scale=['red', 'yellow', 'green'],
template='plotly_dark', # Dark theme for the plot
title=f'Sales & Profit by {groupby_column}'
)
st.plotly_chart(fig, use_container_width=True)
# Download Section
st.markdown('### Downloads 📥')
generate_excel_download_link(df_grouped)
generate_html_download_link(fig)