cylindertransport-web.py

Thu, 06 Apr 2017 17:18:24 +0200

author
mdd
date
Thu, 06 Apr 2017 17:18:24 +0200
changeset 19
32de35694e56
parent 18
ff1941c85fd3
child 21
41083328ca82
permissions
-rwxr-xr-x

finishing first release

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)
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
25 calc.calculate()
18
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
26 image = calc.render_image()
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
27 buffer = cStringIO.StringIO()
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
28 image.save(buffer, format="PNG")
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
29 img_str = base64.b64encode(buffer.getvalue())
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
30 print json.dumps({
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
31 "objects": calc.circles,
19
32de35694e56 finishing first release
mdd
parents: 18
diff changeset
32 "offset": (calc.width - (2 * calc.margin)) / 2,
18
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
33 "scale3d": config.SCALE3D,
ff1941c85fd3 added 2d image to 3d viewer
mdd
parents: 14
diff changeset
34 "image": img_str
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
35 })
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
36 else:
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
37 print "unknown Action %s" % args["action"].value
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
38
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
39 def run():
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
40 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
41 Main program, without action displays the html content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
42 """
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
43 print "Content-Type: text/html;charset=utf-8"
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
44 print ""
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
45
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
46 args = cgi.FieldStorage()
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
47 if "action" in args:
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
48 do_action(args)
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
49 else:
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
50 # display the html content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
51 content = open("stlviewer.html", "r").read()
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
52 cyls = ""
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
53 for cyl in sorted(data.CYLINDER.keys()):
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
54 cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % (
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
55 cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4])
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
56 content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls)
11
098335a1d510 web viewer 3d finished
mdd
parents:
diff changeset
57
14
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
58 print content
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
59
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
60 if __name__ == "__main__":
ba3d8c56e6f5 code cleanup
mdd
parents: 11
diff changeset
61 run()

mercurial