Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,6 @@ import numpy as np
|
|
| 6 |
from PIL import Image
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
-
import base64
|
| 10 |
-
|
| 11 |
-
# Constants
|
| 12 |
-
TEAM_LOGOS = {
|
| 13 |
-
"India": "https://upload.wikimedia.org/wikipedia/en/thumb/8/8d/Cricket_India_Crest.svg/1200px-Cricket_India_Crest.svg.png",
|
| 14 |
-
"Australia": "https://upload.wikimedia.org/wikipedia/en/thumb/0/0d/Australian_Cricket_Team_Crest.svg/1200px-Australian_Cricket_Team_Crest.svg.png",
|
| 15 |
-
# Add more teams as needed
|
| 16 |
-
}
|
| 17 |
|
| 18 |
# Page Configuration
|
| 19 |
st.set_page_config(
|
|
@@ -32,176 +24,146 @@ st.set_page_config(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# Custom CSS
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Data Loading
|
| 42 |
@st.cache_data
|
| 43 |
def load_data():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
df[bowling_cols] = df[bowling_cols].fillna(0)
|
| 51 |
-
|
| 52 |
# Create composite metrics
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Add career span
|
| 60 |
-
df['career_span'] = df['Last_Year'] - df['First_Year']
|
| 61 |
-
|
| 62 |
-
return df
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Error loading data: {e}")
|
| 65 |
-
return pd.DataFrame()
|
| 66 |
|
| 67 |
df = load_data()
|
| 68 |
|
| 69 |
# Utility Functions
|
| 70 |
-
def get_player_image(player_name):
|
| 71 |
-
"""Fetch player image from Cricinfo or other source"""
|
| 72 |
-
try:
|
| 73 |
-
# Placeholder - in production, use actual API
|
| 74 |
-
return None
|
| 75 |
-
except:
|
| 76 |
-
return None
|
| 77 |
-
|
| 78 |
-
def get_team_image(team_name):
|
| 79 |
-
"""Fetch team logo"""
|
| 80 |
-
return TEAM_LOGOS.get(team_name, None)
|
| 81 |
-
|
| 82 |
def styled_metric(label, value, delta=None, help_text=None):
|
| 83 |
"""Create a styled metric card"""
|
| 84 |
-
with st.container(
|
| 85 |
-
st.markdown(f"
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
if delta:
|
| 88 |
st.metric("", "", delta=delta, help=help_text)
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
"""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
fig.add_trace(go.Scatterpolar(
|
| 95 |
-
r=values,
|
| 96 |
-
theta=categories,
|
| 97 |
-
fill='toself',
|
| 98 |
-
name='Performance',
|
| 99 |
-
line=dict(color='#1f77b4'),
|
| 100 |
-
fillcolor='rgba(31, 119, 180, 0.2)'
|
| 101 |
-
))
|
| 102 |
-
|
| 103 |
-
fig.update_layout(
|
| 104 |
-
polar=dict(
|
| 105 |
-
radialaxis=dict(
|
| 106 |
-
visible=True,
|
| 107 |
-
range=[0, max(values)*1.2]
|
| 108 |
-
)),
|
| 109 |
-
showlegend=False,
|
| 110 |
-
title=title,
|
| 111 |
-
template='plotly_white',
|
| 112 |
-
height=400
|
| 113 |
-
)
|
| 114 |
-
return fig
|
| 115 |
|
| 116 |
-
#
|
| 117 |
def show_player_stats():
|
| 118 |
st.header("๐ Player Statistics Explorer", divider='rainbow')
|
| 119 |
|
| 120 |
-
col1, col2 = st.columns(
|
| 121 |
with col1:
|
| 122 |
-
player = st.selectbox("Select Player",
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
if player not in df['Player'].values:
|
| 126 |
-
st.warning("Player not found in dataset")
|
| 127 |
-
return
|
| 128 |
|
| 129 |
player_data = df[df['Player'] == player].iloc[0]
|
| 130 |
|
| 131 |
-
with
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
styled_metric("Matches", player_data[f"Matches_{format_type}"])
|
| 142 |
-
with
|
| 143 |
styled_metric("Runs", player_data[f"batting_Runs_{format_type}"])
|
| 144 |
-
with
|
| 145 |
styled_metric("Average", f"{player_data[f'batting_Average_{format_type}']:.1f}")
|
| 146 |
-
with
|
| 147 |
styled_metric("Strike Rate", f"{player_data[f'batting_SR_{format_type}']:.1f}")
|
| 148 |
|
| 149 |
-
# Performance
|
| 150 |
-
tab1, tab2, tab3
|
| 151 |
|
| 152 |
with tab1:
|
| 153 |
-
st.subheader("Batting Performance
|
| 154 |
|
| 155 |
-
|
|
|
|
| 156 |
batting_values = [
|
| 157 |
player_data[f'batting_Average_{format_type}'],
|
| 158 |
player_data[f'batting_SR_{format_type}'],
|
|
|
|
|
|
|
| 159 |
(player_data[f'batting_Fours_{format_type}']*4 + player_data[f'batting_Sixes_{format_type}']*6) /
|
| 160 |
-
max(1, player_data[f'batting_Runs_{format_type}']) * 100
|
| 161 |
-
player_data[f'batting_impact_{format_type}']
|
| 162 |
]
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
color_discrete_sequence=px.colors.qualitative.Pastel
|
| 176 |
)
|
| 177 |
-
fig.update_layout(showlegend=False)
|
| 178 |
st.plotly_chart(fig, use_container_width=True)
|
| 179 |
-
|
| 180 |
-
# Innings Distribution
|
| 181 |
-
innings_data = {
|
| 182 |
-
'Type': ['Dot Balls', '1-3 Runs', 'Boundaries', 'Sixes'],
|
| 183 |
-
'Percentage': [
|
| 184 |
-
player_data[f'batting_DotBalls_{format_type}'] / player_data[f'batting_BallsFaced_{format_type}'] * 100,
|
| 185 |
-
(player_data[f'batting_Ones_{format_type}'] + player_data[f'batting_Twos_{format_type}'] + player_data[f'batting_Threes_{format_type}']) /
|
| 186 |
-
player_data[f'batting_BallsFaced_{format_type}'] * 100,
|
| 187 |
-
player_data[f'batting_Fours_{format_type}'] / player_data[f'batting_BallsFaced_{format_type}'] * 100,
|
| 188 |
-
player_data[f'batting_Sixes_{format_type}'] / player_data[f'batting_BallsFaced_{format_type}'] * 100
|
| 189 |
-
]
|
| 190 |
-
}
|
| 191 |
-
fig = px.pie(
|
| 192 |
-
innings_data,
|
| 193 |
-
names='Type',
|
| 194 |
-
values='Percentage',
|
| 195 |
-
title='Innings Distribution',
|
| 196 |
-
hole=0.3,
|
| 197 |
-
color_discrete_sequence=px.colors.sequential.RdBu
|
| 198 |
-
)
|
| 199 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 200 |
|
| 201 |
with tab2:
|
| 202 |
if player_data[f'bowling_{format_type}_Innings'] > 0:
|
| 203 |
-
st.subheader("Bowling Performance
|
| 204 |
|
|
|
|
| 205 |
bowling_metrics = ['Wickets', 'Average', 'Economy', 'Strike Rate']
|
| 206 |
bowling_values = [
|
| 207 |
player_data[f'bowling_{format_type}_Wickets'],
|
|
@@ -210,40 +172,12 @@ def show_player_stats():
|
|
| 210 |
player_data[f'bowling_{format_type}_SR']
|
| 211 |
]
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
x=bowling_metrics,
|
| 220 |
-
y=bowling_values,
|
| 221 |
-
color=bowling_metrics,
|
| 222 |
-
title="Bowling Metrics Comparison",
|
| 223 |
-
labels={'x': 'Metric', 'y': 'Value'},
|
| 224 |
-
color_discrete_sequence=px.colors.qualitative.Pastel
|
| 225 |
-
)
|
| 226 |
-
fig.update_layout(showlegend=False)
|
| 227 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 228 |
-
|
| 229 |
-
# Wicket Distribution
|
| 230 |
-
wicket_data = {
|
| 231 |
-
'Type': ['LBW', 'Bowled', 'Caught', 'Stumped', 'Run Out'],
|
| 232 |
-
'Count': [
|
| 233 |
-
player_data[f'bowling_{format_type}_LBW'],
|
| 234 |
-
player_data[f'bowling_{format_type}_Bowled'],
|
| 235 |
-
player_data[f'bowling_{format_type}_Caught'],
|
| 236 |
-
player_data[f'bowling_{format_type}_Stumped'],
|
| 237 |
-
player_data[f'bowling_{format_type}_RunOut']
|
| 238 |
-
]
|
| 239 |
-
}
|
| 240 |
-
fig = px.pie(
|
| 241 |
-
wicket_data,
|
| 242 |
-
names='Type',
|
| 243 |
-
values='Count',
|
| 244 |
-
title='Wicket Distribution',
|
| 245 |
-
hole=0.3,
|
| 246 |
-
color_discrete_sequence=px.colors.sequential.Plasma
|
| 247 |
)
|
| 248 |
st.plotly_chart(fig, use_container_width=True)
|
| 249 |
else:
|
|
@@ -260,369 +194,21 @@ def show_player_stats():
|
|
| 260 |
player_data[f'bowling_{format_type}_5w']
|
| 261 |
]
|
| 262 |
|
| 263 |
-
fig = px.
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
color=milestones,
|
| 267 |
title="Career Milestones",
|
| 268 |
-
|
| 269 |
-
color_discrete_sequence=px.colors.qualitative.Vivid
|
| 270 |
-
)
|
| 271 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 272 |
-
|
| 273 |
-
# Milestone Timeline
|
| 274 |
-
if 'Debut_Date' in df.columns and 'Last_Match_Date' in df.columns:
|
| 275 |
-
timeline_data = {
|
| 276 |
-
'Event': ['Debut', 'First 50', 'First 100', 'Last Match'],
|
| 277 |
-
'Date': [
|
| 278 |
-
player_data['Debut_Date'],
|
| 279 |
-
player_data['First_50_Date'],
|
| 280 |
-
player_data['First_100_Date'],
|
| 281 |
-
player_data['Last_Match_Date']
|
| 282 |
-
]
|
| 283 |
-
}
|
| 284 |
-
fig = px.timeline(
|
| 285 |
-
timeline_data,
|
| 286 |
-
x_start="Date",
|
| 287 |
-
y="Event",
|
| 288 |
-
title="Career Timeline"
|
| 289 |
-
)
|
| 290 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 291 |
-
|
| 292 |
-
with tab4:
|
| 293 |
-
st.subheader("Format Comparison")
|
| 294 |
-
|
| 295 |
-
formats = ['Test', 'ODI', 'T20', 'IPL']
|
| 296 |
-
comparison_data = {
|
| 297 |
-
'Format': formats,
|
| 298 |
-
'Batting Average': [player_data[f'batting_Average_{fmt}'] for fmt in formats],
|
| 299 |
-
'Batting Strike Rate': [player_data[f'batting_SR_{fmt}'] for fmt in formats],
|
| 300 |
-
'Bowling Average': [player_data[f'bowling_{fmt}_Avg'] for fmt in formats],
|
| 301 |
-
'Bowling Economy': [player_data[f'bowling_{fmt}_Eco'] for fmt in formats]
|
| 302 |
-
}
|
| 303 |
-
|
| 304 |
-
fig = px.line(
|
| 305 |
-
comparison_data,
|
| 306 |
-
x='Format',
|
| 307 |
-
y=['Batting Average', 'Batting Strike Rate', 'Bowling Average', 'Bowling Economy'],
|
| 308 |
-
title='Performance Across Formats',
|
| 309 |
-
markers=True
|
| 310 |
)
|
| 311 |
st.plotly_chart(fig, use_container_width=True)
|
| 312 |
|
| 313 |
-
#
|
| 314 |
-
def show_player_comparison():
|
| 315 |
-
st.header("๐ Player Comparison Tool", divider='rainbow')
|
| 316 |
-
|
| 317 |
-
col1, col2 = st.columns(2)
|
| 318 |
-
with col1:
|
| 319 |
-
player1 = st.selectbox("Player 1", sorted(df['Player'].unique()), key='player1')
|
| 320 |
-
format_type = st.selectbox("Compare in Format", ["Test", "ODI", "T20"], key='compare_format')
|
| 321 |
-
|
| 322 |
-
with col2:
|
| 323 |
-
player2 = st.selectbox("Player 2",
|
| 324 |
-
[p for p in sorted(df['Player'].unique()) if p != player1],
|
| 325 |
-
index=1, key='player2')
|
| 326 |
-
|
| 327 |
-
if player1 == player2:
|
| 328 |
-
st.warning("Please select two different players")
|
| 329 |
-
return
|
| 330 |
-
|
| 331 |
-
p1_data = df[df['Player'] == player1].iloc[0]
|
| 332 |
-
p2_data = df[df['Player'] == player2].iloc[0]
|
| 333 |
-
|
| 334 |
-
# Header with player info
|
| 335 |
-
col1, col2 = st.columns(2)
|
| 336 |
-
with col1:
|
| 337 |
-
st.subheader(player1)
|
| 338 |
-
if get_team_image(p1_data['Player_Team']):
|
| 339 |
-
st.image(get_team_image(p1_data['Player_Team']), width=80)
|
| 340 |
-
|
| 341 |
-
with col2:
|
| 342 |
-
st.subheader(player2)
|
| 343 |
-
if get_team_image(p2_data['Player_Team']):
|
| 344 |
-
st.image(get_team_image(p2_data['Player_Team']), width=80)
|
| 345 |
-
|
| 346 |
-
# Comparison metrics
|
| 347 |
-
metrics = [
|
| 348 |
-
("Matches", f"Matches_{format_type}", "higher"),
|
| 349 |
-
("Runs", f"batting_Runs_{format_type}", "higher"),
|
| 350 |
-
("Average", f"batting_Average_{format_type}", "higher"),
|
| 351 |
-
("Strike Rate", f"batting_SR_{format_type}", "higher"),
|
| 352 |
-
("50s", f"batting_50s_{format_type}", "higher"),
|
| 353 |
-
("100s", f"batting_100s_{format_type}", "higher"),
|
| 354 |
-
("Wickets", f"bowling_{format_type}_Wickets", "higher"),
|
| 355 |
-
("Bowling Avg", f"bowling_{format_type}_Avg", "lower"),
|
| 356 |
-
("Economy", f"bowling_{format_type}_Eco", "lower"),
|
| 357 |
-
("Player Impact", f"player_value_{format_type}", "higher")
|
| 358 |
-
]
|
| 359 |
-
|
| 360 |
-
comparison_data = []
|
| 361 |
-
for name, col, better in metrics:
|
| 362 |
-
try:
|
| 363 |
-
p1_val = p1_data[col]
|
| 364 |
-
p2_val = p2_data[col]
|
| 365 |
-
|
| 366 |
-
if better == "higher":
|
| 367 |
-
diff = p1_val - p2_val
|
| 368 |
-
winner = player1 if diff > 0 else player2 if diff < 0 else "Equal"
|
| 369 |
-
else:
|
| 370 |
-
diff = p2_val - p1_val
|
| 371 |
-
winner = player2 if diff > 0 else player1 if diff < 0 else "Equal"
|
| 372 |
-
|
| 373 |
-
comparison_data.append({
|
| 374 |
-
"Metric": name,
|
| 375 |
-
player1: p1_val,
|
| 376 |
-
player2: p2_val,
|
| 377 |
-
"Difference": abs(diff),
|
| 378 |
-
"Winner": winner
|
| 379 |
-
})
|
| 380 |
-
except:
|
| 381 |
-
continue
|
| 382 |
-
|
| 383 |
-
comparison_df = pd.DataFrame(comparison_data)
|
| 384 |
-
|
| 385 |
-
# Display comparison table
|
| 386 |
-
st.subheader("Head-to-Head Comparison", divider='gray')
|
| 387 |
-
|
| 388 |
-
# Heatmap style comparison
|
| 389 |
-
heat_df = comparison_df.set_index("Metric")[[player1, player2]]
|
| 390 |
-
fig = px.imshow(
|
| 391 |
-
heat_df,
|
| 392 |
-
labels=dict(x="Player", y="Metric", color="Value"),
|
| 393 |
-
x=[player1, player2],
|
| 394 |
-
y=heat_df.index,
|
| 395 |
-
aspect="auto",
|
| 396 |
-
color_continuous_scale='RdYlGn',
|
| 397 |
-
title="Performance Heatmap Comparison"
|
| 398 |
-
)
|
| 399 |
-
fig.update_xaxes(side="top")
|
| 400 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 401 |
-
|
| 402 |
-
# Radar chart comparison
|
| 403 |
-
radar_metrics = ['Average', 'Strike Rate', '50s', '100s', 'Wickets', 'Economy']
|
| 404 |
-
radar_values_p1 = [
|
| 405 |
-
p1_data[f'batting_Average_{format_type}'],
|
| 406 |
-
p1_data[f'batting_SR_{format_type}'],
|
| 407 |
-
p1_data[f'batting_50s_{format_type}'],
|
| 408 |
-
p1_data[f'batting_100s_{format_type}'],
|
| 409 |
-
p1_data[f'bowling_{format_type}_Wickets'],
|
| 410 |
-
1/p1_data[f'bowling_{format_type}_Eco'] if p1_data[f'bowling_{format_type}_Eco'] > 0 else 0
|
| 411 |
-
]
|
| 412 |
-
|
| 413 |
-
radar_values_p2 = [
|
| 414 |
-
p2_data[f'batting_Average_{format_type}'],
|
| 415 |
-
p2_data[f'batting_SR_{format_type}'],
|
| 416 |
-
p2_data[f'batting_50s_{format_type}'],
|
| 417 |
-
p2_data[f'batting_100s_{format_type}'],
|
| 418 |
-
p2_data[f'bowling_{format_type}_Wickets'],
|
| 419 |
-
1/p2_data[f'bowling_{format_type}_Eco'] if p2_data[f'bowling_{format_type}_Eco'] > 0 else 0
|
| 420 |
-
]
|
| 421 |
-
|
| 422 |
-
fig = go.Figure()
|
| 423 |
-
|
| 424 |
-
fig.add_trace(go.Scatterpolar(
|
| 425 |
-
r=radar_values_p1,
|
| 426 |
-
theta=radar_metrics,
|
| 427 |
-
fill='toself',
|
| 428 |
-
name=player1,
|
| 429 |
-
line=dict(color='#1f77b4'),
|
| 430 |
-
fillcolor='rgba(31, 119, 180, 0.2)'
|
| 431 |
-
))
|
| 432 |
-
|
| 433 |
-
fig.add_trace(go.Scatterpolar(
|
| 434 |
-
r=radar_values_p2,
|
| 435 |
-
theta=radar_metrics,
|
| 436 |
-
fill='toself',
|
| 437 |
-
name=player2,
|
| 438 |
-
line=dict(color='#ff7f0e'),
|
| 439 |
-
fillcolor='rgba(255, 127, 14, 0.2)'
|
| 440 |
-
))
|
| 441 |
-
|
| 442 |
-
fig.update_layout(
|
| 443 |
-
polar=dict(
|
| 444 |
-
radialaxis=dict(
|
| 445 |
-
visible=True,
|
| 446 |
-
range=[0, max(max(radar_values_p1), max(radar_values_p2))*1.2]
|
| 447 |
-
)),
|
| 448 |
-
showlegend=True,
|
| 449 |
-
title="Performance Radar Comparison",
|
| 450 |
-
template='plotly_white',
|
| 451 |
-
height=500
|
| 452 |
-
)
|
| 453 |
-
|
| 454 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 455 |
-
|
| 456 |
-
# Detailed comparison table
|
| 457 |
-
st.subheader("Detailed Metrics Comparison", divider='gray')
|
| 458 |
-
st.dataframe(
|
| 459 |
-
comparison_df.set_index("Metric"),
|
| 460 |
-
column_config={
|
| 461 |
-
"Difference": st.column_config.NumberColumn(
|
| 462 |
-
"Difference",
|
| 463 |
-
format="%.1f",
|
| 464 |
-
help="Positive values favor Player 1 where higher is better"
|
| 465 |
-
),
|
| 466 |
-
"Winner": st.column_config.TextColumn(
|
| 467 |
-
"Winner",
|
| 468 |
-
help="Player with better performance for this metric"
|
| 469 |
-
)
|
| 470 |
-
},
|
| 471 |
-
use_container_width=True
|
| 472 |
-
)
|
| 473 |
|
| 474 |
-
# Team Comparison Component
|
| 475 |
-
def show_team_comparison():
|
| 476 |
-
st.header("๐ด Team Comparison Analyzer", divider='rainbow')
|
| 477 |
-
|
| 478 |
-
teams = sorted(df['Player_Team'].unique())
|
| 479 |
-
|
| 480 |
-
col1, col2 = st.columns(2)
|
| 481 |
-
with col1:
|
| 482 |
-
team1 = st.selectbox("Team 1", teams, key='team1')
|
| 483 |
-
with col2:
|
| 484 |
-
team2 = st.selectbox("Team 2", [t for t in teams if t != team1], key='team2')
|
| 485 |
-
|
| 486 |
-
format_type = st.selectbox("Select Format", ["Test", "ODI", "T20"], key='team_format')
|
| 487 |
-
|
| 488 |
-
team1_players = df[df['Player_Team'] == team1]
|
| 489 |
-
team2_players = df[df['Player_Team'] == team2]
|
| 490 |
-
|
| 491 |
-
# Team header with logos
|
| 492 |
-
col1, col2 = st.columns(2)
|
| 493 |
-
with col1:
|
| 494 |
-
st.subheader(team1)
|
| 495 |
-
if get_team_image(team1):
|
| 496 |
-
st.image(get_team_image(team1), width=120)
|
| 497 |
-
|
| 498 |
-
with col2:
|
| 499 |
-
st.subheader(team2)
|
| 500 |
-
if get_team_image(team2):
|
| 501 |
-
st.image(get_team_image(team2), width=120)
|
| 502 |
-
|
| 503 |
-
# Team comparison metrics
|
| 504 |
-
metrics = {
|
| 505 |
-
"Batting Average": f"batting_Average_{format_type}",
|
| 506 |
-
"Batting Strike Rate": f"batting_SR_{format_type}",
|
| 507 |
-
"Bowling Average": f"bowling_{format_type}_Avg",
|
| 508 |
-
"Bowling Economy": f"bowling_{format_type}_Eco",
|
| 509 |
-
"Batting Impact": f"batting_impact_{format_type}",
|
| 510 |
-
"Bowling Impact": f"bowling_impact_{format_type}",
|
| 511 |
-
"Team Value": f"player_value_{format_type}"
|
| 512 |
-
}
|
| 513 |
-
|
| 514 |
-
comparison_data = []
|
| 515 |
-
for name, col in metrics.items():
|
| 516 |
-
team1_val = team1_players[col].mean()
|
| 517 |
-
team2_val = team2_players[col].mean()
|
| 518 |
-
comparison_data.append({
|
| 519 |
-
"Metric": name,
|
| 520 |
-
team1: team1_val,
|
| 521 |
-
team2: team2_val,
|
| 522 |
-
"Difference": team1_val - team2_val,
|
| 523 |
-
"Winner": team1 if team1_val > team2_val else team2 if team2_val > team1_val else "Equal"
|
| 524 |
-
})
|
| 525 |
-
|
| 526 |
-
comparison_df = pd.DataFrame(comparison_data)
|
| 527 |
-
|
| 528 |
-
# Team comparison visualization
|
| 529 |
-
st.subheader("Team Performance Comparison", divider='gray')
|
| 530 |
-
|
| 531 |
-
# Parallel coordinates plot
|
| 532 |
-
fig = px.parallel_coordinates(
|
| 533 |
-
comparison_df,
|
| 534 |
-
color="Winner",
|
| 535 |
-
dimensions=[team1, team2, "Metric"],
|
| 536 |
-
labels={team1: team1, team2: team2, "Metric": "Metric"},
|
| 537 |
-
color_continuous_scale=px.colors.diverging.Tealrose,
|
| 538 |
-
title="Team Metrics Parallel Comparison"
|
| 539 |
-
)
|
| 540 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 541 |
-
|
| 542 |
-
# Team strength spider chart
|
| 543 |
-
fig = go.Figure()
|
| 544 |
-
|
| 545 |
-
fig.add_trace(go.Scatterpolar(
|
| 546 |
-
r=comparison_df[team1],
|
| 547 |
-
theta=comparison_df['Metric'],
|
| 548 |
-
fill='toself',
|
| 549 |
-
name=team1,
|
| 550 |
-
line=dict(color='#1f77b4'),
|
| 551 |
-
fillcolor='rgba(31, 119, 180, 0.2)'
|
| 552 |
-
))
|
| 553 |
-
|
| 554 |
-
fig.add_trace(go.Scatterpolar(
|
| 555 |
-
r=comparison_df[team2],
|
| 556 |
-
theta=comparison_df['Metric'],
|
| 557 |
-
fill='toself',
|
| 558 |
-
name=team2,
|
| 559 |
-
line=dict(color='#ff7f0e'),
|
| 560 |
-
fillcolor='rgba(255, 127, 14, 0.2)'
|
| 561 |
-
))
|
| 562 |
-
|
| 563 |
-
fig.update_layout(
|
| 564 |
-
polar=dict(
|
| 565 |
-
radialaxis=dict(
|
| 566 |
-
visible=True,
|
| 567 |
-
range=[0, max(max(comparison_df[team1]), max(comparison_df[team2]))*1.2]
|
| 568 |
-
)),
|
| 569 |
-
showlegend=True,
|
| 570 |
-
title="Team Strength Radar Comparison",
|
| 571 |
-
template='plotly_white',
|
| 572 |
-
height=500
|
| 573 |
-
)
|
| 574 |
-
|
| 575 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 576 |
-
|
| 577 |
-
# Top players comparison
|
| 578 |
-
st.subheader("Top Players Comparison", divider='gray')
|
| 579 |
-
|
| 580 |
-
top_n = st.slider("Number of Top Players to Compare", 3, 10, 5)
|
| 581 |
-
|
| 582 |
-
for metric, col in [("Batting", f"batting_impact_{format_type}"),
|
| 583 |
-
("Bowling", f"bowling_impact_{format_type}")]:
|
| 584 |
-
st.markdown(f"### Top {top_n} {metric} Players")
|
| 585 |
-
|
| 586 |
-
team1_top = team1_players.nlargest(top_n, col)[['Player', col]]
|
| 587 |
-
team2_top = team2_players.nlargest(top_n, col)[['Player', col]]
|
| 588 |
-
|
| 589 |
-
# Create comparison bar chart
|
| 590 |
-
team1_top['Team'] = team1
|
| 591 |
-
team2_top['Team'] = team2
|
| 592 |
-
combined = pd.concat([team1_top, team2_top])
|
| 593 |
-
|
| 594 |
-
fig = px.bar(
|
| 595 |
-
combined,
|
| 596 |
-
x='Player',
|
| 597 |
-
y=col,
|
| 598 |
-
color='Team',
|
| 599 |
-
barmode='group',
|
| 600 |
-
title=f"Top {top_n} {metric} Players Comparison",
|
| 601 |
-
color_discrete_map={team1: '#1f77b4', team2: '#ff7f0e'},
|
| 602 |
-
labels={col: f"{metric} Impact Score"}
|
| 603 |
-
)
|
| 604 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 605 |
-
|
| 606 |
-
# Main App
|
| 607 |
def main():
|
| 608 |
st.title("๐ Cricket Analytics Dashboard PRO")
|
| 609 |
st.markdown("""
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
background-color: #f5f5f5;
|
| 613 |
-
}
|
| 614 |
-
.metric-label {
|
| 615 |
-
font-size: 14px;
|
| 616 |
-
color: #666;
|
| 617 |
-
margin-bottom: 5px;
|
| 618 |
-
}
|
| 619 |
-
.metric-value {
|
| 620 |
-
font-size: 24px;
|
| 621 |
-
font-weight: bold;
|
| 622 |
-
color: #333;
|
| 623 |
-
}
|
| 624 |
-
</style>
|
| 625 |
-
""", unsafe_allow_html=True)
|
| 626 |
|
| 627 |
st.sidebar.header("Navigation")
|
| 628 |
app_mode = st.sidebar.selectbox("Choose Analysis Mode", [
|
|
@@ -631,7 +217,7 @@ def main():
|
|
| 631 |
"๐ด Team Comparison",
|
| 632 |
"๐ฎ Score Predictor",
|
| 633 |
"๐ Match Winner Prediction"
|
| 634 |
-
]
|
| 635 |
|
| 636 |
if app_mode == "๐ Player Statistics":
|
| 637 |
show_player_stats()
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Page Configuration
|
| 11 |
st.set_page_config(
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# Custom CSS
|
| 27 |
+
st.markdown("""
|
| 28 |
+
<style>
|
| 29 |
+
.metric-card {
|
| 30 |
+
background-color: #f8f9fa;
|
| 31 |
+
border-radius: 10px;
|
| 32 |
+
padding: 15px;
|
| 33 |
+
margin-bottom: 15px;
|
| 34 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 35 |
+
}
|
| 36 |
+
.metric-title {
|
| 37 |
+
font-size: 14px;
|
| 38 |
+
color: #6c757d;
|
| 39 |
+
margin-bottom: 5px;
|
| 40 |
+
}
|
| 41 |
+
.metric-value {
|
| 42 |
+
font-size: 24px;
|
| 43 |
+
font-weight: bold;
|
| 44 |
+
color: #212529;
|
| 45 |
+
}
|
| 46 |
+
.player-header {
|
| 47 |
+
display: flex;
|
| 48 |
+
align-items: center;
|
| 49 |
+
gap: 15px;
|
| 50 |
+
margin-bottom: 20px;
|
| 51 |
+
}
|
| 52 |
+
.team-logo {
|
| 53 |
+
width: 60px;
|
| 54 |
+
height: 60px;
|
| 55 |
+
object-fit: contain;
|
| 56 |
+
}
|
| 57 |
+
</style>
|
| 58 |
+
""", unsafe_allow_html=True)
|
| 59 |
|
| 60 |
+
# Data Loading
|
| 61 |
@st.cache_data
|
| 62 |
def load_data():
|
| 63 |
+
# In production, replace with actual API call
|
| 64 |
+
df = pd.read_csv("My_Cricket.csv")
|
| 65 |
+
|
| 66 |
+
# Data preprocessing
|
| 67 |
+
formats = ['Test', 'ODI', 'T20', 'IPL']
|
| 68 |
+
for fmt in formats:
|
|
|
|
|
|
|
| 69 |
# Create composite metrics
|
| 70 |
+
df[f'batting_impact_{fmt}'] = df[f'batting_Average_{fmt}'] * df[f'batting_SR_{fmt}'] / 100
|
| 71 |
+
df[f'bowling_impact_{fmt}'] = (1/df[f'bowling_{fmt}_Avg'].replace(0, 1)) * (1/df[f'bowling_{fmt}_Eco'].replace(0, 1))
|
| 72 |
+
df[f'player_value_{fmt}'] = df[f'batting_impact_{fmt}'] + df[f'bowling_impact_{fmt}']
|
| 73 |
+
|
| 74 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
df = load_data()
|
| 77 |
|
| 78 |
# Utility Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def styled_metric(label, value, delta=None, help_text=None):
|
| 80 |
"""Create a styled metric card"""
|
| 81 |
+
with st.container():
|
| 82 |
+
st.markdown(f"""
|
| 83 |
+
<div class="metric-card">
|
| 84 |
+
<div class="metric-title">{label}</div>
|
| 85 |
+
<div class="metric-value">{value}</div>
|
| 86 |
+
</div>
|
| 87 |
+
""", unsafe_allow_html=True)
|
| 88 |
if delta:
|
| 89 |
st.metric("", "", delta=delta, help=help_text)
|
| 90 |
|
| 91 |
+
def get_team_logo(team_name):
|
| 92 |
+
"""Get team logo (placeholder - replace with actual implementation)"""
|
| 93 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Page Components
|
| 96 |
def show_player_stats():
|
| 97 |
st.header("๐ Player Statistics Explorer", divider='rainbow')
|
| 98 |
|
| 99 |
+
col1, col2 = st.columns(2)
|
| 100 |
with col1:
|
| 101 |
+
player = st.selectbox("Select Player", df['Player'].unique())
|
| 102 |
+
with col2:
|
| 103 |
+
format_type = st.selectbox("Select Format", ["Test", "ODI", "T20", "IPL"])
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
player_data = df[df['Player'] == player].iloc[0]
|
| 106 |
|
| 107 |
+
# Player header with team logo
|
| 108 |
+
with st.container():
|
| 109 |
+
st.markdown(f"""
|
| 110 |
+
<div class="player-header">
|
| 111 |
+
<h2>{player}</h2>
|
| 112 |
+
{f'<img src="{get_team_logo(player_data["Player_Team"])}" class="team-logo">' if get_team_logo(player_data["Player_Team"]) else ''}
|
| 113 |
+
</div>
|
| 114 |
+
""", unsafe_allow_html=True)
|
| 115 |
+
|
| 116 |
+
# Key metrics
|
| 117 |
+
cols = st.columns(4)
|
| 118 |
+
with cols[0]:
|
| 119 |
styled_metric("Matches", player_data[f"Matches_{format_type}"])
|
| 120 |
+
with cols[1]:
|
| 121 |
styled_metric("Runs", player_data[f"batting_Runs_{format_type}"])
|
| 122 |
+
with cols[2]:
|
| 123 |
styled_metric("Average", f"{player_data[f'batting_Average_{format_type}']:.1f}")
|
| 124 |
+
with cols[3]:
|
| 125 |
styled_metric("Strike Rate", f"{player_data[f'batting_SR_{format_type}']:.1f}")
|
| 126 |
|
| 127 |
+
# Performance tabs
|
| 128 |
+
tab1, tab2, tab3 = st.tabs(["Batting Profile", "Bowling Profile", "Milestones"])
|
| 129 |
|
| 130 |
with tab1:
|
| 131 |
+
st.subheader("Batting Performance")
|
| 132 |
|
| 133 |
+
# Batting radar chart
|
| 134 |
+
batting_metrics = ['Average', 'SR', '50s', '100s', 'Boundary%']
|
| 135 |
batting_values = [
|
| 136 |
player_data[f'batting_Average_{format_type}'],
|
| 137 |
player_data[f'batting_SR_{format_type}'],
|
| 138 |
+
player_data[f'batting_50s_{format_type}'],
|
| 139 |
+
player_data[f'batting_100s_{format_type}'],
|
| 140 |
(player_data[f'batting_Fours_{format_type}']*4 + player_data[f'batting_Sixes_{format_type}']*6) /
|
| 141 |
+
max(1, player_data[f'batting_Runs_{format_type}']) * 100
|
|
|
|
| 142 |
]
|
| 143 |
|
| 144 |
+
fig = px.line_polar(
|
| 145 |
+
r=batting_values,
|
| 146 |
+
theta=batting_metrics,
|
| 147 |
+
line_close=True,
|
| 148 |
+
title="Batting Performance Radar"
|
| 149 |
+
)
|
| 150 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 151 |
|
| 152 |
+
# Innings progression
|
| 153 |
+
if 'Innings_Progress' in df.columns:
|
| 154 |
+
fig = px.line(
|
| 155 |
+
df[df['Player'] == player],
|
| 156 |
+
x='Innings_Progress',
|
| 157 |
+
y=f'batting_Runs_{format_type}',
|
| 158 |
+
title="Career Runs Progression"
|
|
|
|
| 159 |
)
|
|
|
|
| 160 |
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
with tab2:
|
| 163 |
if player_data[f'bowling_{format_type}_Innings'] > 0:
|
| 164 |
+
st.subheader("Bowling Performance")
|
| 165 |
|
| 166 |
+
# Bowling metrics
|
| 167 |
bowling_metrics = ['Wickets', 'Average', 'Economy', 'Strike Rate']
|
| 168 |
bowling_values = [
|
| 169 |
player_data[f'bowling_{format_type}_Wickets'],
|
|
|
|
| 172 |
player_data[f'bowling_{format_type}_SR']
|
| 173 |
]
|
| 174 |
|
| 175 |
+
fig = px.bar(
|
| 176 |
+
x=bowling_metrics,
|
| 177 |
+
y=bowling_values,
|
| 178 |
+
color=bowling_metrics,
|
| 179 |
+
title="Bowling Performance",
|
| 180 |
+
color_discrete_sequence=px.colors.qualitative.Pastel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
)
|
| 182 |
st.plotly_chart(fig, use_container_width=True)
|
| 183 |
else:
|
|
|
|
| 194 |
player_data[f'bowling_{format_type}_5w']
|
| 195 |
]
|
| 196 |
|
| 197 |
+
fig = px.pie(
|
| 198 |
+
names=milestones,
|
| 199 |
+
values=milestone_values,
|
|
|
|
| 200 |
title="Career Milestones",
|
| 201 |
+
hole=0.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
st.plotly_chart(fig, use_container_width=True)
|
| 204 |
|
| 205 |
+
# (Other functions like show_player_comparison, show_team_comparison, etc. would follow similar enhanced patterns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def main():
|
| 208 |
st.title("๐ Cricket Analytics Dashboard PRO")
|
| 209 |
st.markdown("""
|
| 210 |
+
Explore player statistics, compare teams, and predict match outcomes using advanced analytics.
|
| 211 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
st.sidebar.header("Navigation")
|
| 214 |
app_mode = st.sidebar.selectbox("Choose Analysis Mode", [
|
|
|
|
| 217 |
"๐ด Team Comparison",
|
| 218 |
"๐ฎ Score Predictor",
|
| 219 |
"๐ Match Winner Prediction"
|
| 220 |
+
])
|
| 221 |
|
| 222 |
if app_mode == "๐ Player Statistics":
|
| 223 |
show_player_stats()
|