Python and Blockchain: Building Decentralized Apps (DApps) with Flask
In the world of technology, there are few buzzwords as popular and intriguing as “blockchain.” This revolutionary technology has gained immense popularity in recent years, thanks to its potential for decentralization and immutability. And what better way to harness the power of blockchain than by combining it with Python, a versatile and powerful programming language?

In this article, we’ll explore the fascinating realm of building decentralized applications, or DApps, using Flask, a popular web framework for Python. We’ll dive into the core concepts of blockchain and understand how Python and Flask can be used to build robust and secure DApps. Whether you’re a beginner just starting your Python journey or an experienced developer looking to explore the world of blockchain, this article has something for you. So, let’s get started!
Understanding Blockchain: The Basics
Before we delve into the world of DApps, we need to have a solid understanding of what blockchain is and how it works. At its core, a blockchain is a decentralized and distributed ledger that records transactions across multiple computers (nodes). Each transaction is grouped into a “block” and added to a chain of previous blocks, forming a chronological record of all transactions.
One of the key features of blockchain is its immutability. Once a transaction is added to the blockchain, it cannot be altered or deleted, making it highly secure. This immutability is achieved through cryptographic hash functions, which create a unique hash for each block based on its content. Any change to a block would result in a different hash, alerting the network to tampering attempts.
Python and Blockchain: A Match Made in Heaven
Python, with its readability, simplicity, and extensive libraries, is a perfect fit for developing blockchain applications. It provides a wide range of libraries such as hashlib
and cryptography
that make it easy to work with cryptographic functions essential for blockchain implementation. Additionally, Python’s syntax makes it accessible to beginners while providing powerful features for more experienced developers.
To demonstrate the power of Python in building DApps, we’ll focus on Flask, a lightweight web framework that is ideal for rapid development. Flask allows us to build web-based interfaces for interacting with blockchain networks, making it a valuable tool for creating user-friendly DApps.
Getting Started with Flask: Setting Up Your Development Environment
Before we start coding our DApp, we need to set up our development environment. First, make sure you have Python installed on your machine. You can download the latest version from the official Python website.
Next, we’ll install Flask. Open your command prompt or terminal and run the following command:
pip install flask
This command will install Flask and its dependencies on your machine. Once the installation is complete, you’re ready to start building your DApp!
Building the Blockchain: Creating the Foundation
Now that we have our development environment ready, let’s start building our blockchain. In this example, we’ll create a basic blockchain using only Python and Flask. Keep in mind that this is a simplified version for educational purposes, and a real-world blockchain implementation would be more complex.
Step 1: Setting Up Our Flask Application
To begin, create a new folder for your project and navigate to it in your command prompt or terminal. Inside the folder, create a new file called blockchain.py
. This file will serve as the main entry point for our Flask application.
Open blockchain.py
in your favorite text editor and add the following code:
from flask import Flask
# Create a Flask application
app = Flask(__name__)
# Run the application
if __name__ == '__main__':
app.run(debug=True)
Save the file and return to your command prompt or terminal. Run the following command to start your Flask application:
python blockchain.py
If everything is set up correctly, you should see output indicating that your Flask application is running. Open your web browser and navigate to http://localhost:5000
. You should see a “Hello, World!” message, indicating that your Flask application is working correctly.
Step 2: Creating the Blockchain Class
Now that our Flask application is up and running, let’s create the foundation of our blockchain. In your blockchain.py
file, add the following code:
from flask import Flask, jsonify
class Blockchain:
def __init__(self):
self.chain = []
self.current_transactions = []
def new_block(self):
# Create a new block and add it to the blockchain
pass
def new_transaction(self):
# Create a new transaction and add it to the list of transactions
pass
@staticmethod
def hash(block):
# Hashes a block
pass
@property
def last_block(self):
# Returns the last block in the blockchain
pass
# Create a Flask application
app = Flask(__name__)
# Create a new instance of the Blockchain class
blockchain = Blockchain()
# Run the application
if __name__ == '__main__':
app.run(debug=True)
In this code snippet, we define a Blockchain
class with several methods for creating new blocks, adding transactions, and hashing blocks. Note that we have left the implementation of these methods empty for now. We’ll fill them in as we progress through the article.
Step 3: Adding Endpoints for Interacting with the Blockchain
Now that we have the foundation of our blockchain, we can start adding endpoints for interacting with it. In Flask, endpoints are defined using decorators.
Add the following code after the Blockchain
class definition:
# ...
@app.route('/mine', methods=['GET'])
def mine():
# Mining logic goes here
return "Mining a new block"
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
# Create a new transaction
return "Adding a new transaction"
@app.route('/chain', methods=['GET'])
def full_chain():
# Return the full blockchain
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200
# ...
In this code, we define three endpoints: /mine
, /transactions/new
, and /chain
.
The /mine
endpoint will handle the mining process, which involves creating a new block. We’ll implement the mining logic later in the article.
The /transactions/new
endpoint will handle the creation of new transactions and adding them to the blockchain.
The /chain
endpoint will return the full blockchain to the user. We’ll use the jsonify
function from Flask to convert our response into JSON format.
Step 4: Implementing the Blockchain Methods
With our endpoints in place, we can now implement the methods of our Blockchain
class to handle the actual blockchain logic. Let’s start with the hash
method.
Add the following code inside the Blockchain
class:
# ...
import hashlib
import json
# ...
@staticmethod
def hash(block):
"""
Generates a SHA-256 hash for a block
:param block: Block object
:return: str
"""
# Sort the dictionary to ensure consistent hashes
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
# ...
In this code, we import the hashlib
and json
modules. The hash
method takes a block as input, converts it into a JSON string, sorts it to ensure consistent hashes, encodes it as bytes, and finally generates a SHA-256 hash.
Next, let’s implement the new_block
and new_transaction
methods.
Add the following code inside the Blockchain
class:
# ...
from time import time
# ...
def new_block(self, proof, previous_hash=None):
"""
Creates a new block and adds it to the blockchain
:param proof: int - Proof of work
:param previous_hash: str - Hash of previous block
:return: dict - New block
"""
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
'transactions': self.current_transactions,
}
# Reset the current list of transactions
self.current_transactions = []
# Add the block to the blockchain
self.chain.append(block)
return block
def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction and adds it to the list of transactions
:param sender: str - Sender's address
:param recipient: str - Recipient's address
:param amount: int - Transaction amount
:return: int - Index of the block that will hold this transaction
"""
transaction = {
'sender': sender,
'recipient': recipient,
'amount': amount,
}
self.current_transactions.append(transaction)
# Return the index of the block that will hold this transaction
return self.last_block['index'] + 1
# ...
In the new_block
method, we create a new block with an index, a timestamp, the proof of work, the previous block’s hash, and the list of transactions. We also reset the list of current transactions, since they have been added to a block.
The new_transaction
method takes three parameters: the sender’s address, the recipient’s address, and the amount of the transaction. It creates a new transaction and adds it to the list of current transactions.
Step 5: Completing the Mining Logic
Now that we have implemented the basic blockchain logic, let’s complete the mining logic.
Modify the /mine
endpoint as follows:
# ...
@app.route('/mine', methods=['GET'])
def mine():
"""
Mines a new block by finding a proof of work
:return: str - Message indicating the mining result
"""
# We'll implement the mining logic here later
return "Mining a new block"
# ...
To mine a new block, we need to find a proof of work, which is a computationally expensive problem to solve. In our simplified example, we’ll use a simple proof of work algorithm that involves finding a number that, when hashed with the previous block’s proof, produces a hash with four leading zeros.
Add the following code inside the /mine
endpoint:
# ...
@app.route('/mine', methods=['GET'])
def mine():
"""
Mines a new block by finding a proof of work
:return: str - Message indicating the mining result
"""
# Generate a new proof of work
last_block = blockchain.last_block
last_proof = last_block['proof']
proof = blockchain.proof_of_work(last_proof)
# Reward the miner (us) for finding the proof
# The "sender" is "0" to indicate that this node has mined a new coin
blockchain.new_transaction(
sender="0",
recipient="our_address",
amount=1,
)
# Create a new block and add it to the blockchain
previous_hash = blockchain.hash(last_block)
block = blockchain.new_block(proof, previous_hash)
response = {
'message': 'New block mined',
'index': block['index'],
'transactions': block['transactions'],
'proof': block['proof'],
'previous_hash': block['previous_hash'],
}
return jsonify(response), 200
# ...
In this code, we generate a new proof of work by calling the proof_of_work
method, which we’ll implement shortly. We then reward ourselves for finding the proof by creating a new transaction with a sender of “0”. Finally, we create a new block and return the relevant information to the user.
Add the following code inside the Blockchain
class:
# ...
from urllib.parse import urlparse
# ...
def proof_of_work(self, last_proof):
"""
Simple proof of work algorithm
- Find a number p' such that hash(pp') contains 4 leading zeros, where p is the previous proof
- p is the previous proof, and p' is the new proof
:param last_proof: int - Previous proof
:return: int - New proof
"""
proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1
return proof
@staticmethod
def valid_proof(last_proof, proof):
"""
Validates the proof: Does hash(last_proof, proof) contain 4 leading zeros?
:param last_proof: int - Previous proof
:param proof: int - Current proof
:return: bool - True if correct, False otherwise
"""
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"
# ...
In the proof_of_work
method, we start with a proof value of 0 and increment it until we find a proof that, when combined with the previous proof, produces a hash with four leading zeros. This process is computationally expensive and serves as the basis for the security of the blockchain.
The valid_proof
method checks if a given proof and the previous proof constitute a valid proof of work. In our case, a valid proof should produce a hash with four leading zeros.
Step 6: Interacting with the Blockchain
Now that we have implemented the core blockchain logic, let’s test our DApp by interacting with the blockchain.
Restart your Flask application if it’s not already running and open a new browser tab. Use an API development tool like Postman to send HTTP requests to our DApp.
Creating a New Transaction
Send a POST
request to http://localhost:5000/transactions/new
with the following JSON payload:
{
"sender": "sender_address",
"recipient": "recipient_address",
"amount": 1
}
Replace sender_address
and recipient_address
with the actual addresses you want to use. This request will create a new transaction and add it to the list of transactions.
Mining a New Block
To mine a new block, send a GET
request to http://localhost:5000/mine
. This request will trigger the mining process, resulting in the creation of a new block and the reward transaction for the miner.
Viewing the Full Blockchain
To view the full blockchain, send a GET
request to http://localhost:5000/chain
. This request will return the full blockchain in JSON format.
Congratulations! You have successfully built a basic blockchain using Python and Flask. While this implementation is simplified for educational purposes, it provides a solid foundation for understanding the concepts and principles behind the blockchain technology.
Real-World Applications of DApps with Flask
Now that we have a good understanding of the fundamentals, let’s explore some real-world applications for DApps built with Python and Flask.
Supply Chain Management
One potential use case for DApps is supply chain management. Blockchain technology can facilitate transparent and secure tracking of products from their origin to the end consumers. Using Flask, Python developers can build DApps that enable suppliers, manufacturers, and retailers to record and verify transactions related to the supply chain. This brings transparency and trust to the entire process, reducing the risk of fraud and ensuring the authenticity of products.
Voting Systems
Another area where DApps can make a significant impact is in voting systems. By leveraging the immutability and transparency of the blockchain, DApps can provide a secure and efficient way to conduct elections. With Flask, Python developers can build DApps that allow voters to cast their votes securely and enable administrators to count and verify the results accurately. This eliminates the need for intermediaries and ensures the integrity of the voting process.
Decentralized Finance (DeFi)
Decentralized finance, also known as DeFi, is a rapidly growing sector that aims to recreate traditional financial systems using blockchain technology. DeFi applications enable users to access financial services such as lending, borrowing, and trading directly without the need for intermediaries. By building DApps with Flask and Python, developers can contribute to the DeFi ecosystem by creating secure and user-friendly platforms for managing digital assets and participating in decentralized lending protocols.
Closing Thoughts
In this article, we explored the exciting world of building decentralized applications, or DApps, using Python and Flask. We learned the basics of blockchain technology and how Python, with its extensive libraries and intuitive syntax, is an excellent choice for developing blockchain applications. By following the step-by-step guide, we built a simple blockchain using Flask and Python, and explored real-world applications for DApps.
Building DApps with Python and Flask opens up a world of possibilities, from transforming supply chain management to making voting systems more secure and enabling decentralized finance. Python and Flask empower developers to explore innovative solutions and contribute to the growing blockchain ecosystem.
So, whether you’re a beginner taking your first steps in Python or an experienced developer looking to expand your skill set, Python and blockchain are a powerful combination that holds immense potential. Start exploring the world of DApps with Flask today, and unlock the possibilities of a decentralized future!
To learn more about blockchain development using Python and Flask, be sure to check out the official Flask documentation (http://flask.pocoo.org/docs/) and the Python cryptography library documentation (https://cryptography.io/en/latest/).