Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file added app/__pycache__/__init__.cpython-314.pyc
Binary file not shown.
Binary file added app/__pycache__/data_loader.cpython-313.pyc
Binary file not shown.
Binary file added app/__pycache__/data_loader.cpython-314.pyc
Binary file not shown.
Binary file added app/__pycache__/data_processor.cpython-313.pyc
Binary file not shown.
Binary file added app/__pycache__/data_processor.cpython-314.pyc
Binary file not shown.
Binary file added app/__pycache__/visualizer.cpython-313.pyc
Binary file not shown.
Binary file added app/__pycache__/visualizer.cpython-314.pyc
Binary file not shown.
20 changes: 14 additions & 6 deletions app/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from dotenv import load_dotenv
import os

load_dotenv()
load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), "..", ".env"))

BASE_URL = os.getenv("BASE_API_URL")
BASE_API_URL = os.getenv("BASE_API_URL")
print(BASE_API_URL)


def fetch_data(endpoint, params=None):
Expand All @@ -29,11 +30,18 @@ def fetch_data(endpoint, params=None):
if params is None:
params = {}

url = f"{BASE_URL}{endpoint}"
url = f"{BASE_API_URL}{endpoint}"
full_url = requests.Request('GET', url, params=params).prepare().url
response = requests.get(full_url)
response.raise_for_status()
return pd.DataFrame(response.json())
try:
response = requests.get(full_url)

if response.status_code == 404:
return pd.DataFrame()
response.raise_for_status()
return pd.DataFrame(response.json())
except requests.exceptions.RequestException as e:
print("API Error:", e)
return pd.DataFrame()


# Cached API calls using Streamlit's cache_data decorator
Expand Down
28 changes: 25 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import streamlit as st
import datetime
from app.data_loader import (
fetch_data,
fetch_sessions,
Expand Down Expand Up @@ -28,15 +29,19 @@

with col1:
# Step 1: Select Year and Country dynamically
available_years = [2023, 2024, 2025]
available_years = [2023, 2024, 2025, 2026]
selected_year = st.selectbox("Select Year", available_years, index=len(available_years) - 1)

# Fetch all meetings for selected year
all_meetings = fetch_data("meetings", {"year": selected_year})
current_year = datetime.datetime.now().year

if all_meetings.empty:
st.error("No meetings found for this year.")
st.stop()
if selected_year >= current_year:
st.warning("No race data yet. Showing upcoming schedule if available.")
else:
st.error("No meetings found for this year.")
st.stop()

available_countries = sorted(all_meetings["country_name"].dropna().unique())
selected_country = st.selectbox("Select Country", available_countries)
Expand All @@ -46,12 +51,21 @@
filtered_meetings["label"] = filtered_meetings["meeting_name"] + " - " + filtered_meetings["location"]
filtered_meetings = filtered_meetings.sort_values(by="meeting_key", ascending=False)

if filtered_meetings.empty:
st.warning("🚧 No Grand Prix available yet for this selection.")
st.stop()

with col2:
selected_meeting = st.selectbox("Select Grand Prix", filtered_meetings["label"], disabled=True)
selected_meeting_key = filtered_meetings.loc[
filtered_meetings["label"] == selected_meeting, "meeting_key"
].values[0]
sessions = fetch_sessions(selected_meeting_key)

if sessions.empty:
st.info("📅 Session schedule not available yet.")
st.stop()

selected_session = st.selectbox("Select Session", sessions["label"])
sessions["session_type"] = sessions["label"].str.extract(r"^(.*?)\s\(")
selected_session_type = sessions.loc[sessions["label"] == selected_session, "session_type"].values[0]
Expand All @@ -72,8 +86,16 @@
with st.expander(f"📈 Lap Time Chart for {selected_session_type} at {selected_country} {selected_year}",
expanded=True):
lap_df = fetch_laps(selected_session_key)

if lap_df.empty:
st.info("🚧 This session hasn't happened yet. Showing schedule only.")
st.stop()

processed_df = process_lap_data(lap_df)

if processed_df.empty:
st.info("🚧 This session hasn’t happened yet. Data will appear after completion.")

# Merge name_acronym into the lap data
processed_df["driver_number"] = processed_df["driver_number"].astype(str)
processed_df = processed_df.merge(driver_info, on="driver_number", how="left")
Expand Down