-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
224 lines (184 loc) · 7.67 KB
/
app.py
File metadata and controls
224 lines (184 loc) · 7.67 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# ----------------------------------------------- Relevant Librarires -----------------------------------------------
import streamlit as st
import plistlib
import pandas as pd
from bs4 import BeautifulSoup
from utils import divider_color, remove_emojis, order_processor, display_order, display_split
st.set_page_config(
page_title="Grocery Splitter",
page_icon="🧮",
layout="wide",
initial_sidebar_state="collapsed",
)
# Remove whitespace from the top of the page and sidebar
st.markdown(
"""
<style>
.block-container {
padding-top: 3rem;
padding-bottom: 3rem;
padding-left: 6vw;
padding-right: 6vw;
}
/* Print styles for full-width PDF */
@page {
margin: 0.5cm;
size: auto;
}
@media print {
* {
box-sizing: border-box !important;
}
html, body {
width: 100% !important;
margin: 0 !important;
padding: 0 !important;
font-size: 12px !important;
}
.block-container {
padding: 0 !important;
max-width: none !important;
width: 100% !important;
min-width: 0 !important;
}
.main, .main .block-container {
max-width: none !important;
width: 100% !important;
padding: 0 !important;
margin: 0 !important;
}
/* Hide Streamlit elements not needed in print */
header, footer, .stDeployButton,
[data-testid="stToolbar"],
[data-testid="stDecoration"],
[data-testid="stStatusWidget"],
[data-testid="stSidebar"],
.stButton button,
iframe,
[data-testid="stHeader"] {
display: none !important;
}
/* Make content full width */
.stApp, .main, [data-testid="stAppViewContainer"],
[data-testid="stMainBlockContainer"] {
width: 100% !important;
max-width: none !important;
min-width: 0 !important;
overflow: visible !important;
padding: 0 !important;
margin: 0 !important;
}
/* Preserve colors */
* {
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
color-adjust: exact !important;
}
/* Ensure columns don't break across pages */
.row-widget, .stHorizontalBlock {
page-break-inside: avoid;
}
/* Make columns flexible */
[data-testid="column"], .stColumn {
min-width: 0 !important;
flex: 1 1 auto !important;
width: auto !important;
}
/* Scale down wide content to fit */
[data-testid="stHorizontalBlock"] {
flex-wrap: nowrap !important;
width: 100% !important;
}
}
</style>
""",
unsafe_allow_html=True,
)
# ----------------------------------------------- Main Page -----------------------------------------------
st.subheader("Grocery Splitter", divider=divider_color)
st.markdown(
"""
<p style="font-size: 13px; font-weight: 400;">
Helping with the age-old problem of splitting grocery bills with friends and family.
</p>
""",
unsafe_allow_html=True,
)
st.markdown("<br/>", unsafe_allow_html=True)
# Create a friends tags
st.write(":material/group: Add your friends / family to split the bill with:")
raw_names = st.text_input("Enter the names separated by commas ( , )")
names = []
if raw_names:
st.markdown("<br/>", unsafe_allow_html=True)
names = [
remove_emojis(f).strip().capitalize()
for f in raw_names.split(",")]
st.write(f"Friends / Family found:")
for f in names:
st.write(f":material/person: {f}")
# Tabs with custom logos
if raw_names:
# check for duplicates
if len(names) != len(set(names)):
st.markdown("<br/>", unsafe_allow_html=True)
st.warning(
" Please remove duplicate names from the list. If you want to add a friend with the same name, please add an extra detail like a last name to differentiate them.",
icon=":material/warning:",
)
elif len(names) > 0:
st.markdown("<br/><br/>", unsafe_allow_html=True)
stores = [
"  ",
"  ",
]
store_choice = st.radio(
"Select the store to upload your order",
stores,
horizontal=True,
)
st.markdown("<br/>", unsafe_allow_html=True)
# File uploader
uploaded_file = st.file_uploader(
"Upload your file containing the orders from ASDA here",
type=["html", "webarchive"],
)
st.markdown("<br/>", unsafe_allow_html=True)
# Steps inside a toggle
with st.expander(" How to Download Your Order List", icon=":material/download:"):
st.divider()
st.write("Follow the steps below to download your order list from ASDA:")
steps = [
"Go to the stores website and log in to your account.",
"Navigate to the 'Orders' section.",
"Select the latest order you want to split.",
"Right-click on the page and select 'Save As'.",
"Save the file as 'Webpage, HTML only' or 'Web Archive'.",
"Upload the saved file using the file uploader below",
]
for i, step in enumerate(steps, start=1):
st.markdown(f" **Step {i}:** {step}")
if uploaded_file:
with st.spinner("Processing the uploaded file..."):
# if the file is a webarchive file
if uploaded_file.type == "application/x-webarchive":
webarchive = plistlib.load(uploaded_file)
data = webarchive.get("WebMainResource", {}).get("WebResourceData")
# Decode and print
html = data.decode("utf-8", errors="replace")
soup = BeautifulSoup(html, "html.parser")
else:
soup = BeautifulSoup(uploaded_file, "html.parser")
items = order_processor(store_choice, soup, stores)
if items:
items = pd.DataFrame(items)
# ignore duplicate items
items = items.drop_duplicates(subset=["name", "weight"], keep="first").reset_index(drop=True)
st.markdown("<br/><br/>", unsafe_allow_html=True)
split = display_order(items, names)
display_split(split, items)
else:
st.info(
" No items found. Please upload a valid order receipt.",
icon=":material/info:",
)