Created
June 19, 2021 11:06
-
-
Save shinysu/8909551d72228110de1055e8946f41a4 to your computer and use it in GitHub Desktop.
currency_converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request | |
import requests | |
API_KEY = 'ea23bb1bdf8801bbe0b5a7da925461ca' | |
app = Flask(__name__) | |
def convert_currency(from_currency, to_currency, amount): | |
url = 'http://data.fixer.io/api/latest?access_key='+API_KEY | |
data = requests.get(url).json() | |
rates = data['rates'] | |
eur_value = (1/rates[from_currency]) * amount | |
to_value = eur_value * rates[to_currency] | |
return round(to_value, 3) | |
@app.route('/', methods=["GET", "POST"]) | |
def index(): | |
converted_amount ='' | |
if request.method == "POST": | |
from_currency = request.form["from_currency"] | |
to_currency = request.form["to_currency"] | |
amount = int(request.form["amount"]) | |
converted_amount = convert_currency(from_currency, to_currency, amount) | |
return render_template('index.html', converted_amount=converted_amount) | |
elif request.method == "GET": | |
return render_template('index.html', converted_amount=converted_amount) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Currency Converter</title> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1> Currency Converter</h1> | |
<br> | |
<form action="#" method="POST"> | |
<div class="row"> | |
<div class="col-md-2"> | |
From currency: | |
</div> | |
<div class="col-md-3"> | |
<select class="form-select" aria-label="Default select example" name="from_currency"> | |
<option selected>Choose the currency</option> | |
<option value="USD">USD</option> | |
<option value="INR">INR</option> | |
<option value="EUR">EUR</option> | |
</select> | |
</div> | |
</div> | |
<br> | |
<div class="row"> | |
<div class="col-md-2"> | |
To currency: | |
</div> | |
<div class="col-md-3"> | |
<select class="form-select" aria-label="Default select example" name="to_currency"> | |
<option selected>Choose the currency</option> | |
<option value="USD">USD</option> | |
<option value="INR">INR</option> | |
<option value="EUR">EUR</option> | |
</select> | |
</div> | |
</div> | |
<br> | |
<div class="row"> | |
<div class="col-md-2"> | |
Enter the amount: | |
</div> | |
<div class="col-md-5"> | |
<input type="text" name="amount"> | |
<button type="submit" value="submit">Convert</button> | |
</div> | |
</div> | |
<br> | |
<div class="row"> | |
<div class="col-md-2"> | |
Converted amount: | |
</div> | |
<div class="col-md-3"> | |
<input type="text" name="converted_amt" value="{{ converted_amount }}" disabled> | |
</div> | |
</div> | |
</form> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment