Mpavan45 commited on
Commit
3ff902e
·
verified ·
1 Parent(s): e144b08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -91
app.py CHANGED
@@ -1,94 +1,71 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import seaborn as sns
5
-
6
- # Set Streamlit page configuration
7
- st.set_page_config(page_title="CrickMetrics", page_icon="🏏", layout="wide")
8
-
9
- # Title and description
10
- st.title("🏏 CrickMetrics: Player Performance Dashboard")
11
- st.markdown("Upload batting and bowling CSVs to explore player metrics with interactive visualizations.")
12
-
13
- # Sidebar for uploading CSV files
14
- st.sidebar.header("Upload CSV Files")
15
- batting_file = st.sidebar.file_uploader("Upload Batting CSV", type=["csv"])
16
- bowling_file = st.sidebar.file_uploader("Upload Bowling CSV", type=["csv"])
17
-
18
- # Check if both files are uploaded
19
- if batting_file and bowling_file:
20
- # Load CSVs
21
- batting_df = pd.read_csv(batting_file)
22
- bowling_df = pd.read_csv(bowling_file)
23
-
24
- # Extract unique teams from both CSVs
25
- teams = sorted(set(batting_df['Team'].unique()).union(bowling_df['Team'].unique()))
26
- selected_team = st.sidebar.selectbox("Select a Team", teams)
27
-
28
- # Extract unique players for the selected team from both CSVs
29
- batting_players = set(batting_df[batting_df['Team'] == selected_team]['Player'].unique())
30
- bowling_players = set(bowling_df[bowling_df['Team'] == selected_team]['Player'].unique())
31
-
32
- # Combine players from both CSVs
33
- all_players = sorted(batting_players.union(bowling_players))
34
- selected_player = st.sidebar.selectbox("Select a Player", all_players)
35
-
36
- # Filter the data for the selected player
37
- player_batting_data = batting_df[(batting_df['Player'] == selected_player) & (batting_df['Team'] == selected_team)]
38
- player_bowling_data = bowling_df[(bowling_df['Player'] == selected_player) & (bowling_df['Team'] == selected_team)]
39
-
40
- if not player_batting_data.empty and not player_bowling_data.empty:
41
- # Display the team and player name
42
- st.sidebar.markdown(f"### 🏏 **Team:** {selected_team}")
43
- st.sidebar.markdown(f"### 👤 **Player:** {selected_player}")
44
-
45
- # Display metrics side by side
46
- col1, col2 = st.columns(2)
47
-
48
- # Batting Metrics Visualization
49
- with col1:
50
- st.subheader(f"{selected_player} - Batting Metrics")
51
-
52
- fig, ax = plt.subplots(1, 2, figsize=(12, 5))
53
-
54
- # Batting Runs vs Matches
55
- sns.barplot(x="Matches", y="Runs", data=player_batting_data, ax=ax[0], color='blue')
56
- ax[0].set_title("Matches vs Runs")
57
- ax[0].set_xlabel("Matches")
58
- ax[0].set_ylabel("Runs")
59
-
60
- # Batting Average over Years
61
- sns.lineplot(x="Year", y="Average", data=player_batting_data, ax=ax[1], color='green')
62
- ax[1].set_title("Batting Average over Years")
63
- ax[1].set_xlabel("Year")
64
- ax[1].set_ylabel("Average")
65
-
66
- plt.tight_layout()
67
- st.pyplot(fig)
68
-
69
- # Bowling Metrics Visualization
70
- with col2:
71
- st.subheader(f"{selected_player} - Bowling Metrics")
72
-
73
- fig, ax = plt.subplots(1, 2, figsize=(12, 5))
74
-
75
- # Bowling Wickets vs Matches
76
- sns.barplot(x="Matches", y="Wickets", data=player_bowling_data, ax=ax[0], color='orange')
77
- ax[0].set_title("Matches vs Wickets")
78
- ax[0].set_xlabel("Matches")
79
- ax[0].set_ylabel("Wickets")
80
-
81
- # Bowling Economy over Years
82
- sns.lineplot(x="Year", y="Economy", data=player_bowling_data, ax=ax[1], color='red')
83
- ax[1].set_title("Bowling Economy over Years")
84
- ax[1].set_xlabel("Year")
85
- ax[1].set_ylabel("Economy")
86
-
87
- plt.tight_layout()
88
- st.pyplot(fig)
89
-
90
- else:
91
- st.warning(f"No data found for {selected_player}. Please upload valid CSV files.")
92
-
93
  else:
94
- st.info("Please upload both Batting and Bowling CSV files to view player metrics.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
+ import os
5
+
6
+ # Streamlit App
7
+ st.set_page_config(page_title="Cricket Player Stats Dashboard", layout="wide")
8
+
9
+ # Create a folder to save CSVs if not exists
10
+ data_folder = "data"
11
+ os.makedirs(data_folder, exist_ok=True)
12
+
13
+ # File paths
14
+ batting_path = os.path.join(data_folder, "batting.csv")
15
+ bowling_path = os.path.join(data_folder, "bowling.csv")
16
+
17
+ # Upload CSVs only if they are not already saved
18
+ if not (os.path.exists(batting_path) and os.path.exists(bowling_path)):
19
+ uploaded_batting = st.sidebar.file_uploader("Upload Batting CSV", type=["csv"], key="batting")
20
+ uploaded_bowling = st.sidebar.file_uploader("Upload Bowling CSV", type=["csv"], key="bowling")
21
+
22
+ if uploaded_batting and uploaded_bowling:
23
+ batting_df = pd.read_csv(uploaded_batting)
24
+ bowling_df = pd.read_csv(uploaded_bowling)
25
+
26
+ # Save the files permanently
27
+ batting_df.to_csv(batting_path, index=False)
28
+ bowling_df.to_csv(bowling_path, index=False)
29
+ st.success("CSV files saved successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  else:
31
+ # Load saved CSVs
32
+ batting_df = pd.read_csv(batting_path)
33
+ bowling_df = pd.read_csv(bowling_path)
34
+
35
+ # Extract unique teams and players
36
+ teams = sorted(set(batting_df['Country'].unique()).union(bowling_df['Country'].unique()))
37
+ team = st.sidebar.selectbox("Select Team", teams)
38
+
39
+ # Filter players by team
40
+ batting_players = batting_df[batting_df['Country'] == team]['player_name'].unique()
41
+ bowling_players = bowling_df[bowling_df['Country'] == team]['player_name'].unique()
42
+ players = sorted(set(batting_players).union(bowling_players))
43
+ player = st.sidebar.selectbox("Select Player", players)
44
+
45
+ # Filter data for the selected player
46
+ player_batting = batting_df[(batting_df['player_name'] == player) & (batting_df['Country'] == team)]
47
+ player_bowling = bowling_df[(bowling_df['player_name'] == player) & (bowling_df['Country'] == team)]
48
+
49
+ st.title(f"{player}'s Performance Dashboard")
50
+
51
+ # Batting Visualization
52
+ if not player_batting.empty:
53
+ st.subheader("Batting Stats")
54
+ col1, col2 = st.columns(2)
55
+ with col1:
56
+ fig_bat_runs = px.bar(player_batting, x='Format', y='Runs', color='Format', title='Runs by Format')
57
+ st.plotly_chart(fig_bat_runs, use_container_width=True)
58
+ with col2:
59
+ fig_bat_sr = px.line(player_batting, x='Format', y='SR', title='Strike Rate by Format')
60
+ st.plotly_chart(fig_bat_sr, use_container_width=True)
61
+
62
+ # Bowling Visualization
63
+ if not player_bowling.empty:
64
+ st.subheader("Bowling Stats")
65
+ col3, col4 = st.columns(2)
66
+ with col3:
67
+ fig_bowl_wickets = px.bar(player_bowling, x='Format', y='Wickets', color='Format', title='Wickets by Format')
68
+ st.plotly_chart(fig_bowl_wickets, use_container_width=True)
69
+ with col4:
70
+ fig_bowl_eco = px.line(player_bowling, x='Format', y='Eco', title='Economy Rate by Format')
71
+ st.plotly_chart(fig_bowl_eco, use_container_width=True)