This directory contains the backend components for the Fastlytics application.
main.py: A FastAPI application that serves Formula 1 data. It reads pre-processed data from thedata_cachedirectory and can also fetch live data using the FastF1 library.processor.py: A Python script that uses the FastF1 library to fetch historical F1 data (schedules, results, standings, lap times, etc.), processes it, and saves it into JSON files within thedata_cachedirectory. This pre-processing step speeds up API responses for historical data.data_cache/: Directory where theprocessor.pyscript stores the processed JSON data files, organized by year.cache/: Directory used by the FastF1 library to cache raw API responses, reducing redundant external requests..env: Environment variables file. Should containFASTLYTICS_API_KEYfor securing the API endpoints and optionallyFRONTEND_URLfor CORS configuration.requirements.txt: (Optional - If you create one) Lists Python dependencies.
-
Dependencies: Ensure you have Python installed. It's recommended to use a virtual environment. Install dependencies (primarily
fastapi,uvicorn,fastf1,pandas,numpy,python-dotenv):# Install core dependencies + FastF1 with plotting extras pip install fastapi uvicorn "fastf1[full]" pandas numpy python-dotenv requests requests_cache # Optional: Install Gunicorn for production deployment pip install gunicorn # Or install from requirements.txt if provided # pip install -r requirements.txt
-
Environment Variables: Create a
.envfile in this directory and add your desiredFASTLYTICS_API_KEY.FASTLYTICS_API_KEY=your_secret_api_key_here FRONTEND_URL=http://localhost:5173 # Or your frontend's URL -
Run Data Processor: Execute the processor script to fetch and cache historical data. This can take some time initially.
python processor.py
-
Run API Server: Start the FastAPI server using Uvicorn.
# For development (with auto-reload) uvicorn main:app --host 0.0.0.0 --port 8000 --reloadThe API will then be accessible, typically at
http://localhost:8000.Alternative (Production with Gunicorn): For production deployments, using Gunicorn with Uvicorn workers is often recommended for better process management.
# Example: Run with 4 worker processes gunicorn -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8000 -w 4-k uvicorn.workers.UvicornWorker: Specifies the Uvicorn worker class for ASGI compatibility.--bind 0.0.0.0:8000: Sets the address and port to listen on.-w 4: (Optional) Specifies the number of worker processes (adjust based on your server resources).