Messy initial binding to backend API.
This commit is contained in:
@@ -5,20 +5,46 @@ Standard functions to call for fares information.
|
|||||||
# Imports
|
# Imports
|
||||||
import requests
|
import requests
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
from os import environ
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
# Init.
|
# Init.
|
||||||
|
COLUMN_RENAMES = {
|
||||||
|
"origin_code": "Origin",
|
||||||
|
"destination_code": "Destination",
|
||||||
|
"route_code": "Route",
|
||||||
|
"status_code": "Status",
|
||||||
|
"usage_code": "Usage",
|
||||||
|
"direction": "Direction",
|
||||||
|
"end_date": "Ends",
|
||||||
|
"start_date": "Starts",
|
||||||
|
"toc": "TOC",
|
||||||
|
"cross_london_flag": "Cross London",
|
||||||
|
"settlement_flag": "Settlement",
|
||||||
|
"discount_flag": "Discount",
|
||||||
|
"flow_id": "ID",
|
||||||
|
"ticket_code": "Ticket Code",
|
||||||
|
"fare": "Fare",
|
||||||
|
"restriction_code": "Restriction",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
def fares_query(origin: str, dest: str, date: str) -> pd.DataFrame:
|
def fares_query(origin: str, destination: str) -> pd.DataFrame:
|
||||||
_ = requests # .get(url=url, headers=headers)
|
response = requests.get(
|
||||||
df = pd.DataFrame(
|
url=f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}",
|
||||||
{
|
auth=(
|
||||||
"origin": [origin for _ in range(10)],
|
environ.get("BD_FARES_USER", ""),
|
||||||
"dest": [dest for _ in range(10)],
|
environ.get("BD_FARES_PASS", ""),
|
||||||
"date": [date for _ in range(10)],
|
),
|
||||||
"fare": np.random.rand(10) * 20,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
return df
|
dfs, data = [], json.loads(response.content.decode())
|
||||||
|
for flow_fares in data:
|
||||||
|
df = pd.json_normalize(flow_fares["fares"]).drop("flow_id", axis=1)
|
||||||
|
meta = pd.DataFrame.from_records(
|
||||||
|
[flow_fares["flow_id"] | {"Number": number} for number in df.index]
|
||||||
|
).rename(COLUMN_RENAMES, axis=1)
|
||||||
|
df.index = pd.MultiIndex.from_frame(meta)
|
||||||
|
dfs.append(df)
|
||||||
|
return pd.concat(dfs).rename(COLUMN_RENAMES, axis=1)
|
||||||
|
|||||||
+16
-8
@@ -19,12 +19,13 @@ HTML_CONTENT_HEADER = """
|
|||||||
<head>
|
<head>
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Merriweather+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet">
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Merriweather+Sans:ital,wght@0,300..800;1,300..800&display=swap">
|
||||||
<link rel="icon" type="image/x-icon" href="https://cdn.ballast-data.co.uk/Logo.svg">
|
<link rel="stylesheet" href="https://cdn.ballast-data.co.uk/theme.css">
|
||||||
|
<link rel="icon" type="image/x-icon" href="https://cdn.ballast-data.co.uk/Icon.svg">
|
||||||
</head>
|
</head>
|
||||||
<body style="font-family: 'Merriweather Sans'; font-size: 1.6em;">
|
<body>
|
||||||
<img height=67px src="https://cdn.ballast-data.co.uk/Logo.svg">
|
<img height=80px src="https://cdn.ballast-data.co.uk/Logo.svg">
|
||||||
<b> Fares </b> <br>
|
<b style="font-size: 100px; line-height: 1; font-weight: 400;"> Fares </b> <br>
|
||||||
<br>
|
<br>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -52,10 +53,17 @@ class FaresHandler(BaseHTTPRequestHandler):
|
|||||||
"""
|
"""
|
||||||
table = fares_query(
|
table = fares_query(
|
||||||
origin=(_d := self.parse_url_query()).get("origin", "NCL"),
|
origin=(_d := self.parse_url_query()).get("origin", "NCL"),
|
||||||
dest=_d.get("dest", "NCL"),
|
destination=_d.get("destination", "NCL"),
|
||||||
date=_d.get("date", "2026-04-06"),
|
|
||||||
).style
|
).style
|
||||||
table = table.format({"fare": "{:.2f}"})
|
table = table.set_table_styles(
|
||||||
|
[
|
||||||
|
{"selector": "tr", "props": [("vertical-align", "text-top")]},
|
||||||
|
{
|
||||||
|
"selector": "tbody td",
|
||||||
|
"props": [("border-top", "1px solid var(--dark-color)")],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
text = table.to_html()
|
text = table.to_html()
|
||||||
return HTML_CONTENT_HEADER + text + HTML_CONTENT_FOOTER
|
return HTML_CONTENT_HEADER + text + HTML_CONTENT_FOOTER
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user