-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.py
More file actions
48 lines (41 loc) · 1.62 KB
/
application.py
File metadata and controls
48 lines (41 loc) · 1.62 KB
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
from flask import Flask, render_template, request, jsonify, json
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!<br><a href='ui'>Calc and JSON</a><br><br><a href='json'>JSON return</a><br><a href='hello'>Hello</a><br><br>"
@app.route("/json")
def jsonreturn():
# read JSON file and send them to the web client
# See templates/calc.html to know how you call this
with open("./data/input.json", 'r') as f:
json_data = json.load(f)
json_str = json.dumps(json_data)
return json_str
@app.route('/hello')
@app.route('/hello/<name>')
def hello2(name=None):
# /hello will reply basic html
# /hello/<name> will reply with username specified in <name>
return render_template('hello.html', name=name)
@app.route('/ui')
def ui():
# /ui will reply basic html
# /ui/<name> will reply with username specified in <name>
return render_template('calc.html')
@app.route('/simulate')
def simulate():
# This function read two parameters from web request
# Returns the sum of two parameters.
# See templates/calc.html to know how you call this
param1 = request.args.get('input1')
param2 = request.args.get('input2')
# replace the next line with your simulator
output1 = int(param1) + int(param2)
json_str = '{"input1":' + str(param1) + ',"input2":' + str(param2) +', "output":' + str(output1) + '}'
json_data = json.loads(json_str)
# write your output to a file
with open('./data/output.json', 'w') as f:
json.dump(json_data, f, indent=4)
return json_str
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)