Table of Contents
- Introduction
- Prerequisites
- Setting up the Project
- Creating the Flask Application
- Building the Cryptocurrency Tracker
- Conclusion
Introduction
In this tutorial, we will learn how to build a Cryptocurrency Tracker using Flask, a popular Python web framework. By the end of this tutorial, you will have a fully functional web application that can display real-time information about various cryptocurrencies.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of Python programming. Familiarity with web development concepts and Flask will be helpful but not necessary. You will also need to have the following software installed:
- Python (version 3.6 or higher)
- Flask (installed via pip)
Setting up the Project
First, let’s create a project directory for our Cryptocurrency Tracker. Open your terminal or command prompt and follow these steps:
- Create a new directory for the project:
mkdir cryptocurrency_tracker
- Enter the project directory:
cd cryptocurrency_tracker
Next, we need to set up a virtual environment to isolate our project dependencies. Run the following commands:
- Install the virtualenv package:
pip install virtualenv
- Create a virtual environment:
virtualenv env
- Activate the virtual environment:
- For Windows:
env\Scripts\activate
- For macOS/Linux:
source env/bin/activate
- For Windows:
Creating the Flask Application
Now that our project is set up, let’s create a basic Flask application. Follow these steps:
- Create a new Python file named
app.py
in the project directory. - Open
app.py
in a text editor and add the following code:from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, world!" if __name__ == '__main__': app.run(debug=True)
In this code, we import Flask and create an instance of the Flask class called
app
. We define a route/
that returns a “Hello, world!” message. Finally, we run the Flask application if the script is executed directly. - Save the file and go back to your terminal or command prompt.
- Start the Flask development server by running
python app.py
.
You should see an output similar to this:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Open your web browser and visit http://127.0.0.1:5000/
. You should see the “Hello, world!” message displayed.
Building the Cryptocurrency Tracker
Now that we have a basic Flask application, let’s integrate a cryptocurrency API to fetch real-time data. We’ll be using the CoinGecko API in this tutorial.
- Open
app.py
in your text editor and modify the code as follows:from flask import Flask, render_template import requests app = Flask(__name__) @app.route('/') def home(): # Fetch cryptocurrency data from CoinGecko API url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin,ethereum,litecoin' response = requests.get(url) data = response.json() # Render the template with cryptocurrency data return render_template('index.html', cryptocurrencies=data) if __name__ == '__main__': app.run(debug=True)
In this code, we import the
requests
library to make HTTP requests. We modify thehome
route to fetch cryptocurrency data from the CoinGecko API and pass it to theindex.html
template using therender_template
function. - Create a new directory named
templates
in the project directory. - Inside the
templates
directory, create a new file namedindex.html
. - Open
index.html
in a text editor and add the following code:<!DOCTYPE html> <html> <head> <title>Cryptocurrency Tracker</title> </head> <body> <h1>Cryptocurrency Tracker</h1> <table> <tr> <th>Name</th> <th>Symbol</th> <th>Price</th> </tr> {% for crypto in cryptocurrencies %} <tr> <td>{{ crypto['name'] }}</td> <td>{{ crypto['symbol'].upper() }}</td> <td>{{ crypto['current_price'] }}</td> </tr> {% endfor %} </table> </body> </html>
In this code, we define an HTML table to display the cryptocurrency data. We use the Jinja2 templating engine to loop over the
cryptocurrencies
data and dynamically populate the table rows with the cryptocurrency name, symbol, and current price. - Save both
app.py
andindex.html
. - Restart the Flask development server by pressing
Ctrl + C
in your terminal or command prompt, and then runningpython app.py
again.
Visit http://127.0.0.1:5000/
in your web browser, and you should see a table displaying the current prices of Bitcoin, Ethereum, and Litecoin.
Congratulations! You have successfully built a Cryptocurrency Tracker web application using Flask. Feel free to explore further by adding more features to the application, such as historical price charts or additional cryptocurrencies.
Conclusion
In this tutorial, we learned how to build a Cryptocurrency Tracker using Flask and the CoinGecko API. We covered the basic steps of setting up a Flask project, created a simple Flask application, and integrated a cryptocurrency API to fetch real-time data. By following this tutorial, you should now have a solid foundation for building web applications with Flask and integrating external APIs.