Thu, 06 Apr 2017 17:18:24 +0200
finishing first release
11 | 1 | #!/usr/bin/env python |
2 | # -*- coding: UTF-8 -*- | |
14 | 3 | """ |
4 | Web CGI script for 3D View | |
5 | """ | |
11 | 6 | |
7 | import cgi, data, json, config | |
8 | from cylindertransport import CylinderSpacerCalculator | |
18 | 9 | import base64 |
10 | import cStringIO | |
11 | 11 | |
12 | # enable debugging | |
14 | 13 | #import sys |
14 | #sys.stderr = sys.stdout | |
18 | 15 | import cgitb |
16 | cgitb.enable() | |
11 | 17 | |
18 | def do_action(args): | |
14 | 19 | """ |
20 | process some actions (like JSON requests) | |
21 | """ | |
11 | 22 | if args["action"].value == "calculate": |
23 | cylinders = args.getlist("cylinders[]") | |
24 | calc = CylinderSpacerCalculator(cylinders) | |
25 | calc.calculate() | |
18 | 26 | image = calc.render_image() |
27 | buffer = cStringIO.StringIO() | |
28 | image.save(buffer, format="PNG") | |
29 | img_str = base64.b64encode(buffer.getvalue()) | |
11 | 30 | print json.dumps({ |
31 | "objects": calc.circles, | |
19 | 32 | "offset": (calc.width - (2 * calc.margin)) / 2, |
18 | 33 | "scale3d": config.SCALE3D, |
34 | "image": img_str | |
11 | 35 | }) |
36 | else: | |
37 | print "unknown Action %s" % args["action"].value | |
38 | ||
14 | 39 | def run(): |
40 | """ | |
41 | Main program, without action displays the html content | |
42 | """ | |
43 | print "Content-Type: text/html;charset=utf-8" | |
44 | print "" | |
11 | 45 | |
14 | 46 | args = cgi.FieldStorage() |
47 | if "action" in args: | |
48 | do_action(args) | |
49 | else: | |
50 | # display the html content | |
51 | content = open("stlviewer.html", "r").read() | |
52 | cyls = "" | |
53 | for cyl in sorted(data.CYLINDER.keys()): | |
54 | cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % ( | |
55 | cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]) | |
56 | content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls) | |
11 | 57 | |
14 | 58 | print content |
59 | ||
60 | if __name__ == "__main__": | |
61 | run() |