-
Notifications
You must be signed in to change notification settings - Fork 0
/
web4.py
62 lines (50 loc) · 1.61 KB
/
web4.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
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 jinja2 import Environment, FileSystemLoader
from webob import Request, Response
items = [
'app.js',
'react.js',
'leaflet.js',
'D3.js',
'moment.js',
'math.js',
'main.css',
'bootstrap.css',
'normalize.css',
]
js = []
css = []
for item in items:
dividedOne, dividedTwo = item.split('.')
if dividedTwo == 'js':
js.append(item)
elif dividedTwo == 'css':
css.append(item)
class WsgiTopBottomMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
response = self.app(environ, start_response).decode()
if response.find('<head>' and '<body>') > -1:
beforehead, head = response.split('<head>')
datahead, afterhead = head.split('</head>')
beforebody, body = afterhead.split('<body>')
databody, afterbody = body.split('</body>')
data = '<head>' + datahead + '</head>' + '<body>' + databody +'</body>'
yield (beforehead + data + afterbody).encode()
else:
yield (response).encode()
def app(environ, start_response):
response_code = '200 OK'
response_type = ('Content-Type', 'text/HTML')
start_response(response_code, [response_type])
return ''''''
app = WsgiTopBottomMiddleware(app)
request = Request.blank('/index.html')
# request = Request.blank('/about/aboutme.html')
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('index.html')
print(template.render(scripts=js, styles=css))
# env = Environment(loader=FileSystemLoader('.'))
# template = env.get_template('about/aboutme.html')
# print(template.render(scripts=js, styles=css))
print(request.get_response(app))