-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (46 loc) · 1.58 KB
/
main.py
File metadata and controls
62 lines (46 loc) · 1.58 KB
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
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from yaml import dump, safe_load
from jinja2 import Environment, FileSystemLoader, exceptions
app = FastAPI()
try:
with open("app.yaml", "r") as stream:
_data = safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
def get_template(node_name: str):
try:
template = env.get_template('app/' + node_name + '.html')
except exceptions.TemplateNotFound as exc:
template = env.get_template('app/' + 'default.html')
finally:
return template
def get_card_data(node_name: str):
data = None
if 'cards' in _data:
if node_name in _data['cards']:
data = _data['cards'][node_name]
elif node_name in _data:
data = _data[node_name]
return data
@app.get("/", response_class=HTMLResponse)
def read_path_main():
template = env.get_template('admin/' + 'main.html')
return template.render(data=_data)
@app.get("/admin", response_class=HTMLResponse)
def read_admin():
template = env.get_template('admin/' + 'main.html')
return template.render(data=_data)
@app.get("/{node_name}", response_class=HTMLResponse)
def read_path(node_name: str):
data = get_card_data(node_name)
if data is None:
template = env.get_template('app/' + 'default.html')
return template.render()
common = None
if 'common' in _data:
common = _data['common']
template = get_template(node_name)
return template.render(data, common=common)