aboutsummaryrefslogtreecommitdiff
path: root/html_view.py
blob: 2e820666f134fdaae8393bcb42c1ebd5f3509862 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import util
import ptv_client

def format_stop_and_route_name_li(stop_and_route):
    stop, route = stop_and_route
    return '<li>Stop: {}, Route: {} {} {}</li>'.format(
        stop['stop_name'], ptv_client.get_route_type(route['route_type']),
        route['route_number'], route['route_name'])

def format_stop_and_route_name_ol(stop_and_routes):
    return '<ol>{}</ol>'.format(''.join(
        map(format_stop_and_route_name_li, stop_and_routes)))

def format_stop_and_route_name_tr(stop_and_route):
    stop, route = stop_and_route
    return """
<tr>
  <td>
    <a href="/?route-type={}&stop={}&route={}">{}</a>
  </td>
  <td>{}</td><td>{}</td><td>{}</td>
</tr>
""".format(
    route['route_type'], stop['stop_id'], route['route_id'],
    stop['stop_name'], ptv_client.get_route_type(route['route_type']),
    route['route_number'], route['route_name'])

def format_stop_and_route_name_table(stop_and_routes):
    return """
<table>
  <tr>
    <th>Stop</th>
    <th>Route type</th>
    <th>Route number</th>
    <th>Route name</th>
  </tr>
    {}
</table>
""".format(''.join(map(format_stop_and_route_name_tr, stop_and_routes)))

def format_departure_tr(departure, direction_names):
    return """
<tr>
  <td>{}</td><td>{}</td><td>{}</td>
</tr>
""".format(
    util.format_time(util.parse_time(departure['estimated_departure_utc'])),
    util.format_time(util.parse_time(departure['scheduled_departure_utc'])),
    direction_names[departure['direction_id']])

def format_departure_table(departures, direction_names):
    return """
<table>
  <tr>
    <th>Estimated</th>
    <th>Scheduled</th>
    <th>Direction</th>
  </tr>
    {}
</table>
""".format(''.join(format_departure_tr(dep, direction_names)
                   for dep in departures))

def style():
    return """
<style>
  tr:nth-child(even) {background-color: #f2f2f2;}
  tr:hover {background-color: coral;}
</style>
"""

def html(body):
    return """
<!DOCTYPE html>
<html>
<head>
  %(style)s
</head>
<body>
  %(body)s
</body>
</html>
""" % {'style': style(), 'body': body}