forked from dfm/flask-d3-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path__init__.py
89 lines (67 loc) · 2.09 KB
/
__init__.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
"""
This file is part of the flask+d3 Hello World project.
"""
import json
import flask
from flask import request
import numpy as np
app = flask.Flask(__name__)
@app.route("/hello")
def index():
"""
When you request the root path, you'll get the index.html template.
"""
return flask.render_template("index.html")
@app.route("/")
def gindex():
"""
When you request the gaus path, you'll get the gaus.html template.
"""
mux = request.args.get('mux', '')
muy = request.args.get('muy', '')
if len(mux)==0: mux="3."
if len(muy)==0: muy="3."
return flask.render_template("gaus.html",mux=mux,muy=muy)
@app.route("/data")
@app.route("/data/<int:ndata>")
def data(ndata=100):
"""
On request, this returns a list of ``ndata`` randomly made data points.
:param ndata: (optional)
The number of data points to return.
:returns data:
A JSON string of ``ndata`` data points.
"""
x = 10 * np.random.rand(ndata) - 5
y = 0.5 * x + 0.5 * np.random.randn(ndata)
A = 10. ** np.random.rand(ndata)
c = np.random.rand(ndata)
return json.dumps([{"_id": i, "x": x[i], "y": y[i], "area": A[i],
"color": c[i]}
for i in range(ndata)])
@app.route("/gdata")
@app.route("/gdata/<float:mux>/<float:muy>")
def gdata(ndata=100,mux=.5,muy=0.5):
"""
On request, this returns a list of ``ndata`` randomly made data points.
about the mean mux,muy
:param ndata: (optional)
The number of data points to return.
:returns data:
A JSON string of ``ndata`` data points.
"""
x = np.random.normal(mux,.5,ndata)
y = np.random.normal(muy,.5,ndata)
A = 10. ** np.random.rand(ndata)
c = np.random.rand(ndata)
return json.dumps([{"_id": i, "x": x[i], "y": y[i], "area": A[i],
"color": c[i]}
for i in range(ndata)])
if __name__ == "__main__":
import os
port = 8000
# Open a web browser pointing at the app.
os.system("open http://localhost:{0}/".format(port))
# Set up the development server on port 8000.
app.debug = True
app.run(port=port)