Spaces:
Runtime error
Runtime error
File size: 13,382 Bytes
c6a8375 9a58c6e 54b3a40 c6a8375 fa6c7f3 c6a8375 b4fc692 c6a8375 77c3e94 b68790d b4fc692 9bf9819 77c3e94 b68790d b4fc692 9bf9819 77c3e94 b68790d b4fc692 9bf9819 77c3e94 b68790d b4fc692 9bf9819 77c3e94 b68790d 14e895f 9bf9819 77c3e94 b68790d b4fc692 9bf9819 b4fc692 c6a8375 b4fc692 54b3a40 b4fc692 54b3a40 b4fc692 54b3a40 b4fc692 c6a8375 |
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import streamlit as st
import pandas as pd
from docxtpl import DocxTemplate, InlineImage
from docx.shared import Mm, Inches
import datetime
from datetime import timedelta, date
import io
# from utils import *
########## Title for the Web App ##########
st.title("Report Generator")
########## Create Input field ##########
# feedback = st.text_input('Type your text here', 'Customer suggested that the customer service needs to be improved and the response time needs to be improved.')
# if st.button('Click for predictions!'):
# with st.spinner('Generating predictions...'):
# topics_prob, sentiment_prob, touchpoint_prob = get_single_prediction(feedback)
# bar_topic = px.bar(topics_prob, x='probability', y='topic')
# bar_touchpoint = px.bar(touchpoint_prob, x='probability', y='touchpoint')
# pie = px.pie(sentiment_prob,
# values='probability',
# names='sentiment',
# color_discrete_map={'positive':'rgb(0, 204, 0)',
# 'negative':'rgb(215, 11, 11)'
# },
# color='sentiment'
# )
# st.plotly_chart(bar_topic, use_container_width=True)
# st.plotly_chart(bar_touchpoint, use_container_width=True)
# st.plotly_chart(pie, use_container_width=True)
# st.write("\n")
# st.subheader('Or... Upload a csv file if you have a file instead.')
# st.write("\n")
# st.download_button(
# label="Download sample file here",
# data=sample_file,
# file_name='sample_data.csv',
# mime='text/csv',
# )
uploaded_files = st.file_uploader("Upload multiple files", accept_multiple_files=True)
if len(uploaded_files) > 0:
# with st.spinner('Generating report...'):
for uploaded_file in uploaded_files:
if uploaded_file.name == 'Flip_accum.xlsx':
flip_accum1 = pd.read_excel(uploaded_file, skiprows=8, nrows=11, usecols="A:D")
flip_accum2 = pd.read_excel(uploaded_file, skiprows=24, nrows=5, usecols="A:N")
#st.write('flip_accum1: ' + str(flip_accum1.shape))
elif uploaded_file.name == 'Fold_accum.xlsx':
fold_accum1 = pd.read_excel(uploaded_file, skiprows=8, nrows=11, usecols="A:D")
fold_accum2 = pd.read_excel(uploaded_file, skiprows=24, nrows=5, usecols="A:N")
#st.write('fold_accum1: ' + str(fold_accum1.shape))
elif uploaded_file.name == 'Flip_today.xlsx':
flip_today1 = pd.read_excel(uploaded_file, skiprows=8, nrows=11, usecols="A:D")
flip_today2 = pd.read_excel(uploaded_file, skiprows=24, nrows=5, usecols="A:M")
#st.write('flip_today1: ' + str(flip_today1.shape))
elif uploaded_file.name == 'Fold_today.xlsx':
fold_today1 = pd.read_excel(uploaded_file, skiprows=8, nrows=11, usecols="A:D")
fold_today2 = pd.read_excel(uploaded_file, skiprows=24, nrows=5, usecols="A:M")
#st.write('fold_today1: ' + str(fold_today1.shape))
elif uploaded_file.name == 'FlipFold4_accum.xlsx':
flipfold_accum = pd.read_excel(uploaded_file, skiprows=8, nrows=11, usecols="A:D")
flipfold_accum2 = pd.read_excel(uploaded_file, skiprows=24, nrows=5, usecols="A:N")
#st.write('flipfold_accum2: ' + str(flipfold_accum2.shape))
elif uploaded_file.name == 'FlipFold4_analysis.xlsx':
flipfold = pd.read_excel(uploaded_file, skiprows=9)
#st.write('flipfold: ' + str(flipfold.shape))
elif uploaded_file.name == 'flipfold4_report_template.docx':
doc = DocxTemplate(uploaded_file)
if datetime.datetime.now().day == 1:
day_suffix = "st"
elif datetime.datetime.now().day == 2:
day_suffix = "nd"
elif datetime.datetime.now().day == 3:
day_suffix = "rd"
else:
day_suffix = "th"
if round(((flipfold_accum2.iloc[2, 4] - flipfold_accum2.iloc[2, 13])/flipfold_accum2.iloc[2, 13]) * 100) < 0:
increase_decrease = "Decrease"
else:
increase_decrease = "Increase"
flipfold = flipfold[['Symptom\nGroup 1', 'Subsidiary', 'Marketing Name']]
flipfold.columns = ['symptom', 'subsidiary', 'Marketing Name']
display = ['Display', 'Touch', 'OCTA/Backglass Broken', 'Sensor']
quick_discharge = ['Quick Discharge', 'Charging', 'Discharging']
appearance = ['Appearance', 'Case', 'Button']
others = ['In Process', 'WIFI', 'Connection', 'S pen', 'Fault Operation', 'Bluetooth']
flipfold['symptom'] = flipfold['symptom'].apply(lambda x:
'Display' if x in display else
'Quick Discharge' if x in quick_discharge else
'Appearance' if x in appearance else
'Others' if x in others else
'Sound/Call Audio' if x == 'Sound/Call audio' else
x
)
template = pd.DataFrame({
'symptom': ['Total', 'Heat', 'Display', 'Camera', 'Quick Discharge', 'Power', 'Rebooting', 'App/SW', 'Sound/Call Audio', 'Appearance', 'Others'],
'SEAO Total': [0]*11,
'SAVINA': [0]*11,
'SEAU': [0]*11,
'SEIN': [0]*11,
'SENZ': [0]*11,
'SEPCO': [0]*11,
'SESP': [0]*11,
'SME': [0]*11,
'TSE': [0]*11
}).set_index('symptom')
flip4 = flipfold[flipfold['Marketing Name'] == 'Galaxy Z Flip4']
flip4_groupby = pd.DataFrame(flip4.groupby(['symptom', 'subsidiary'])['subsidiary'].count())
flip4_groupby.columns=['count']
flip4_groupby.reset_index(inplace=True)
flip4_groupby = flip4_groupby.pivot(index='symptom', columns='subsidiary', values='count').fillna(0)
fold4 = flipfold[flipfold['Marketing Name'] == 'Galaxy Z Fold4']
fold4_groupby = pd.DataFrame(fold4.groupby(['symptom', 'subsidiary'])['subsidiary'].count())
fold4_groupby.columns=['count']
fold4_groupby.reset_index(inplace=True)
fold4_groupby = fold4_groupby.pivot(index='symptom', columns='subsidiary', values='count').fillna(0)
template_flip4 = template.copy()
template_fold4 = template.copy()
for col in template.columns:
for row in template.index:
try:
template_flip4.loc[row, col] = flip4_groupby.loc[row, col]
except:
continue
for col in template.columns:
for row in template.index:
try:
template_fold4.loc[row, col] = fold4_groupby.loc[row, col]
except:
continue
# Account for SEPCO data entry error
template_flip4.loc['Display', 'SEPCO'] = template_flip4.loc['Display', 'SEPCO'] - 4
template_flip4.loc['App/SW', 'SEPCO'] = template_flip4.loc['App/SW', 'SEPCO'] - 2
template_flip4.loc['Others', 'SEPCO'] = template_flip4.loc['Others', 'SEPCO'] + 6
# Account for SEVT into SAVINA count
template_fold4.loc['Display', 'SAVINA'] = template_fold4.loc['Display', 'SAVINA'] + 5
template_fold4.loc['Others', 'SAVINA'] = template_fold4.loc['Others', 'SAVINA'] + 2
template_fold4.loc['Rebooting', 'SAVINA'] = template_fold4.loc['Rebooting', 'SAVINA'] + 1
template_fold4.loc['Appearance', 'SAVINA'] = template_fold4.loc['Appearance', 'SAVINA'] + 1
template_flip4.loc['Appearance', 'SAVINA'] = template_flip4.loc['Appearance', 'SAVINA'] + 1
template_flip4.loc['Others', 'SAVINA'] = template_flip4.loc['Others', 'SAVINA'] + 2
template_flip4['SEAO Total'] = template_flip4[['SAVINA', 'SEAU', 'SEIN', 'SENZ', 'SEPCO', 'SESP', 'SME', 'TSE']].sum(axis=1)
template_flip4.loc['Total'] = template_flip4[['SEAO Total', 'SAVINA', 'SEAU', 'SEIN', 'SENZ', 'SEPCO', 'SESP', 'SME', 'TSE']].sum(axis=0)
template_flip4 = template_flip4.astype(int)
template_fold4['SEAO Total'] = template_fold4[['SEAO Total', 'SAVINA', 'SEAU', 'SEIN', 'SENZ', 'SEPCO', 'SESP', 'SME', 'TSE']].sum(axis=1)
template_fold4.loc['Total'] = template_fold4[['SEAO Total', 'SAVINA', 'SEAU', 'SEIN', 'SENZ', 'SEPCO', 'SESP', 'SME', 'TSE']].sum(axis=0)
template_fold4 = template_fold4.astype(int)
flip4_dict = {'a' + str(i): template_flip4.values.flatten()[i-1] for i in range(1,100)}
fold4_dict = {'b' + str(i): template_fold4.values.flatten()[i-1] for i in range(1,100)}
context = {
#"topleft": topleft,
#"topright": topright,
#"bottomleft": bottomleft,
#"bottomright": bottomright,
"date0" : int((datetime.date.today() - date(2022, 9, 2))/ timedelta(days=1) + 1),
"date1" : datetime.datetime.now().strftime("%#d.%#m.%Y"),
"date2" : (datetime.datetime.now() - timedelta(days=1)).strftime("%#d/%#m"),
"date3": datetime.datetime.now().strftime("%b.%#d"),
"day_suffix": day_suffix,
"v2": "{:>6}".format(f'{flip_today1.iloc[5, 2] + fold_today1.iloc[5, 2]:,}'),
"v3": f'{int(flip_accum1.iloc[3, 2]):,}',
"v4": f'{int(flip_accum1.iloc[5, 2]):,}',
"v5": f'{int(flip_accum1.iloc[7, 2]):,}',
"v6": f'{int(flip_accum2.iloc[1, 12]):,}',
"v7": f'{int(flip_accum2.iloc[1, 5]):,}',
"v8": f'{int(flip_accum2.iloc[1, 6]):,}',
"v9": f'{int(fold_accum1.iloc[3, 2]):,}',
"v10": f'{int(fold_accum1.iloc[5, 2]):,}',
"v11": f'{int(fold_accum1.iloc[7, 2]):,}',
"v12": f'{int(fold_accum2.iloc[1, 12]):,}',
"v13": f'{int(fold_accum2.iloc[1, 5]):,}',
"v14": f'{int(fold_accum2.iloc[1, 6]):,}',
# "v21": f'{int(flip_today1.iloc[3, 2]):,}', # changed on 5 Sep 2022
# "v22": f'{int(flip_today1.iloc[5, 2]):,}',
# "v23": f'{int(flip_today1.iloc[7, 2]):,}',
"v21": f'{int(flip_today2.iloc[1, 12]):,}',
"v22": f'{int(flip_today2.iloc[1, 5]):,}',
"v23": f'{int(flip_today2.iloc[1, 6]):,}',
"v24": f'{int(fold_today2.iloc[1, 12]):,}',
"v25": f'{int(fold_today2.iloc[1, 5]):,}',
"v26": f'{int(fold_today2.iloc[1, 6]):,}',
# Table 1 Subtotals
"v16": f'{int(flip_accum1.iloc[7, 2] + fold_accum1.iloc[7, 2]):,}',
"v17": f'{int(flip_accum1.iloc[3, 2] + fold_accum1.iloc[3, 2]):,}',
"v18": f'{int(flip_accum2.iloc[1, 12] + fold_accum2.iloc[1, 12]):,}',
"v19": f'{int(flip_accum2.iloc[1, 5] + fold_accum2.iloc[1, 5]):,}',
"v20": f'{int(flip_accum2.iloc[1, 6] + fold_accum2.iloc[1, 6]):,}',
"v27": f'{int(flip_today2.iloc[1, 12]) + int(fold_today2.iloc[1, 12]):,}',
"v28": f'{int(flip_today2.iloc[1, 5]) + int(fold_today2.iloc[1, 5]):,}',
"v29": f'{int(flip_today2.iloc[1, 6]) + int(fold_today2.iloc[1, 6]):,}',
"v30": f'{int(flip_today2.iloc[1, 4]):,}',
"v31": f'{int(fold_today2.iloc[1, 4]):,}',
"v15": f'{int(flip_today2.iloc[1, 4]) + int(fold_today2.iloc[1, 4]):,}',
"v1": f'{int(flip_accum1.iloc[5, 2] + fold_accum1.iloc[5, 2]):,}',
"v32": f'{int(flipfold_accum2.iloc[2, 4]):,}',
"v33": f'{int(flip_accum2.iloc[2, 4]):,}',
"v34": f'{int(fold_accum2.iloc[2, 4]):,}',
"v35": f'{int(flipfold_accum2.iloc[2, 13]):,}',
"v36": f'{int(fold_accum2.iloc[2, 13]):,}',
"v37": f'{int(flip_accum2.iloc[2, 13]):,}',
"v38": abs(round(((flipfold_accum2.iloc[2, 4] - flipfold_accum2.iloc[2, 13])/flipfold_accum2.iloc[2, 13]) * 100)),
"increase_decrease": increase_decrease,
"c12": int(template_flip4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum()),
"c19": int(template_fold4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum()),
"c14": int(template_flip4.loc[['Camera', 'Others'], 'SEAO Total'].sum()),
"c21": int(template_fold4.loc[['Camera', 'Others'], 'SEAO Total'].sum()),
"c1": int(flip4_dict["a1"] + fold4_dict["b1"]),
"c2": int(flip4_dict["a19"] + fold4_dict["b19"]),
"c3": int(flip4_dict["a64"] + fold4_dict["b64"]),
"c4": int(flip4_dict["a82"] + fold4_dict["b82"]),
"c5": int(template_flip4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum() + template_fold4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum()),
"c6": int(flip4_dict["a73"] + fold4_dict["b73"]),
"c7": int(template_flip4.loc[['Camera', 'Others'], 'SEAO Total'].sum() + template_fold4.loc[['Camera', 'Others'], 'SEAO Total'].sum()),
"d1": round(100*(flip4_dict["a19"] + fold4_dict["b19"])/(flip4_dict["a1"] + fold4_dict["b1"])),
"d2": round(100*(flip4_dict["a64"] + fold4_dict["b64"])/(flip4_dict["a1"] + fold4_dict["b1"])),
"d3": round(100*(template_flip4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum() + template_fold4.loc[['Heat', 'Quick Discharge', 'Power', 'Rebooting'], 'SEAO Total'].sum())/(flip4_dict["a1"] + fold4_dict["b1"]))
}
context2 = {**context, **flip4_dict, **fold4_dict}
doc.render(context2)
# Create in-memory buffer
file_stream = io.BytesIO()
# Save the .docx to the buffer
doc.save(file_stream)
# Reset the buffer's file-pointer to the beginning of the file
file_stream.seek(0)
#doc.save("SEAO Fold 4_Flip 4 Quality Monitoring (" + datetime.datetime.now().strftime("%#d %b") + ").docx")
st.download_button(
label="Download report here",
data=file_stream,
file_name="SEAO Fold 4_Flip 4 Quality Monitoring (" + datetime.datetime.now().strftime("%#d %b") + ").docx",
mime='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
|