-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
35 lines (34 loc) · 1.14 KB
/
graph.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
from cgi import parse_qs
from template import html
import matplotlib.pyplot as plt
def application(environ, start_response):
if environ['PATH_INFO'] == '/img/graph.png':
try:
with open('./img/graph.png', 'rb') as f:
response_body = f.read()
except:
response_body = ''
start_response('200 OK', [
('Content-Type', 'image/png'),
('Content-Length', str(len(response_body)))
])
return [response_body]
else:
d = parse_qs(environ['QUERY_STRING'])
a = d.get('a', [''])[0]
b = d.get('b', [''])[0]
c = d.get('c', [''])[0]
if '' not in [a, b, c]:
a, b, c = int(a), int(b), int(c)
x = [n / 10.0 for n in range(-40, 41)]
y = [a * n ** 2 + b * n + c for n in x]
fig = plt.figure()
graph = plt.plot(x, y)
plt.grid()
fig.savefig('graph.png')
response_body = html
start_response('200 OK', [
('Content-Type', 'text/html'),
('Content-Length', str(len(response_body)))
])
return [response_body]