File size: 5,993 Bytes
dfb892c
 
 
486d1a0
d088c08
486d1a0
 
 
 
 
 
019b80e
486d1a0
 
5b62439
486d1a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b62439
486d1a0
5b62439
486d1a0
b48c470
 
97e38ab
1052de5
 
97e38ab
 
1052de5
 
97e38ab
1052de5
 
 
97e38ab
1052de5
 
 
41060fc
9433514
1052de5
 
 
97e38ab
1052de5
 
 
 
97e38ab
1052de5
41060fc
1052de5
486d1a0
 
97e38ab
486d1a0
97e38ab
019b80e
486d1a0
 
dfb892c
486d1a0
 
 
 
 
97e38ab
486d1a0
 
97e38ab
486d1a0
 
 
 
 
 
dfb892c
486d1a0
 
dfb892c
486d1a0
 
 
 
 
 
97e38ab
486d1a0
 
 
 
 
 
97e38ab
 
486d1a0
97e38ab
486d1a0
 
 
 
dfb892c
486d1a0
 
 
 
 
 
97e38ab
 
486d1a0
 
 
 
 
 
97e38ab
486d1a0
 
 
 
 
 
41060fc
486d1a0
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;
    }
    </style>
""", 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'<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'}))