Spaces:
Running
Running
Jon Solow
commited on
Commit
·
ad48790
1
Parent(s):
a3b2378
Add RZ Opps page
Browse files
src/pages/6_Redzone_Opportunities.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from config import DEFAULT_ICON
|
| 6 |
+
from shared_page import common_page_config
|
| 7 |
+
|
| 8 |
+
from queries.footballguys.constants import YEAR
|
| 9 |
+
from queries.footballguys.refresh import request_stat
|
| 10 |
+
from streamlit_filter import filter_dataframe
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@st.cache_data(ttl=60 * 60 * 24)
|
| 14 |
+
def load_data():
|
| 15 |
+
stat_name = "redzone"
|
| 16 |
+
data = request_stat(stat_name)
|
| 17 |
+
data_load_time_str = datetime.datetime.utcnow().strftime("%m/%d/%Y %I:%M %p")
|
| 18 |
+
return data, data_load_time_str
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_page():
|
| 22 |
+
page_title = f"Player Redzone Opportunities - {YEAR}"
|
| 23 |
+
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
| 24 |
+
common_page_config()
|
| 25 |
+
st.title(page_title)
|
| 26 |
+
if st.button("Refresh Data"):
|
| 27 |
+
st.cache_data.clear()
|
| 28 |
+
data, data_load_time_str = load_data()
|
| 29 |
+
st.write(f"Data loaded as of: {data_load_time_str} UTC")
|
| 30 |
+
|
| 31 |
+
selected_subtotals = st.selectbox("Show:", ["Player Totals", "Position Totals"], index=0)
|
| 32 |
+
if selected_subtotals == "Player Totals":
|
| 33 |
+
data = data[~data.name.str.contains(" Totals")]
|
| 34 |
+
elif selected_subtotals == "Position Totals":
|
| 35 |
+
data = data[data.name.str.contains(" Totals")]
|
| 36 |
+
|
| 37 |
+
value_types = st.selectbox("Counts / Percent:", ["Counts", "Percent"], index=0)
|
| 38 |
+
if value_types == "Percent":
|
| 39 |
+
numerical_data = data.select_dtypes(include=np.number)
|
| 40 |
+
numerical_cols = numerical_data.columns
|
| 41 |
+
df_percent_values = numerical_data / data.groupby("TEAM").transform(sum).select_dtypes(include=np.number)
|
| 42 |
+
data.loc[:, numerical_cols] = df_percent_values
|
| 43 |
+
|
| 44 |
+
with st.container():
|
| 45 |
+
filtered_data = filter_dataframe(data)
|
| 46 |
+
st.dataframe(
|
| 47 |
+
filtered_data,
|
| 48 |
+
hide_index=True,
|
| 49 |
+
height=35 * (len(filtered_data) + 1) + 12,
|
| 50 |
+
use_container_width=False,
|
| 51 |
+
column_config={},
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
get_page()
|
src/queries/footballguys/helpers.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import lxml.html
|
| 2 |
import pandas as pd
|
| 3 |
import requests
|
|
@@ -7,7 +8,7 @@ from queries.footballguys import constants as fbgc
|
|
| 7 |
|
| 8 |
def url_to_pandas(url) -> List[pd.DataFrame]:
|
| 9 |
page = requests.get(url)
|
| 10 |
-
table = pd.read_html(page.text.replace("<br>", "-"))
|
| 11 |
return table
|
| 12 |
|
| 13 |
|
|
@@ -78,9 +79,7 @@ def parse_snaps(team_short_name: str, base_url: str = fbgc.BASE_URL, year: int =
|
|
| 78 |
|
| 79 |
def add_targets_position(team_df: pd.DataFrame):
|
| 80 |
# fill blanks up by reversing index, fill down, and re-reversing
|
| 81 |
-
positions = team_df.name.apply(lambda x: x.replace(" Totals", "") if " Totals" in x else None)[::-1].
|
| 82 |
-
method="ffill"
|
| 83 |
-
)[::-1]
|
| 84 |
team_df.insert(0, "POS", positions)
|
| 85 |
|
| 86 |
|
|
|
|
| 1 |
+
from io import StringIO
|
| 2 |
import lxml.html
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
|
|
|
| 8 |
|
| 9 |
def url_to_pandas(url) -> List[pd.DataFrame]:
|
| 10 |
page = requests.get(url)
|
| 11 |
+
table = pd.read_html(StringIO(page.text.replace("<br>", "-")))
|
| 12 |
return table
|
| 13 |
|
| 14 |
|
|
|
|
| 79 |
|
| 80 |
def add_targets_position(team_df: pd.DataFrame):
|
| 81 |
# fill blanks up by reversing index, fill down, and re-reversing
|
| 82 |
+
positions = team_df.name.apply(lambda x: x.replace(" Totals", "") if " Totals" in x else None)[::-1].ffill()[::-1]
|
|
|
|
|
|
|
| 83 |
team_df.insert(0, "POS", positions)
|
| 84 |
|
| 85 |
|