import streamlit as st
import pandas as pd
import plotly.express as px
# Streamlit App Configuration
st.set_page_config(page_title="BowLChaL - Cricket Dashboard", layout="wide")
# Custom CSS for animations and styling
st.markdown("""
""", unsafe_allow_html=True)
# File paths (root directory)
batting_path = "Batting_10_Teams_Final.csv"
bowling_path = "Bowling_10_Teams_Final.csv"
# Load datasets with caching
@st.cache_data
def load_data(batting_path, bowling_path):
try:
batting_df = pd.read_csv(batting_path)
bowling_df = pd.read_csv(bowling_path)
return batting_df, bowling_df
except FileNotFoundError:
st.error("Data files not found. Please check file paths.")
st.stop()
# Load data
batting_df, bowling_df = load_data(batting_path, bowling_path)
# Data validation
if batting_df.empty or bowling_df.empty:
st.warning("No data available in the datasets")
st.stop()
# Title
st.markdown('
BowLChaL
', unsafe_allow_html=True)
# Team Selection in 2x5 Grid
teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
st.markdown('
', unsafe_allow_html=True)
# First row (5 teams)
row1 = st.columns(5)
for i, t in enumerate(teams[:5]):
with row1[i]:
# Display the styled button using st.markdown
button_html = f"""
{t}
"""
st.markdown(button_html, unsafe_allow_html=True)
# Hidden Streamlit button to handle the click
if st.button("", key=f"hidden_team_{t}_row1", help=f"Select {t}", on_click=lambda t=t: st.session_state.update({'selected_team': t})):
pass
# Second row (5 teams)
row2 = st.columns(5)
for i, t in enumerate(teams[5:10]):
with row2[i]:
# Display the styled button using st.markdown
button_html = f"""
{t}
"""
st.markdown(button_html, unsafe_allow_html=True)
# Hidden Streamlit button to handle the click
if st.button("", key=f"hidden_team_{t}_row2", help=f"Select {t}", on_click=lambda t=t: st.session_state.update({'selected_team': t})):
pass
st.markdown('
', unsafe_allow_html=True)
# Player Selection and Stats Display
if 'selected_team' in st.session_state:
team = st.session_state['selected_team']
st.markdown(f'
', unsafe_allow_html=True)
if stats_type in ["All Stats", "Batting Stats"] and not player_batting.empty:
st.markdown('
', unsafe_allow_html=True)
st.markdown('
Batting Stats
', unsafe_allow_html=True)
st.dataframe(player_batting.iloc[:min(17, len(player_batting)), :17])
# Plotly Visualizations for Batting
col1, col2 = st.columns(2)
with col1:
fig_runs = px.bar(player_batting, x='Format', y='Runs', color='Format', title='Runs by Format',
color_discrete_sequence=px.colors.qualitative.Bold)
fig_runs.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", font_color="white")
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 vs Runs by Format', hover_data=['Innings'],
color_discrete_sequence=px.colors.qualitative.Vivid)
fig_sr.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", font_color="white")
st.plotly_chart(fig_sr, use_container_width=True)
st.markdown('
', unsafe_allow_html=True)
if stats_type in ["All Stats", "Bowling Stats"] and not player_bowling.empty:
st.markdown('
', unsafe_allow_html=True)
st.markdown('
Bowling Stats
', unsafe_allow_html=True)
st.dataframe(player_bowling.iloc[:min(15, len(player_bowling)), :15])
# Plotly Visualizations for Bowling
col3, col4 = st.columns(2)
with col3:
fig_wickets = px.pie(player_bowling, names='Format', values='Wickets', title='Wickets Distribution by Format',
color_discrete_sequence=px.colors.sequential.Plasma)
fig_wickets.update_layout(paper_bgcolor="rgba(0,0,0,0)", font_color="white")
st.plotly_chart(fig_wickets, use_container_width=True)
with col4:
fig_eco = px.line(player_bowling, x='Format', y='Eco', title='Economy Rate by Format',
markers=True, color_discrete_sequence=['#00ff99'])
fig_eco.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", font_color="white")
st.plotly_chart(fig_eco, use_container_width=True)
st.markdown('
', unsafe_allow_html=True)
# Additional Feature: Player Comparison Button
if st.button("Compare with Another Player", key="compare"):
compare_player = st.selectbox("Select Player to Compare", [p for p in players if p != player], key="compare_select")
st.session_state['compare_player'] = compare_player
st.write(f"Comparison between {player} and {compare_player} coming soon!")
# Footer with Extra Flair
st.markdown("""
""", unsafe_allow_html=True)
# JavaScript to handle click events for custom buttons
st.markdown("""
""", unsafe_allow_html=True)