-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
255 lines (181 loc) · 7.72 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
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
import streamlit as st
from PIL import Image
import base64
import pandas as pd
import datetime as dt
####
# setting page's format
####
st.set_page_config(layout="wide",page_title="London Bulls Official Website")
# remove "made with streamlit" text - this makes app prettier
hide_default_format = """
<style>
#MainMenu {visibility: hidden; }
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_default_format, unsafe_allow_html=True)
####
# App
####
# App's title image
image = Image.open('./images/logo_LB_no_icon.png')
st.image(image) # , caption='Sunrise by the mountains'
# App's icon image
# LOGO_IMAGE = "logo.png"
# st.markdown(
# """
# <style>
# .container {
# display: flex;
# }
# .logo-text {
# font-weight:700 !important;
# font-size:50px !important;
# color: #f9a01b !important;
# padding-top: 75px !important;
# }
# .logo-img {
# float:right;
# }
# </style>
# """,
# unsafe_allow_html=True
# st.markdown(
# f"""
# <div class="container">
# <img class="logo-img" src="data:image/png;base64,{base64.b64encode(open(LOGO_IMAGE, "rb").read()).decode()}">
# <p class="logo-text">Logo Much ?</p>
# </div>
# """,
# unsafe_allow_html=True
# )
# App's title
st.title('London Bulls Official Website')
# Load the Excel file into the session state object
if "df" not in st.session_state:
# Load the Excel file into a DataFrame
# st.session_state.df = pd.read_excel(".\Files\London Bulls Excel 21-22.xlsx", sheet_name ="Rolling")
st.session_state.df = pd.read_csv("./Files/london_bulls_excel.csv")
# Load the Excel file into a DataFrame
# df = pd.read_excel(".\Files\London Bulls Excel 21-22.xlsx", sheet_name ="Rolling")
##
# Data cleaning
##
# cols to lowercase, and blank spaces to underscore
st.session_state.df.columns = st.session_state.df.columns.str.lower()
st.session_state.df.columns = st.session_state.df.columns.str.replace(' ','_')
# Convert the 'date' column to datetime dtype
st.session_state.df['date'] = pd.to_datetime(st.session_state.df['date'])
##
# Data gathering
##
# # Create a list of unique player names from the CSV file
unique_players = st.session_state.df['full_name'].unique().tolist()
# # Create a list of unique tournament names from the CSV file
unique_tournaments = st.session_state.df['tournament'].unique().tolist()
# # Data gathering - Create a list of unique opponent names from the CSV file
unique_opponents = st.session_state.df['opponent'].unique().tolist()
# # Display first two rows of the dataframe
st.subheader('A sneaky peak into some previous player statistics:')
st.write( st.session_state.df.sample(6) )
# Define a function to add a new row to the DataFrame
def add_row(df, players, starting, minutes, goals, goals_conceded, yellow_card, red_card, tournament, date, opponent):
new_rows = []
for player in players:
new_row = {"full_name": player,
"starting": starting,
"minutes": minutes,
"goals": goals,
"goals_conceded": goals_conceded,
"yellow_card": yellow_card,
"red_card": red_card,
"tournament": tournament,
"date": date,
"opponent": opponent
}
new_rows.append(new_row)
new_rows_df = pd.DataFrame(new_rows)
df = pd.concat([df, new_rows_df], ignore_index=True)
return df
st.write('\n')
# # Add title for the subsection
st.subheader('Update player statistics after the latest match:')
# Split screen in two cols
col1, col2 = st.columns(2)
with col1:
# Create a form for adding a new row
players = st.multiselect("Select players", unique_players) # Use multiselect for selecting multiple players
# Allow users to add new players to the list
new_player = st.text_input("Enter a new player (if not in the list)")
if new_player:
unique_players.append(new_player)
players.append(new_player) # Automatically select the new player
starting = st.selectbox("starting? :smoking:", ("yes", "no"))
minutes = st.number_input("Enter minutes", format="%d", step=1)
goals = st.number_input(":soccer: Enter goals", format="%d", step=1)
goals_conceded = st.number_input(":lock: Enter goals conceded", format="%d", step=1)
# setting another col
with col2:
# Create a form for adding a new row
yellow_card = st.text_input("Enter 1 if player received a red card or leave empty")
red_card = st.text_input("Enter 1 if player received a yellow card or leave empty")
# Allow users to select a tournament from the existing list, or select an empty option
tournament = st.selectbox("Select tournament (leave empty if it's a new tournament)", [""] + unique_tournaments)
# Allow users to add a new tournament
new_tournament = st.text_input("Enter a new tournament (if not in the list)")
if new_tournament:
unique_tournaments.append(new_tournament)
tournament = new_tournament # Automatically use the new tournament
date = st.date_input("Enter date")
# Allow users to select an opponent from the existing list, or select an empty option
opponent = st.selectbox("Select opponent (leave empty if it's a new opponent)", [""] + unique_opponents)
# Allow users to add a new opponent if the selected option is empty
if not opponent:
new_opponent = st.text_input("Enter the new opponent name")
if new_opponent:
unique_opponents.append(new_opponent)
opponent = new_opponent # Automatically use the new opponent
# convert dates to int
# st.session_state.df['date'] = st.session_state.df['date'].apply( lambda item: item.strftime("%Y-%m-%d") )
##
# Buttons
##
add_button = st.button("Press here to add statistics")
st.write('\n')
# # If the "Press here to add statistics" button is clicked, add the new rows to the DataFrame
if add_button:
st.session_state.df = add_row(st.session_state.df,players, starting, minutes, goals, goals_conceded, yellow_card, red_card, tournament, date, opponent)
st.success("New data added successfully!")
# checkpiont
print(st.session_state.df.tail(4))
# horizzontal line
st.divider()
##
# Display data
#
st.subheader("Player statistics after the latest match:")
# Convert the 'date' column to datetime dtype
st.session_state.df['date'] = pd.to_datetime(st.session_state.df['date'])
# Find the most recent date in the DataFrame
most_recent_date = st.session_state.df['date'].max()
# Filter the DataFrame to display only rows with the most recent date
df_most_recent = st.session_state.df[st.session_state.df['date'] == most_recent_date].copy()
# Change the date format to 'dd-mmm-yyyy'
df_most_recent['date'] = pd.to_datetime(df_most_recent['date']).dt.strftime('%d-%b-%Y')
# Display the filtered DataFrame
st.write(df_most_recent)
st.write('\n')
st.divider()
##
# Add a button to save the updated DataFrame to the Excel file
save_button = st.button(":white_check_mark: :checkered_flag: Save statistics for the most recent match")
# If the "Save" button is clicked, append the new data to the Excel file
# if save_button:
# with pd.ExcelWriter(".\Files\london_bulls_excel.csv", mode='a', engine="openpyxl") as writer:
# df_most_recent.to_csv(writer, index=False, header=False, startrow=len(df) - len(df_most_recent))
# st.success("Statistics for the most recent match have been saved!")
# If the "Save" button is clicked, append the new data to the Excel file
if save_button:
st.session_state.df.to_csv("./Files/london_bulls_excel.csv", index=False)
st.success("Statistics have been saved!")