44import os
55from dotenv import load_dotenv
66
7- # Load environment variables from .env
87load_dotenv ()
98
109app = Flask (__name__ )
1110
12- # Get API key from .env
1311API_KEY = os .getenv ("API_KEY" )
14-
1512if not API_KEY :
16- raise ValueError ("API key not found. Make sure API_KEY is set in .env " )
13+ raise ValueError ("API key not found" )
1714
1815API_URL = f"https://v6.exchangerate-api.com/v6/{ API_KEY } /pair"
1916
20- # Get all ISO 4217 currencies (sorted)
2117CURRENCIES = sorted ([
2218 (c .alpha_3 , c .name ) for c in pycountry .currencies
2319 if hasattr (c , 'alpha_3' ) and hasattr (c , 'name' )
@@ -34,21 +30,16 @@ def convert():
3430 from_currency = request .form ['from_currency' ]
3531 to_currency = request .form ['to_currency' ]
3632
37- url = f"{ API_URL } /{ from_currency } /{ to_currency } "
38- response = requests .get (url )
33+ response = requests .get (f"{ API_URL } /{ from_currency } /{ to_currency } " )
3934 data = response .json ()
4035
4136 if data .get ('result' ) == 'success' :
42- rate = data ['conversion_rate' ]
43- converted = round (amount * rate , 2 )
37+ converted = round (amount * data ['conversion_rate' ], 2 )
4438 result = f"{ amount } { from_currency } = { converted } { to_currency } "
4539 else :
46- result = "Conversion failed. Please check your currencies or API key. "
40+ result = "Conversion failed."
4741
4842 except Exception as e :
49- result = f"Error: { str ( e ) } "
43+ result = f"Error: { e } "
5044
5145 return render_template ('index.html' , currencies = CURRENCIES , result = result )
52-
53- if __name__ == '__main__' :
54- app .run (debug = True )
0 commit comments