Spaces:
Sleeping
Sleeping
| 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(""" | |
| <style> | |
| .main-title { | |
| color: #2E86C1; | |
| font-size: 36px; | |
| font-weight: bold; | |
| text-align: center; | |
| padding: 20px 0; | |
| } | |
| .section-header { | |
| color: #1A5276; | |
| font-size: 24px; | |
| padding: 10px 0; | |
| } | |
| .metric-card { | |
| background-color: #EAF2F8; | |
| padding: 15px; | |
| border-radius: 10px; | |
| text-align: center; | |
| box-shadow: 0 2px 5px rgba(0,0,0,0.1); | |
| margin: 5px; | |
| } | |
| .metric-card h3 { | |
| color: #1A5276; | |
| font-size: 18px; | |
| margin-bottom: 5px; | |
| } | |
| .metric-card p { | |
| color: #2E86C1; | |
| font-size: 24px; | |
| font-weight: bold; | |
| margin: 0; | |
| } | |
| .stTabs [data-baseweb="tab-list"] { | |
| gap: 10px; | |
| } | |
| .stTabs [data-baseweb="tab"] { | |
| background-color: #F0F3F4; | |
| border-radius: 5px; | |
| padding: 10px 20px; | |
| color: #1A5276; | |
| } | |
| .stTabs [data-baseweb="tab"][aria-selected="true"] { | |
| background-color: #2E86C1; | |
| color: white; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Data Directory Setup | |
| data_folder = "cricket_data" | |
| os.makedirs(data_folder, exist_ok=True) | |
| # File Paths | |
| batting_path = "Batting_10_Teams_Final.csv" | |
| bowling_path = "Bowling_10_Teams_Final.csv" | |
| # Load Data | |
| try: | |
| batting_df = pd.read_csv(batting_path) | |
| bowling_df = pd.read_csv(bowling_path) | |
| except FileNotFoundError: | |
| st.error("CSV files not found. Please ensure 'Batting_10_Teams_Final.csv' and 'Bowling_10_Teams_Final.csv' are in the correct directory.") | |
| st.stop() | |
| # Sidebar Configuration | |
| with st.sidebar: | |
| st.image("https://cdn-icons-png.flaticon.com/512/1099/1099640.png", width=100) | |
| st.markdown("### 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) | |
| # Filter Player Data | |
| player_batting = batting_df[(batting_df['player_name'] == selected_player) & | |
| (batting_df['Country'] == selected_team)] | |
| player_bowling = bowling_df[(bowling_df['player_name'] == selected_player) & | |
| (bowling_df['Country'] == selected_team)] | |
| # Main Dashboard | |
| st.markdown(f'<div class="main-title">{selected_player}\'s Cricket Analytics</div>', unsafe_allow_html=True) | |
| # Key Metrics Section | |
| st.markdown('<div class="section-header">Key Performance Metrics</div>', 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'<div class="metric-card"><h3>Total Runs</h3><p>{total_runs}</p></div>', unsafe_allow_html=True) | |
| with col2: | |
| avg_sr = player_batting['SR'].mean().round(2) | |
| st.markdown(f'<div class="metric-card"><h3>Avg Strike Rate</h3><p>{avg_sr}</p></div>', unsafe_allow_html=True) | |
| with col3: | |
| matches = len(player_batting) | |
| st.markdown(f'<div class="metric-card"><h3>Matches</h3><p>{matches}</p></div>', 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('<div class="section-header">Batting Performance</div>', 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('<div class="section-header">Bowling Performance</div>', 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'})) | |