hectorjelly's picture
Update app.py
1bc3119
import datetime
import pandas as pd
import streamlit as st
import timeago
st.set_page_config(layout="wide")
st.markdown(
"""
<style>
.block-container {
padding-top: 1rem;
}
#MainMenu {visibility: hidden;}
</style>
""",
unsafe_allow_html=True
)
now = datetime.datetime.now()
MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv"
@st.cache_data(ttl=1800)
def fetch_match_history():
"""
Fetch the match results from the last 24 hours.
Cache the result for 30min to avoid unnecessary requests.
Return a DataFrame.
"""
df = pd.read_csv(MATCH_RESULTS_URL)
df["timestamp"] = pd.to_datetime(df.timestamp, unit="s")
df = df[df["timestamp"] >= pd.Timestamp.now() - pd.Timedelta(hours=24)]
df.columns = ["home", "away", "timestamp", "result"]
return df
match_df = fetch_match_history()
def num_matches_played():
return match_df.shape[0]
teams = sorted(
list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold
)
st.title("🤗 SoccerTwos Challenge Different Metrics - Just for Fun! - Only last 24hours of games considered")
team_results = {}
for i, row in match_df.iterrows():
home_team = row["home"]
away_team = row["away"]
result = row["result"]
if home_team not in team_results:
team_results[home_team] = [0, 0, 0, 0]
if away_team not in team_results:
team_results[away_team] = [0, 0, 0, 0]
if result == 0:
team_results[home_team][2] += 1
team_results[away_team][0] += 1
team_results[home_team][3] += 1
elif result == 1:
team_results[home_team][0] += 1
team_results[away_team][2] += 1
team_results[home_team][3] += 3
else:
team_results[home_team][1] += 1
team_results[away_team][1] += 1
team_results[away_team][3] += 3
df = pd.DataFrame.from_dict(
team_results, orient="index", columns=["wins", "draws", "losses", "points"]
).sort_values(by="points", ascending=False)
df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True)
df = df[["owner", "team", "wins", "draws", "losses", "points"]]
df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100
stats = df
st.header("League Table")
st.dataframe(stats)
st.subheader("Match Results")
st.write("Note: Match results are not displayed in this version of the app.")