Cric_Metrics / pages /1player_information.py
Sathwikchowdary's picture
Update pages/1player_information.py
4eb15b3 verified
raw
history blame
8.67 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
import base64
from streamlit_extras.switch_page_button import switch_page
# Load and encode background image
def get_base64_bg(image_path):
with open(image_path, "rb") as img_file:
encoded = base64.b64encode(img_file.read()).decode()
return f"data:image/jpg;base64,{encoded}"
background_img = get_base64_bg("background.jpg")
# Inject custom CSS with background
st.markdown(
f"""
<style>
.stApp {{
background-image: url("{background_img}");
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}}
.header {{
text-align: center;
color: red;
font-size: 30px;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
position: relative;
padding-top: 20px;
}}
.logo {{
position: absolute;
right: 20px;
top: 10px;
width: 80px;
height: auto;
}}
.sub-header {{
color: darkorange;
font-size: 26px;
font-weight: bold;
margin-top: 30px;
}}
.text {{
font-size: 17px;
color: black;
font-style: italic;
background-color: rgba(255, 255, 255, 0.75);
padding: 15px;
border-radius: 10px;
margin-bottom: 20px;
}}
.footer {{
font-size: 14px;
color: #333;
margin-top: 50px;
text-align: center;
font-style: italic;
}}
</style>
""",
unsafe_allow_html=True
)
# App Header with Logo
st.markdown(
"""
<div class='header'>
🏏 Cric Metrics - AI-Powered Cricket Insights 🏏
<img src='1.png' class='logo'>
</div>
""",
unsafe_allow_html=True
)
# Set page configuration
st.set_page_config(page_title="Career Insights", layout="wide")
# Load data
df = pd.read_csv("Reduced_final_teams.csv")
# Get unique player names
player_names = df["Player"].unique()
# Search box for filtering player names
search_query = st.text_input("Search Player Name:")
filtered_players = [name for name in player_names if search_query.lower() in name.lower()] if search_query else player_names
if len(filtered_players) == 0:
st.warning("Player not found. Please try a different name.")
# Player selection dropdown
selected_player = st.selectbox("Select Player", filtered_players)
# Buttons for Batting and Bowling
show_batting = st.button("Show Batting Stats")
show_bowling = st.button("Show Bowling Stats")
if selected_player:
player_data = df[df["Player"] == selected_player].iloc[0]
labels = ["Test", "ODI", "T20", "IPL"]
if show_batting:
st.subheader(f"Batting Stats for {selected_player}")
col1, col2 = st.columns(2)
with col1:
# Pie Chart - Matches Played
matches = [
player_data.get("Matches_Test", 0),
player_data.get("Matches_ODI", 0),
player_data.get("Matches_T20", 0),
player_data.get("Matches_IPL", 0)
]
fig, ax = plt.subplots()
ax.pie(matches, labels=labels, autopct="%1.1f%%", startangle=90)
ax.set_title(f"Matches Played by {selected_player}")
st.pyplot(fig)
with col2:
# Bar Chart - Runs Scored
batting_runs = [
player_data.get("batting_Runs_Test", 0),
player_data.get("batting_Runs_ODI", 0),
player_data.get("batting_Runs_T20", 0),
player_data.get("batting_Runs_IPL", 0)
]
fig, ax = plt.subplots()
ax.bar(labels, batting_runs, color=["gold", "green", "blue", "red"])
ax.set_ylabel("Runs Scored")
ax.set_title(f"Runs Scored by {selected_player}")
st.pyplot(fig)
col3, col4 = st.columns(2)
with col3:
# Stacked Bar Chart - 100s and 50s
hundreds = [
player_data.get("batting_100s_Test", 0),
player_data.get("batting_100s_ODI", 0),
player_data.get("batting_100s_T20", 0),
player_data.get("batting_100s_IPL", 0)
]
fifties = [
player_data.get("batting_50s_Test", 0),
player_data.get("batting_50s_ODI", 0),
player_data.get("batting_50s_T20", 0),
player_data.get("batting_50s_IPL", 0)
]
fig, ax = plt.subplots()
ax.bar(labels, hundreds, label="100s", color="gold")
ax.bar(labels, fifties, label="50s", color="blue", bottom=hundreds)
ax.set_ylabel("Count")
ax.set_title(f"Centuries & Fifties by {selected_player}")
ax.legend()
st.pyplot(fig)
with col4:
# Line Chart - Strike Rate & Average
strike_rate = [
player_data.get("batting_SR_Test", 0),
player_data.get("batting_SR_ODI", 0),
player_data.get("batting_SR_T20", 0),
player_data.get("batting_SR_IPL", 0)
]
batting_avg = [
player_data.get("batting_Average_Test", 0),
player_data.get("batting_Average_ODI", 0),
player_data.get("batting_Average_T20", 0),
player_data.get("batting_Average_IPL", 0)
]
fig, ax = plt.subplots()
ax.plot(labels, strike_rate, marker='o', linestyle='-', color='red', label="Strike Rate")
ax.plot(labels, batting_avg, marker='s', linestyle='--', color='green', label="Batting Average")
ax.set_ylabel("Value")
ax.set_title(f"Strike Rate & Batting Average of {selected_player}")
ax.legend()
st.pyplot(fig)
# Bar Chart - Balls Faced
batting_balls = [
player_data.get("batting_Balls_Test", 0),
player_data.get("batting_Balls_ODI", 0),
player_data.get("batting_Balls_T20", 0),
player_data.get("batting_Balls_IPL", 0)
]
fig, ax = plt.subplots(figsize=(5,3))
ax.bar(labels, batting_balls, color=["red", "green", "blue", "purple"])
ax.set_ylabel("Balls Faced")
ax.set_title(f"Balls Faced by {selected_player}")
st.pyplot(fig)
if show_bowling:
st.subheader(f"Bowling Stats for {selected_player}")
col1, col2 = st.columns(2)
with col1:
# Pie Chart - Bowling Averages
bowling_avg = [
0 if pd.isna(player_data.get("bowling_Test_Avg", 0)) else float(player_data.get("bowling_Test_Avg", 0)),
0 if pd.isna(player_data.get("bowling_ODI_Avg", 0)) else float(player_data.get("bowling_ODI_Avg", 0)),
0 if pd.isna(player_data.get("bowling_T20_Avg", 0)) else float(player_data.get("bowling_T20_Avg", 0)),
0 if pd.isna(player_data.get("bowling_IPL_Avg", 0)) else float(player_data.get("bowling_IPL_Avg", 0))
]
fig, ax = plt.subplots()
ax.pie(bowling_avg, labels=labels, autopct="%1.1f%%", startangle=90)
ax.set_title(f"Bowling Averages of {selected_player}")
st.pyplot(fig)
with col2:
# Bar Chart - Bowling Innings
bowling_innings = [
player_data.get("bowling_Test_Innings", 0),
player_data.get("bowling_ODI_Innings", 0),
player_data.get("bowling_T20_Innings", 0),
player_data.get("bowling_IPL_Innings", 0)
]
fig, ax = plt.subplots()
ax.bar(labels, bowling_innings, color=["blue", "green", "purple", "orange"])
ax.set_ylabel("Innings Bowled")
ax.set_title(f"Bowling Innings of {selected_player}")
st.pyplot(fig)
# Balls Bowled
balls_bowled = [
player_data.get("bowling_Test_Balls", 0),
player_data.get("bowling_ODI_Balls", 0),
player_data.get("bowling_T20_Balls", 0),
player_data.get("bowling_IPL_Balls", 0)
]
fig, ax = plt.subplots(figsize=(5, 3))
ax.bar(labels, balls_bowled, color=["red", "yellow", "blue", "green"])
ax.set_ylabel("Balls Bowled")
ax.set_title(f"Balls Bowled by {selected_player}")
st.pyplot(fig)