import requests
import csv
import json
from flask import Flask, render_template, request
app = Flask(__name__)
response = requests.get("http://api.nbp.pl/api/exchangerates/tables/A?format=json")
mydata = response.json()
fields = ['Currency', 'Code', 'Mid']
lastdata = []
for x in mydata[0]['rates']:
listing = [x['currency'], x['code'], x['mid']]
lastdata.append(listing)
@app.route('/kalkulator', methods=["GET", "POST"])
def calc():
wynik = ""
if request.method == "POST":
data = request.form
curr = data.get('curr')
quantity = data.get('quantity')
for item in lastdata:
if curr == item[1]:
wynik =float(item[2]) * int(quantity)
return wynik
break
print(f"Musisz wydać: {wynik}")
return render_template("index.html", result=wynik)
A oto szablon:
<!DOCTYPE html>
<html>
<body>
<h1>Kalkulator walut</h1>
<form method="POST">
<label for="currencies">Wybierz walutę, którą chcesz wymienić:</label>
<select name="curr" id="curr">
<option value="usd">USD</option>
<option value="thb">THB</option>
<option value="aud">AUD</option>
<option value="hkd">HKD</option>
<option value="cad">CAD</option>
<option value="nzd">NZD</option>
<option value="sgd">SGD</option>
<option value="eur">EUR</option>
<option value="huf">HUF</option>
<option value="chf">CHF</option>
<option value="gbp">GBP</option>
<option value="uah">UAH</option>
<option value="usd">USD</option>
<option value="jpy">JPY</option>
<option value="czk">CZK</option>
<option value="dkk">DKK</option>
<option value="isk">ISK</option>
<option value="nok">NOK</option>
<option value="sek">SEK</option>
</select>
<label for="quantity">Ile waluty wymieniasz?: </label>
<input type="text" id="quantity" name="quantity"><br>
<input type="submit" value="Submit">
</form>
{{result}}
</body>
</html>