cylindertransport-web.py

changeset 14
ba3d8c56e6f5
parent 11
098335a1d510
child 18
ff1941c85fd3
equal deleted inserted replaced
13:39fb313ba27c 14:ba3d8c56e6f5
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*- 2 # -*- coding: UTF-8 -*-
3 """
4 Web CGI script for 3D View
5 """
3 6
4 import cgi, data, json, config 7 import cgi, data, json, config
5 from cylindertransport import CylinderSpacerCalculator 8 from cylindertransport import CylinderSpacerCalculator
6 9
7 # enable debugging 10 # enable debugging
8 import cgitb, sys 11 #import sys
9 sys.stderr = sys.stdout 12 #sys.stderr = sys.stdout
13 #import cgitb
10 #cgitb.enable() 14 #cgitb.enable()
11 15
12
13 def do_action(args): 16 def do_action(args):
17 """
18 process some actions (like JSON requests)
19 """
14 if args["action"].value == "calculate": 20 if args["action"].value == "calculate":
15 cylinders = args.getlist("cylinders[]") 21 cylinders = args.getlist("cylinders[]")
16 calc = CylinderSpacerCalculator(cylinders) 22 calc = CylinderSpacerCalculator(cylinders)
17 calc.calculate() 23 calc.calculate()
18 print json.dumps({ 24 print json.dumps({
20 "scale3d": config.SCALE3D 26 "scale3d": config.SCALE3D
21 }) 27 })
22 else: 28 else:
23 print "unknown Action %s" % args["action"].value 29 print "unknown Action %s" % args["action"].value
24 30
31 def run():
32 """
33 Main program, without action displays the html content
34 """
35 print "Content-Type: text/html;charset=utf-8"
36 print ""
25 37
38 args = cgi.FieldStorage()
39 if "action" in args:
40 do_action(args)
41 else:
42 # display the html content
43 content = open("stlviewer.html", "r").read()
44 cyls = ""
45 for cyl in sorted(data.CYLINDER.keys()):
46 cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
47 cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4])
48 content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
26 49
50 print content
27 51
28 52 if __name__ == "__main__":
29 53 run()
30 print("Content-Type: text/html;charset=utf-8")
31 print("")
32
33 args = cgi.FieldStorage()
34 if "action" in args:
35 do_action(args)
36 else:
37 # display the html content
38 content = open("stlviewer.html", "r").read()
39 cyls = ""
40 for cyl in sorted(data.CYLINDER.keys()):
41 cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
42 cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]);
43 content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
44
45 print content

mercurial