Skip to content

proj-jra/OpenF1_tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenF1 API: Interactive Strategy Dashboard Tutorial with Streamlit & Plotly

Welcome to this tutorial, where you'll learn to build an interactive Formula 1 strategy dashboard using the OpenF1 API, Streamlit, and Plotly. This hands-on project is ideal for those interested in data visualization, sports analytics, and modern Python web tools.

Overview

This dashboard enables users to:

  • Select a race by year and country
  • View lap times per driver with pit stop flags
  • Analyze tire strategy over the race distance
  • Compare pit stop durations

Technologies used:

  • OpenF1 API for motorsport telemetry data
  • Pandas for data handling
  • Plotly for interactive charts
  • Streamlit for web UI

Project Structure

openf1-dashboard-tutorial/
├── app/
│   ├── data_loader.py        # Handles OpenF1 API requests
│   ├── data_processor.py     # Cleans and enriches OpenF1 data
│   └── visualizer.py         # Builds interactive visualizations from OpenF1 data
├── main.py                   # Streamlit app logic
├── requirements.txt          # Python dependencies
└── .env                      # Contains BASE_API_URL for OpenF1

📸 Screenshot

lap_time_chart tyre_strategy_chart pit_stop_chart

Setup & Requirements

1. Create and activate a virtual environment

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

2. Install dependencies

pip install streamlit pandas plotly python-dotenv

3. Create a .env file

BASE_API_URL=https://api.openf1.org/v1/

Launch the App

streamlit run main.py

This will open the dashboard in your default browser.


3. main.py Highlights

Features:

Dynamic year/country selection

Granular session data filtering

Lap time, tire strategy, and pit stop visualizations (8/27: Added Lap Sector Times)

Key Flow:

Fetch meeting/session info via data_loader.py

Process raw data in data_processor.py

Visualize with plot_lap_times(), plot_tire_strategy(), plot_pit_stop() from visualizer.py

Inline comments in the code guide you through OpenF1 endpoint usage:

meetings returns all races in a season

sessions returns FP1, Quali, Race for a given race (meeting_key)

laps, pit, stints, and drivers use session_key to pull telemetry data

4 🔍 File Descriptions

data_loader.py

Handles OpenF1 API calls using requests, with optional pagination logic. Each fetch function:

Specifies the OpenF1 endpoint (e.g., "laps", "drivers")

Applies query filters (like session_key or meeting_key)

Uses @st.cache_data to reduce network calls

data_processor.py

Cleans and formats raw OpenF1 data:

Filters invalid lap or pit rows

Calculates stint lap ranges from lap_start to lap_end

Builds a driver_color_map from drivers.team_colour to use in plots

visualizer.py

Creates interactive charts:

[new] plot_sector_times(): bar chart of sectors (1-3) colored by fastest(magenta), average (green), and slowest (red) with driver and lap selection. plot_lap_times(): line chart of lap_duration colored by driver

plot_tire_strategy(): horizontal bar chart from stints

plot_pit_stop(): vertical bar chart for pit_duration

All charts format hover templates and colors using OpenF1 data fields.


Extend This Project

Ideas to build on:

  • Add tire degradation trends
  • Compare qualifying vs. race pace
  • Highlight fastest laps and race events
  • Integrate sector time analytics (50%)

About

Currently a work in progress for improvements.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%