-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflask_server.py
144 lines (124 loc) · 4.65 KB
/
flask_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
144
"""
Cross-origin MDPVis Server
===================
This is a minimal server for integrating MDPVis with a domain.
The domain_bridge.py is expected to be defined in the parent
directory.
:copyright: (C) 2015 by Sean McGregor.
:license: MIT, see LICENSE for more details.
"""
from flask import Flask, jsonify, request, redirect
import sys
print """
Starting Flask Server...
Note, you may be able to specify a domain at this point by adding it as a
positional argument.
"""
# Specify which domain should be selected from the domain bridge.
# This is an optional argument that is used for domain_bridges that sit
# atop a collection of MDP domains. You should leave this as the
# empty string unless you must select between domains.
domain = ""
if len(sys.argv) > 1:
domain = sys.argv[1]
# Add the parent folder path to the sys.path list so
# we can include its bridge
sys.path.insert(0, '..')
import domain_bridge
try:
# The typical way to import flask-cors
from flask.ext.cors import cross_origin
except ImportError:
# Path hack allows examples to be run without installation.
import os
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0, parentdir)
from flask.ext.cors import cross_origin
# Static Resources directory assumes the visualization is served from
# a directory called 'FireVis'. todo: rename repository to MDPVis
app = Flask('MDPVis', static_folder='.', static_url_path='')
@app.route("/MDPvis/", methods=['GET'])
@cross_origin()
def MDPvis():
return redirect("/index2.html", code=302)
@app.route("/dashboard/pages/", methods=['GET'])
@cross_origin()
def MDPvisDashboard():
return redirect("/dashboard/pages/index.html", code=302)
@app.route("/", methods=['GET'])
@cross_origin()
def site_root():
'''
This view has CORS enabled for all domains, representing the simplest
configuration of view-based decoration. You can test this endpoint
with:
$ curl --include -X GET http://127.0.0.1:5000/ \
--header Origin:www.examplesite.com
Which should return something like:
>> HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 186
Access-Control-Allow-Origin: www.examplesite.com
Server: Werkzeug/0.10.4 Python/2.7.9
Date: Tue, 19 May 2015 17:13:39 GMT
THE DOCUMENT FOUND BELOW
'''
return '''
<h1>Hello MDPVis!</h1>
<p style='font-size: 150%;'>Your server is running and ready for
integration with your MDP domain and optimizer.</p>
<p style='font-size: 150%;'>To test the other endpoints, visit
<a href="/initialize">/initialize</a>,
<a href="/trajectories">/trajectories</a>,
<a href="/optimize">/optimize</a>, or
<a href="/state">/state</a>
'''
@app.route("/initialize", methods=['GET'])
@cross_origin(allow_headers=['Content-Type'])
def cross_origin_initialize():
'''
Asks the domain for the parameters to seed the visualization.
'''
return jsonify(domain_bridge.initialize())
@app.route("/trajectories", methods=['GET'])
@cross_origin(allow_headers=['Content-Type'])
def cross_origin_trajectories():
'''
Asks the domain for the trajectories generated by the
requested parameters.
'''
q = parse_query(request.args)
trajectories = domain_bridge.trajectories(q)
return jsonify({"trajectories": trajectories})
@app.route("/optimize", methods=['GET'])
@cross_origin(allow_headers=['Content-Type'])
def cross_origin_optimize():
'''
Asks the domain to optimize for the current parameters, then return the
new set of parameters for the optimized policy.
'''
q = parse_query(request.args)
return jsonify(domain_bridge.optimize(q))
@app.route("/state", methods=['GET'])
@cross_origin(allow_headers=['Content-Type'])
def cross_origin_state():
'''
Ask the domain for the referenced state and state snapshots.
'''
q = parse_query(request.args)
return jsonify(domain_bridge.state(q))
def parse_query(queryObject):
"""
Get all the reward, transition, policy, and events into their dictionaries.
"""
queryDict = {"reward":{}, "transition":{}, "policy":{}, "Event Number": -1, "Pathway Number": -1}
for key in queryObject:
cur = key.replace("]","[").split("[") # Quick and dirty hack
if len(cur) > 1:
queryDict[cur[0]][cur[1]] = float(queryObject[key])
else:
queryDict[cur[0]] = float(queryObject[key][0])
return queryDict
# Binds the server to port 8938 and listens to all IP addresses.
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8938, debug=True)