-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·131 lines (97 loc) · 3.42 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# this is a flask python app
# the interactive graph is in the mainview.js which calls server side functions for calculations via AJAX
# this could be done on the client side, but the server side could introduce complex calculations
import os
import json
import random
from flask import Flask, render_template
from flask.ext.bootstrap import Bootstrap
from flask import request
# my simple statistics custom code
from linear_regression import *
from simplestat import *
#-- define app configs
app = Flask(__name__)
Bootstrap(app)
app.config['BOOTSTRAP_USE_CDN'] = True
#3 items
#rounte, caption internal_name
nav_bar = [
('/main/', 'main', 'graph'),
('/play/', 'play', 'play'),
('/about/', 'about', 'about')]
start_with_teaser = False
#--- main views
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
"""catch all paths here"""
if path == "":
if start_with_teaser:
return render_template('start.html')
else:
return create_view('main')
path_names = [x[0] for x in nav_bar]
nav_dict = {path: (name,caption) for (path, name, caption) in nav_bar}
#route to the main view
path = '/' + path
if path in nav_dict.keys():
name = nav_dict[path][0]
return create_view(name)
else:
return 'path: %s not found paths' % (path,nav_names)
def create_view(viewname):
"""create standard view"""
return render_template(viewname + '.html',navigation_bar=nav_bar,active_page=viewname)
#--- expose some statistic functions via AJAX
#this is the hard part, pushing data through from d3 to server side
#easy things could be done on the client instead (science.js is used for gaussian data)
@app.route('/data/')
def get_data():
""" get data from server """
return json.dumps(random_data(20))
@app.route('/sampledata/')
def get_sample_data():
""" get data from server """
rd = {'values':sample_data_xy()}
return json.dumps(rd)
@app.route('/dist/',methods=['POST'])
def dist():
""" get the distribution of values based on randomness faktor and number of points """
if request.method == 'POST':
vals = json.loads(request.form['vals'])
rd = {'values': random_data(vals["beta"],vals["size"])}
s = json.dumps(rd)
return s
@app.route('/distskew/',methods=['POST'])
def skewdist():
""" a skewed distribution """
if request.method == 'POST':
vals = json.loads(request.form['vals'])
rd = {'values': random_data_skew(vals["beta"])}
s = json.dumps(rd)
return s
@app.route('/stat/<statname>/',methods=['POST'])
def show_stat_name(statname):
""" general statistic function """
if request.method == 'POST':
# values come as JSON dicts
vals = json.loads(request.form['vals'])
xvals = [z["x"] for z in vals]
yvals = [z["y"] for z in vals]
s = ""
if statname=='regression':
s = json.dumps(get_linear_reg(xvals,yvals))
"""
#not used currently
elif statname=='median':
s = json.dumps(get_median(xvals))
elif statname=='average':
s = json.dumps(get_avg(xvals))
"""
return s
if __name__ == '__main__':
#run flask app with simple HTTP server. add your own server
app.run(debug=False, port=8080, host='0.0.0.0')