-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
29 lines (26 loc) · 913 Bytes
/
index.py
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
#!/bin/python3
'''Serve radio station links using CherryPy with Jinja2 template.'''
from json import load
import os
import cherrypy
from jinja2 import Environment, FileSystemLoader
# this module’s directory path
WORKING_DIR = os.path.dirname(__file__)
# initialize cherrypy environment
e = Environment(loader=FileSystemLoader(f'{WORKING_DIR}/templates'))
# render index template
class Index():
'''Index page class.'''
@cherrypy.expose()
def index(self):
'''Render index page template.'''
template = e.get_template('index.html')
return template.render(station_data=self.station_data())
# radio station data
def station_data(self):
'''Get radio station data.'''
with open(f'{WORKING_DIR}/json/stations.json') as json_file:
return load(json_file)
# if web app runs as standalone
if __name__ == '__main__':
cherrypy.quickstart(Index())