-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (49 loc) · 2.03 KB
/
app.py
File metadata and controls
58 lines (49 loc) · 2.03 KB
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
import streamlit as st
import pandas as pd
import plotly.express as px
import base64
from io import StringIO, BytesIO
def generate_excel_download_link(df):
towrite = BytesIO()
df.to_excel(towrite, encoding="utf-8", index=False, header=True) # write to BytesIO buffer
towrite.seek(0) # reset pointer
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)
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)
st.set_page_config(page_title='Data Visuals')
st.title('Data Visuals 📈')
st.subheader('Enter your Excel file')
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)
groupby_column = st.selectbox(
'What would you like to analyse?',
('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_white',
title=f'<b>Sales & Profit by {groupby_column}</b>'
)
st.plotly_chart(fig)
# -- DOWNLOAD SECTION
st.subheader('Downloads:')
generate_excel_download_link(df_grouped)
generate_html_download_link(fig)