/
server.py
143 lines (120 loc) · 5.13 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
"""
Module to tie the quantities, calculator, and form modules together into an online calculator.
The calculator is hosted on ktheis.pythonanywhere.com, but you could have a local server or run it
on other platforms. In the calculator, you define values of named physical quantities,
and then do arithmetic with them. The web server is implemented using web.py.
"""
# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
# +++++++++++ WEB.PY +++++++++++
# Here is a simple web.py app. It is currently serving the default welcome
# page. You can adapt this if you want
# If you're using a different web framework, you'll need to comment all this
# out
import web
urls = (
'/', 'index',
'/custom', 'preload',
'/homework/(.*)', 'homework',
'/(js|css|png|ico)/(.*)', 'static',
'/example(.*)', 'example'
)
import sys
sys.path.append("/var/www")
from calculator import calc, State, markup_comments
from form import newform, printableLog, helpform
from ChemEq import talk_to_student
class index:
"""
Defines the home page of the PQCalc server.
"""
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
known = [" -- nothing yet -- "]
browser = web.ctx.env['HTTP_USER_AGENT'].lower()
mobile = ("ipad" in browser or "iphone" in browser or "ipod" in browser)
if "ipod" in browser or "iphone" in browser or "android" in browser:
mobile = "ipod"
return newform("", "", "", known, "", mobile, False, "")
def POST(self):
"""
Load quantities defined previously by the user, process current commands and output results.
"""
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
browser = web.ctx.env['HTTP_USER_AGENT'].lower()
mobile = ("ipad" in browser or "iphone" in browser or "nexus" in browser)
if "ipod" in browser or "iphone" in browser or "android" in browser:
mobile = "ipod"
try:
state = web.input()
if state['sub'] == "reset":
return newform("", "", "", "", "", mobile, False, "")
except:
return newform("", "", "", "", "", mobile, False, "")
oldsymbols = state['memory']
logbook = state['logbook']
inputlog = state['inputlog']
if state['sub'] == "export":
allsymbols = State(oldsymbols, mobile)
return printableLog(allsymbols, logbook, inputlog)
if state['sub'] == "help":
return helpform(mobile)
commands = state['commands']
outp, logp, mem, known, oneline, good_input,linespace = calc(oldsymbols, commands, mobile)
inputlog = inputlog + "\n" + good_input
return newform(outp, logp, mem, known, logbook, mobile, oneline, inputlog, linespace=linespace)
from quantities import Units as Units
class static:
def GET(self, media, file):
print (media, file)
try:
f = open('static/' + file, 'r')
return f.read()
except:
return 'bull %s %s' % (media, file) # you can send an 404 error here if you want
class example:
"""
Shows the PQCalc form pre-filled with example calculation commands.
"""
def GET(self, examplenr):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
browser = web.ctx.env['HTTP_USER_AGENT'].lower()
known = ["nothing yet"]
prefill = examplenr
mobile = ""
mobile = ("ipad" in browser or "iphone" in browser or "nexus" in browser)
if "ipod" in browser or "iphone" in browser:
mobile = "ipod"
return newform("", "", "", known, "", mobile, False, "", prefill=prefill)
class preload:
"""
Defines the home page of the PQCalc server with switches loaded.
"""
def GET(self):
web.header('Content-Type', 'text/html; charset=utf-8', unique=True)
browser = web.ctx.env['HTTP_USER_AGENT'].lower()
mobile = ("ipad" in browser or "iphone" in browser or "nexus" in browser)
if "ipod" in browser or "iphone" in browser or "android" in browser:
mobile = "ipod"
try:
state = web.input(switch="")
if state['switch'] == "":
return newform("", "", "", "", "", mobile, False)
except:
return newform("", "", "", "", "", mobile, False)
oldsymbols = ""
logbook = ""
commands = "\n".join(["__%s__ = 1" % switch for switch in state['switch'].split("*")])
outp, logp, mem, known, oneline, good_input,linespace = calc(oldsymbols, commands, mobile)
return newform(outp, logp, mem, known, logbook, mobile, oneline, "", linespace=linespace)
# comment out these two lines if you want to use another framework
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()