import streamlit as st
import pandas as pd
import plotly.express as px
import os
# Streamlit Configuration
st.set_page_config(
page_title="Cricket Performance Analytics",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for Styling
st.markdown("""
""", unsafe_allow_html=True)
# Load Data
batting_path = "Teams_batting.csv"
bowling_path = "team_bowling.csv"
if not os.path.exists(batting_path) or not os.path.exists(bowling_path):
st.error("CSV files not found. Please check file paths.")
st.stop()
batting_df = pd.read_csv(batting_path)
bowling_df = pd.read_csv(bowling_path)
# Debugging: Print column names
st.write("Batting DataFrame Columns:", batting_df.columns.tolist())
st.write("Bowling DataFrame Columns:", bowling_df.columns.tolist())
# Correct column names
if "player_name" not in batting_df.columns:
st.error("Column 'player_name' not found! Available columns: " + ", ".join(batting_df.columns))
st.stop()
# Sidebar Team Selection
teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
selected_team = st.selectbox("Choose a Team", teams, format_func=lambda x: x.upper())
# Player Selection
batting_players = batting_df[batting_df['Country'] == selected_team]['player_name'].unique()
bowling_players = bowling_df[bowling_df['Country'] == selected_team]['player_name'].unique()
players = sorted(set(batting_players).union(bowling_players))
selected_player = st.selectbox("Choose a Player", players)
st.success("Code successfully executed!")
# Filter Player Data
player_batting = batting_df[(batting_df['player_name'] == selected_player) &
(batting_df[team_column] == selected_team)]
player_bowling = bowling_df[(bowling_df['player_name'] == selected_player) &
(bowling_df[team_column] == selected_team)]
# Main Dashboard
st.markdown(f'
{selected_player}\'s Cricket Analytics
', unsafe_allow_html=True)
# Key Metrics Section
st.markdown('', unsafe_allow_html=True)
if not player_batting.empty:
col1, col2, col3 = st.columns(3)
with col1:
total_runs = player_batting['runs'].sum()
st.markdown(f'', unsafe_allow_html=True)
with col2:
avg_sr = player_batting['sr'].mean().round(2)
st.markdown(f'', unsafe_allow_html=True)
with col3:
matches = len(player_batting)
st.markdown(f'', unsafe_allow_html=True)
else:
st.warning("No batting data available to display key metrics.")
# Visualization Section
tab1, tab2 = st.tabs(["Batting Analysis", "Bowling Analysis"])
with tab1:
if not player_batting.empty:
st.markdown('', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
fig_runs = px.pie(player_batting, values='runs', names='format',
title='Runs Distribution by Format',
color_discrete_sequence=px.colors.sequential.Blues)
fig_runs.update_layout(title_x=0.5)
st.plotly_chart(fig_runs, use_container_width=True)
with col2:
fig_sr = px.scatter(player_batting, x='format', y='sr',
size='runs', color='format',
title='Strike Rate Analysis',
hover_data=['runs'])
fig_sr.update_layout(title_x=0.5)
st.plotly_chart(fig_sr, use_container_width=True)
else:
st.info("No batting statistics available for this player.")
with tab2:
if not player_bowling.empty:
st.markdown('', unsafe_allow_html=True)
col3, col4 = st.columns(2)
with col3:
fig_wickets = px.bar(player_bowling, x='format', y='wickets',
color='format',
title='Wickets by Format',
color_discrete_sequence=px.colors.sequential.Reds)
fig_wickets.update_layout(title_x=0.5)
st.plotly_chart(fig_wickets, use_container_width=True)
with col4:
fig_eco = px.area(player_bowling, x='format', y='eco',
title='Economy Rate Trend',
color_discrete_sequence=['#2ECC71'])
fig_eco.update_layout(title_x=0.5)
st.plotly_chart(fig_eco, use_container_width=True)
else:
st.info("No bowling statistics available for this player.")
# Raw Data Expander
with st.expander("View Raw Data"):
if not player_batting.empty:
st.write("### Batting Statistics")
st.dataframe(player_batting.iloc[:, :17].style.set_properties(**{'background-color': '#F8F9F9'}))
if not player_bowling.empty:
st.write("### Bowling Statistics")
st.dataframe(player_bowling.iloc[:, :15].style.set_properties(**{'background-color': '#F8F9F9'}))