Create your own currency converter page with Python (Flask)
Today, we will create a simple currency converter widget for our website. We will use Python and Flask together to build this simple widget. Let’s start.
Firstly, you need to create a folder and a venv folder inside this folder:
$ mkdir currencyconverter $ cd currencyconverter $ python3 -m venv venv
If you are using Windows, then you should use this command
$ py -3 -m venv venv
We will use Python3 for this project but if you want to continue with Python2, you need to use these commands;
$ python2 -m virtualenv venv
After these steps, active your environment;
$ . venv/bin/activate
At least, install the Flask.
$ pip install Flask
Let’s start to build a currency converter
I’ll continue with MacOS, if you do not know how to use Flask on Windows. Please visit https://flask.palletsprojects.com/en/1.1.x/quickstart/
I’ll create an app.py and export it;
$ export FLASK_APP=app.py
Then, you should create a folder that named templates. Your architecture will look like this;
/app.py /templates /index.html
app.py and the index.html files will look like this.
and open the browser and go to http://127.0.0.1:5000/convert/gbp/usd/1 , you will see your index.html file here.
If you can see “gbpusd” in the output of this URL, then everything is okay.
Let’s continue. We will use the “Requests” library to make API requests. You can install “requests” with the given command below.
pip install requests
Also, we need to parse the requested JSON data. So, we need to import “json”.
We are ready to make an API request to get latest currency value.
r = req.get('https://api.finage.co.uk/last?currencies='+currency+'&apikey=YOUR_API_KEY')
This line will make an API request. We will get “currency” from the URL and calculate the amount. URL will fill the link that given above and it will look like this.
In this project, we will get the base currency and the second currency from the URL. After that, our application will make an API request to get the latest values from the Finage. Also, we get the amount from the URL. Check the last status of the index.html and app.py below;
If you make a request like http://localhost:5000/convert/usd/gbp/100
You’ll see something like this;
You can check the available currency list from here
If you want to try Finage’s API, you can get your FREE API with the link given below.
Visit Github to access this project.
If you have any questions, please feel free to ask. Also, you can contact with our support team on the live chat section.