Skip to content

Commit a5effef

Browse files
committed
v01.6
1 parent 91cae23 commit a5effef

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

currency-converter/app.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from flask import Flask, render_template, request
2+
import requests
3+
import pycountry
4+
5+
app = Flask(__name__)
6+
7+
# ✅ Directly put your API key here (⚠️ not for public sharing)
8+
API_KEY = "b22be4aca453b5cb0870cc2f"
9+
API_URL = f"https://v6.exchangerate-api.com/v6/{API_KEY}/pair"
10+
11+
# Get all ISO 4217 currencies (sorted)
12+
CURRENCIES = sorted([
13+
(c.alpha_3, c.name) for c in pycountry.currencies
14+
if hasattr(c, 'alpha_3') and hasattr(c, 'name')
15+
])
16+
17+
@app.route('/')
18+
def index():
19+
return render_template('index.html', currencies=CURRENCIES)
20+
21+
@app.route('/convert', methods=['POST'])
22+
def convert():
23+
try:
24+
amount = float(request.form['amount'])
25+
from_currency = request.form['from_currency']
26+
to_currency = request.form['to_currency']
27+
28+
url = f"{API_URL}/{from_currency}/{to_currency}"
29+
response = requests.get(url)
30+
data = response.json()
31+
32+
if data.get('result') == 'success':
33+
rate = data['conversion_rate']
34+
converted = round(amount * rate, 2)
35+
result = f"{amount} {from_currency} = {converted} {to_currency}"
36+
else:
37+
result = "Conversion failed. Please check your currencies or API key."
38+
39+
except Exception as e:
40+
result = f"Error: {str(e)}"
41+
42+
return render_template('index.html', currencies=CURRENCIES, result=result)
43+
44+
if __name__ == '__main__':
45+
app.run(debug=True)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
flask
2+
requests
3+
pycountry
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Currency Converter</title>
5+
</head>
6+
<body>
7+
<h1>Currency Converter</h1>
8+
9+
<form action="/convert" method="post">
10+
<input type="number" name="amount" step="0.01" placeholder="Enter amount" required>
11+
12+
<select name="from_currency" required>
13+
{% for code, name in currencies %}
14+
<option value="{{ code }}">{{ code }} - {{ name }}</option>
15+
{% endfor %}
16+
</select>
17+
18+
to
19+
20+
<select name="to_currency" required>
21+
{% for code, name in currencies %}
22+
<option value="{{ code }}">{{ code }} - {{ name }}</option>
23+
{% endfor %}
24+
</select>
25+
26+
<button type="submit">Convert</button>
27+
</form>
28+
29+
{% if result %}
30+
<h2>{{ result }}</h2>
31+
{% endif %}
32+
</body>
33+
</html>

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ <h3>Dodge & Survive</h3>
5656
<h3>The Sound Quiz</h3>
5757
<p>A simple quiz. You hear sounds and you have to guess them.</p>
5858
</a>
59+
</div>
60+
<div class="game-card">
61+
<a href="./currency-converter/index.html">
62+
<h3>Currency Converter</h3>
63+
<p>A currency converter with all major currencies.</p>
64+
</a>
5965
</div>
6066
</div>
6167

@@ -76,4 +82,4 @@ <h3>The Sound Quiz</h3>
7682
</script>
7783

7884
</body>
79-
</html>
85+
</html>

0 commit comments

Comments
 (0)