Skip to content

Commit 3eaf7ae

Browse files
Refactor Namma Metro main application to fetch ridership data from API
- Replaced local JSONL file reading with an API call to retrieve ridership data. - Added a refresh button to update the displayed data dynamically. - Updated the FastAPI title and welcome message for consistency across the application.
1 parent f56ee4e commit 3eaf7ae

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

apps/namma_metro/main.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1+
import os
2+
import requests
13
import streamlit as st
2-
import json
34
import pandas as pd
4-
from pathlib import Path
5-
6-
from traffic_data_bengaluru.utils import *
7-
8-
data_directory = get_data_directory()
9-
ridership_filepath = data_directory / "namma_metro" / "raw" / "ridership.jsonl"
5+
from dotenv import load_dotenv
6+
load_dotenv()
107

118
st.title("🚇 Namma Metro")
129
st.set_page_config(page_title="Namma Metro Ridership", layout="wide")
1310

14-
records = []
15-
with open(ridership_filepath, "r") as f:
16-
for line in f:
17-
item = json.loads(line)
18-
results = item.get("results", [])
19-
if results:
20-
records.append(results[0])
11+
base_url = os.environ.get("TDB_API_BASE_URL")
12+
url = f"{base_url}/metro/ridership"
13+
14+
# Function to fetch data
15+
def fetch_data():
16+
response = requests.get(url)
17+
response.raise_for_status()
18+
return pd.DataFrame(response.json())
19+
20+
# Initialize session state
21+
if "df" not in st.session_state:
22+
st.session_state.df = fetch_data()
23+
24+
# Refresh button
25+
if st.button("Refresh"):
26+
st.session_state.df = fetch_data()
2127

22-
if not records:
23-
st.warning("No ridership data found.")
24-
else:
25-
df = pd.DataFrame(records).sort_values(by='RidershipDate', ascending=False)
26-
st.dataframe(df)
28+
# Display the table
29+
st.dataframe(st.session_state.df)

nbs/apis/01_app.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
"source": [
4040
"#| export\n",
4141
"\n",
42-
"app = FastAPI(title=\"Namma Traffic API\")\n",
42+
"app = FastAPI(title=\"Traffic Data Bengaluru API\")\n",
4343
"app.include_router(metro_router)\n",
4444
"\n",
4545
"@app.get(\"/\")\n",
4646
"def root():\n",
47-
" return {\"message\": \"Welcome to Namma Traffic API\"}"
47+
" return {\"message\": \"Welcome to Traffic Data Bengaluru API\"}"
4848
]
4949
},
5050
{

nbs/namma_metro/01_ridership.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,13 @@
378378
],
379379
"metadata": {
380380
"kernelspec": {
381-
"display_name": "python3",
381+
"display_name": ".venv",
382382
"language": "python",
383383
"name": "python3"
384+
},
385+
"language_info": {
386+
"name": "python",
387+
"version": "3.12.3"
384388
}
385389
},
386390
"nbformat": 4,

traffic_data_bengaluru/apis/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from .namma_metro import router as metro_router
99

1010
# %% ../../nbs/apis/01_app.ipynb 3
11-
app = FastAPI(title="Namma Traffic API")
11+
app = FastAPI(title="Traffic Data Bengaluru API")
1212
app.include_router(metro_router)
1313

1414
@app.get("/")
1515
def root():
16-
return {"message": "Welcome to Namma Traffic API"}
16+
return {"message": "Welcome to Traffic Data Bengaluru API"}

0 commit comments

Comments
 (0)