-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (40 loc) · 1.58 KB
/
main.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
import os
import jinja2
import webapp2
template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir), autoescape=False)
class BaseHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
return self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
return self.write(self.render_str(template, **kw))
def render_template(self, view_filename, params=None):
if params is None:
params = {}
template = jinja_env.get_template(view_filename)
return self.response.out.write(template.render(params))
# handlers
class MainHandler(BaseHandler):
def get(self):
message = "This is my message"
return self.render_template("hello.html", params={"message": message})
# URLs
app = webapp2.WSGIApplication([
webapp2.Route('/', MainHandler),
], debug=True)
# run on server
localhost = True # True: non-GAE localhost server; False: GAE on either localhost or on Google Cloud
if localhost:
def main():
from paste import httpserver
from paste.cascade import Cascade
from paste.urlparser import StaticURLParser
assets_dir = os.path.join(os.path.dirname(__file__))
static_app = StaticURLParser(directory=assets_dir)
web_app = Cascade([app, static_app])
httpserver.serve(web_app, host='localhost', port='8080')
if __name__ == '__main__':
main()