Thu, 06 Apr 2017 19:00:07 +0200
ht is not PVC....
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) | |
21 | 25 | # Bug... Calculate gets called by render_image, producing double circles list |
26 | #calc.calculate() | |
18 | 27 | image = calc.render_image() |
28 | buffer = cStringIO.StringIO() | |
29 | image.save(buffer, format="PNG") | |
30 | img_str = base64.b64encode(buffer.getvalue()) | |
11 | 31 | print json.dumps({ |
32 | "objects": calc.circles, | |
19 | 33 | "offset": (calc.width - (2 * calc.margin)) / 2, |
18 | 34 | "scale3d": config.SCALE3D, |
35 | "image": img_str | |
11 | 36 | }) |
37 | else: | |
38 | print "unknown Action %s" % args["action"].value | |
39 | ||
14 | 40 | def run(): |
41 | """ | |
42 | Main program, without action displays the html content | |
43 | """ | |
44 | print "Content-Type: text/html;charset=utf-8" | |
45 | print "" | |
11 | 46 | |
14 | 47 | args = cgi.FieldStorage() |
48 | if "action" in args: | |
49 | do_action(args) | |
50 | else: | |
51 | # display the html content | |
52 | content = open("stlviewer.html", "r").read() | |
53 | cyls = "" | |
54 | for cyl in sorted(data.CYLINDER.keys()): | |
55 | cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % ( | |
56 | cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]) | |
57 | content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls) | |
11 | 58 | |
14 | 59 | print content |
60 | ||
61 | if __name__ == "__main__": | |
62 | run() |