Python Programming: Building a Cryptocurrency Tracker with Flask

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting up the Project
  4. Creating the Flask Application
  5. Building the Cryptocurrency Tracker
  6. 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:

  1. Python (version 3.6 or higher)
  2. 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:

  1. Create a new directory for the project: mkdir cryptocurrency_tracker
  2. 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:

  1. Install the virtualenv package: pip install virtualenv
  2. Create a virtual environment: virtualenv env
  3. Activate the virtual environment:
    • For Windows: env\Scripts\activate
    • For macOS/Linux: source env/bin/activate

Creating the Flask Application

Now that our project is set up, let’s create a basic Flask application. Follow these steps:

  1. Create a new Python file named app.py in the project directory.
  2. 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.

  3. Save the file and go back to your terminal or command prompt.
  4. 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.

  1. 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 the home route to fetch cryptocurrency data from the CoinGecko API and pass it to the index.html template using the render_template function.

  2. Create a new directory named templates in the project directory.
  3. Inside the templates directory, create a new file named index.html.
  4. 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.

  5. Save both app.py and index.html.
  6. Restart the Flask development server by pressing Ctrl + C in your terminal or command prompt, and then running python 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.