Dealing with foreign exchange and currency conversion is a common task for developers in fintech. Building a currency converter app with real-time exchange rates can be both practical and educational. This tutorial will guide you through creating a Python-based currency converter using Streamlit and a forex API.
What Is a Currency Converter App?
A currency converter app is a tool that calculates the value of one currency in terms of another. It provides real-time conversions, helping users track fluctuating exchange rates effortlessly.
Why Use Streamlit for Your Currency Converter?
Streamlit is an open-source Python library that simplifies building web applications without requiring front-end expertise. It’s ideal for beginners who want to deploy apps quickly.
Key Benefits of Streamlit:
- No web development knowledge needed.
- Fast prototyping with minimal code.
- Easy deployment to share with others.
Prerequisites for Building a Currency Converter
To create this app, you’ll need:
- A forex API (e.g., TraderMade) for live exchange rates.
- Python installed on your system.
- Streamlit library (
pip install streamlit).
Step-by-Step Guide to Building the App
1. Install Required Libraries
Run this command in your terminal:
pip install streamlit requests2. Import Libraries and Set Up the API
import streamlit as st
import requests
base_url = "https://marketdata.tradermade.com/api/v1/convert"
api_key = "YOUR_API_KEY" # Replace with your actual API key3. Define Currency Conversion Functions
def convert_currency(amount, from_currency, to_currency):
url = f"{base_url}?api_key={api_key}&from={from_currency}&to={to_currency}&amount={amount}"
response = requests.get(url)
if response.status_code == 200:
rate = response.json()["quote"]
converted_amount = response.json()["total"]
return rate, converted_amount
return None, None
def fetch_supported_currencies():
url = "https://marketdata.tradermade.com/api/v1/live_currencies_list?api_key=YOUR_API_KEY"
response = requests.get(url)
if response.status_code == 200:
return list(response.json()["available_currencies"].keys())
return None4. Build the Streamlit Interface
def currency_converter():
st.image("tradermade_logo-01.png", width=200)
amount = st.number_input("Enter amount to convert:", value=100, step=1)
supported_currencies = fetch_supported_currencies()
if supported_currencies:
from_currency = st.selectbox("From currency:", supported_currencies, index=19)
to_currencies = st.multiselect("To currencies:", supported_currencies, default=["USD"])
if st.button("Convert"):
st.write("Conversion results:")
for to_currency in to_currencies:
rate, converted_amount = convert_currency(amount, from_currency, to_currency)
if rate:
st.write(f"{amount} {from_currency} = {converted_amount} {to_currency} (Rate: 1 {from_currency} = {rate} {to_currency})")
else:
st.error("Failed to fetch rates. Check your API key.")
if __name__ == "__main__":
currency_converter()5. Run the App
Execute this command in your terminal:
streamlit run currency_converter.pyFAQs About Python Currency Converters
1. How accurate are the exchange rates?
The rates are fetched in real-time from the API provider, ensuring high accuracy.
2. Can I add more currencies?
Yes! Update the supported_currencies list or modify the API endpoint.
3. Is Streamlit free to use?
Absolutely. Streamlit is open-source and free for deployment.
4. How do I deploy this app online?
Conclusion
You’ve just built a functional currency converter app in Python! Customize it further by:
- Adding historical rate graphs.
- Implementing error handling for API failures.
- Enhancing the UI with custom CSS.
For support, contact the API provider or explore more Python tutorials. Happy coding!
### Key Optimizations:
1. **SEO Keywords**: "Python currency converter," "Streamlit app," "forex API," "real-time exchange rates."
2. **Structure**: Clear headings, code blocks, and bullet points for readability.