-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-app.py
78 lines (67 loc) · 2.01 KB
/
web-app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import tornado.ioloop
import tornado.web
import sys
import os
from pythonql.RunPYQL import runProgramFromString
from pythonql.RunPYQL import runProgramFromFile
import io
import traceback
settings = {
"static_path" : os.path.join(os.path.dirname(__file__),"pages"),
"debug" : True
}
class BaseHandler(tornado.web.RequestHandler):
None
class IndexHandler(BaseHandler):
def get(self):
self.render("index.html")
class MainHandler(BaseHandler):
def get(self):
self.render("main.html")
class ShowQuery(BaseHandler):
def get(self):
self.render("query.html", query=self.get_argument("q"))
class RunQuery(BaseHandler):
def get(self):
query_f = self.get_argument("q")
out_buf = io.StringIO()
old_stdout = sys.stdout
sys.stdout = out_buf
try:
(comp_time,exec_time) = runProgramFromFile(query_f)
print("-------Compile time: %.3f, Execution time: %.3f" %
(comp_time,exec_time) )
except:
old_stderr = sys.stderr
sys.stderr = out_buf
traceback.print_exc()
sys.stderr = old_stderr
sys.stdout = old_stdout
self.render("result.html",output=out_buf.getvalue())
def post(self):
query = self.get_argument("query_string")
query += "\n"
out_buf = io.StringIO()
old_stdout = sys.stdout
sys.stdout = out_buf
try:
(comp_time,exec_time) = runProgramFromString(query)
print("-------Compile time: %.3f, Execution time: %.3f" %
(comp_time,exec_time) )
except:
old_stderr = sys.stderr
sys.stderr = out_buf
traceback.print_exc()
sys.stderr = old_stderr
sys.stdout = old_stdout
self.render("result.html",output=out_buf.getvalue())
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/main.html", MainHandler),
(r"/query.html", ShowQuery),
(r"/run_query", RunQuery),
(r"/pages/(.*)", tornado.web.StaticFileHandler,{"path":settings['static_path']})
], **settings)
if __name__ == "__main__":
application.listen(8080)
tornado.ioloop.IOLoop.instance().start()