Update src/streamlit_app.py
Browse files- src/streamlit_app.py +66 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,68 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from nba_data import get_player_id, get_player_stats
|
| 5 |
+
import altair as alt
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="NBA Head-to-Head", page_icon="π")
|
| 8 |
+
st.title("π NBA Head-to-Head Player Comparison")
|
| 9 |
+
|
| 10 |
+
# --- Player 1 ---
|
| 11 |
+
player_1 = st.text_input("π Player 1 Full Name", key="player_1")
|
| 12 |
+
player_2 = st.text_input("π Player 2 Full Name", key="player_2")
|
| 13 |
+
|
| 14 |
+
stats_1 = None
|
| 15 |
+
stats_2 = None
|
| 16 |
+
season_selected = None
|
| 17 |
+
|
| 18 |
+
if player_1 and player_2:
|
| 19 |
+
player_1_id = get_player_id(player_1)
|
| 20 |
+
player_2_id = get_player_id(player_2)
|
| 21 |
+
|
| 22 |
+
if player_1_id and player_2_id:
|
| 23 |
+
stats_1 = get_player_stats(player_1_id)
|
| 24 |
+
stats_2 = get_player_stats(player_2_id)
|
| 25 |
+
|
| 26 |
+
common_seasons = sorted(
|
| 27 |
+
list(set(stats_1["SEASON_ID"]).intersection(set(stats_2["SEASON_ID"]))), reverse=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if common_seasons:
|
| 31 |
+
season_selected = st.selectbox("π
Select Season", common_seasons)
|
| 32 |
+
|
| 33 |
+
df_1 = stats_1[stats_1["SEASON_ID"] == season_selected]
|
| 34 |
+
df_2 = stats_2[stats_2["SEASON_ID"] == season_selected]
|
| 35 |
+
|
| 36 |
+
st.subheader(f"π {player_1} vs {player_2} - {season_selected}")
|
| 37 |
+
combined = pd.DataFrame({
|
| 38 |
+
"Stat": ["PTS", "AST", "REB", "STL", "BLK"],
|
| 39 |
+
player_1: [
|
| 40 |
+
float(df_1["PTS"].values[0]),
|
| 41 |
+
float(df_1["AST"].values[0]),
|
| 42 |
+
float(df_1["REB"].values[0]),
|
| 43 |
+
float(df_1["STL"].values[0]),
|
| 44 |
+
float(df_1["BLK"].values[0]),
|
| 45 |
+
],
|
| 46 |
+
player_2: [
|
| 47 |
+
float(df_2["PTS"].values[0]),
|
| 48 |
+
float(df_2["AST"].values[0]),
|
| 49 |
+
float(df_2["REB"].values[0]),
|
| 50 |
+
float(df_2["STL"].values[0]),
|
| 51 |
+
float(df_2["BLK"].values[0]),
|
| 52 |
+
],
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
chart_data = combined.melt("Stat", var_name="Player", value_name="Value")
|
| 56 |
+
|
| 57 |
+
chart = alt.Chart(chart_data).mark_bar().encode(
|
| 58 |
+
x="Stat:N",
|
| 59 |
+
y="Value:Q",
|
| 60 |
+
color="Player:N",
|
| 61 |
+
tooltip=["Player", "Stat", "Value"]
|
| 62 |
+
).properties(width=600, height=400)
|
| 63 |
|
| 64 |
+
st.altair_chart(chart, use_container_width=True)
|
| 65 |
+
else:
|
| 66 |
+
st.warning("These players do not have overlapping seasons.")
|
| 67 |
+
else:
|
| 68 |
+
st.error("One or both players not found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|