-
Notifications
You must be signed in to change notification settings - Fork 1
/
currency.py
42 lines (27 loc) · 966 Bytes
/
currency.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
from flask import Flask, render_template, request
import requests
app = Flask(__name__)
@app.route("/")
def main():
return render_template("start.html")
@app.route("/convert")
def convert():
amount = request.args.get('amount')
base = request.args.get('from').upper()
other = request.args.get('to').upper()
if base is None or other is None:
return render_template("start.html")
if amount is "":
amount = "1"
res = requests.get("https://api.exchangeratesapi.io/latest",
params={"base": base, "symbols": other})
if res.status_code != 200:
return render_template("error.html")
data = res.json()
for x in data["rates"]:
print(x)
rate = data["rates"][other]
ans = float(amount)*float(rate)
return render_template("answer.html", ans = ans, amount = amount, base = base, rate = rate, other = other)
if __name__ == '__main__':
app.run(debug=True)