cylindertransport-web.py

Thu, 06 Apr 2017 19:00:07 +0200

author
mdd
date
Thu, 06 Apr 2017 19:00:07 +0200
changeset 22
08f0118587d7
parent 21
41083328ca82
permissions
-rwxr-xr-x

ht is not PVC....

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

mercurial