Certainly! Let's create a Python web application using Flask to display college baseball game predictions. In this setup, we'll assume that the matchups are provided as two lists of runs for each team, and the winner is determined by comparing the total runs scored by each team.
Project Structure
baseball_predictions/
├── app.py
├── templates/
│ └── index.html
├── static/
│ └── style.css
└── data.json
JSON Structure
Your JSON file (data.json) should look something like this:
{
"matchups": [
{
"team1": "Team A",
"team2": "Team B",
"runs": [
[5, 3, 7, 2], // Team A runs
[4, 6, 3, 5] // Team B runs
]
},
{
"team1": "Team C",
"team2": "Team D",
"runs": [
[3, 4, 2, 6], // Team C runs
[5, 2, 7, 1] // Team D runs
]
}
// More matchups...
]
}
Flask App Code
We'll use Flask to create a simple web server that reads the JSON file, processes the data, and displays the predictions.
app.py
from flask import Flask, render_template, jsonify
import json
app = Flask(__name__)
def load_data(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def calculate_wins(matchups):
results = []
for matchup in matchups:
team1 = matchup['team1']
team2 = matchup['team2']
runs = matchup['runs']
team1_runs = runs[0]
team2_runs = runs[1]
team1_wins = sum(1 for r1, r2 in zip(team1_runs, team2_runs) if r1 > r2)
team2_wins = sum(1 for r1, r2 in zip(team1_runs, team2_runs) if r2 > r1)
ties = len(team1_runs) - team1_wins - team2_wins
total_simulations = len(team1_runs)
team1_win_probability = (team1_wins / total_simulations) * 100
team2_win_probability = (team2_wins / total_simulations) * 100
results.append({
'team1': team1,
'team2': team2,
'team1_win_probability': team1_win_probability,
'team2_win_probability': team2_win_probability,
'ties': ties
})
return results
@app.route('/')
def index():
data = load_data('data.json')
matchups = data.get('matchups', [])
results = calculate_wins(matchups)
return render_template('index.html', results=results)
if __name__ == '__main__':
app.run(debug=True)
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>College Baseball Predictions</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>College Baseball Game Predictions</h1>
<div id="predictions-container">
{% for result in results %}
<div class="matchup">
<h2>{{ result.team1 }} vs {{ result.team2 }}</h2>
<p>{{ result.team1 }} win probability: {{ result.team1_win_probability }}%</p>
<p>{{ result.team2 }} win probability: {{ result.team2_win_probability }}%</p>
<p>Ties: {{ result.ties }}</p>
</div>
{% endfor %}
</div>
</body>
</html>
static/style.css
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f4f4f4;
}
h1 {
color: #333;
text-align: center;
}
.matchup {
border: 1px solid #ddd;
padding: 15px;
margin-bottom: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.matchup h2 {
color: #007bff;
}
.matchup p {
font-size: 16px;
margin: 5px 0;
}
Running the App
- Install Flask: If you haven't already, install Flask using pip:
pip install Flask
- Run the App: Navigate to the
baseball_predictions directory and run the Flask app:
python app.py
- Open in Browser: Open your web browser and go to
http://127.0.0.1:5000/ to see the predictions.
How It Works
- Loading Data: The
load_data function reads the data.json file.
- Calculating Wins: The
calculate_wins function processes the matchups, comparing the runs for each team to determine the winner of each simulation.
- Displaying Data: The results are passed to the
index.html template and displayed in a simple card-like format for each matchup.
This setup should provide a clear and functional web app for displaying college baseball game predictions based on simulation runs.