Spaces:
Runtime error
Runtime error
import datetime | |
import pandas as pd | |
import streamlit as st | |
import timeago | |
# Set page title and favicon | |
st.set_page_config(page_title="24 Hours Form Table", page_icon=":soccer:",layout="wide") | |
st.markdown( | |
""" | |
<style> | |
.block-container { | |
padding-top: 1rem; | |
} | |
#MainMenu {visibility: hidden;} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
# Set title and create a new tab for league history | |
st.title("24 Hours Form Table!") | |
tab_team, tab_history = st.tabs(["Form Table", "Next"]) | |
# Fetch the match results from the last 24 hours | |
MATCH_RESULTS_URL = "https://huggingface.co/datasets/huggingface-projects/bot-fight-data/raw/main/soccer_history.csv" | |
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() | |
# Define a function to calculate the total number of matches played | |
def num_matches_played(): | |
return match_df.shape[0] | |
# Get a list of all teams that have played in the last 24 hours | |
teams = sorted( | |
list(pd.concat([match_df["home"], match_df["away"]]).unique()), key=str.casefold | |
) | |
# Create the form table, which shows the win percentage for each team | |
st.header("Form Table") | |
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] | |
if away_team not in team_results: | |
team_results[away_team] = [0, 0, 0] | |
if result == 0: | |
team_results[home_team][2] += 1 | |
team_results[away_team][0] += 1 | |
elif result == 1: | |
team_results[home_team][0] += 1 | |
team_results[away_team][2] += 1 | |
else: | |
team_results[home_team][1] += 1 | |
team_results[away_team][1] += 1 | |
# Create a DataFrame from the results dictionary and calculate the win percentage | |
df = pd.DataFrame.from_dict( | |
team_results, orient="index", columns=["wins", "draws", "losses"] | |
).sort_index() | |
df[["owner", "team"]] = df.index.to_series().str.split("/", expand=True) | |
df = df[["owner", "team", "wins", "draws", "losses"]] | |
df["win_pct"] = (df["wins"] / (df["wins"] + df["draws"] + df["losses"])) * 100 | |
# Display the DataFrame as a table, sorted by win percentage | |
stats = df.sort_values(by="win_pct", ascending=False) | |
styled_stats = stats.style.set_table_attributes("style='font-size: 20px'").set_table_styles([dict(selector='th', props=[('max-width', '200px')])]) | |
styled_stats = styled_stats.set_table_attributes("style='max-height: 1200px; overflow: auto'") | |
st.dataframe(styled_stats) | |
# Create a new tab for league history over time | |
with tab_history: | |
st.write("Coming soon!") | |