Welcome to Day 7 of the 12 Days of DigitalOcean! Today, we’re kicking off the second project in this series: an Email-Based Receipt Processing Service. This app is designed to make your life a little easier by organizing your emailed receipts into a database. No more endless scrolling through your inbox to find that one receipt you need.
If you enjoyed the Birthday Reminder Service, you’ll love this next step. It’s another practical, production-ready app that highlights how DigitalOcean can help you turn ideas into real, working solutions. By the end of this tutorial, your Flask app will be live on the web and ready to process receipt data like a pro.
We’ve all been there—scrambling to find a specific receipt for tax season or reimbursements. It’s not fun. This app aims to solve that by automating the whole process.
It’s simple, efficient, and takes a tedious task off your plate. Today, we’re focusing on building the backend system with Flask, setting it up for production with Gunicorn, and deploying it to DigitalOcean’s App Platform.
Here’s what we’ll cover today:
This tutorial will give you hands-on experience with deploying a real-world Flask app while keeping things manageable and fun.
Before diving in, make sure you have:
Flask will serve as the backbone of this project, handling all incoming requests and processing the receipt data. Let’s get started by setting up the basics.
Create Your Project Directory:
Keeping everything in a dedicated folder makes managing your project easier as it grows. Run the following commands to set up the directory:
mkdir email-receipt-processor
cd email-receipt-processor
Set Up a Virtual Environment:
A virtual environment is like a sandbox for your project. It keeps your dependencies isolated, so you don’t accidentally break other Python projects or your system’s Python setup.
pip install virtualenv
virtualenv -p python3 venv
Activate the Virtual Environment: Activate your virtual environment to install dependencies locally for the project:
source venv/bin/activate
Install Flask and Gunicorn:
Flask will be your web framework, and Gunicorn will handle production server duties.
pip install Flask gunicorn
Save Your Dependencies:
A requirements.txt
file ensures everyone (and every machine) running your project has the same setup.
pip freeze > requirements.txt
Write Your Flask App:
Let’s start with a simple Flask app. Create a file called app.py
and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to the Email-Based Receipt Processor!"
if __name__ == "__main__":
app.run()
Test It Locally:
Fire up the app and check it in your browser:
python app.py
Open your browser and navigate to http://localhost:5000
. You should see: “Welcome to the Email-Based Receipt Processor!”
Flask’s built-in server is great for development, but it’s not designed to handle the traffic and performance needs of a production app. That’s where Gunicorn comes in.
Create a Gunicorn Config File:
Gunicorn needs a configuration file to know how to run your app. Create one with:
touch gunicorn_config.py
Configure Gunicorn:
Open gunicorn_config.py
and add:
bind = "0.0.0.0:8080"
workers = 2
bind
: Makes the app accessible over the web on port 8080workers
: Sets the number of simultaneous requests the app can handle. Two is a good starting point for small apps.GitHub is where the magic happens—it’s how we’ll version control the app and integrate it with DigitalOcean.
Initialize a Git Repository:
A .gitignore
file prevents unnecessary files (like your virtual environment) from being tracked by Git. Run:
git init
echo "venv/" > .gitignore
echo "*.pyc" >> .gitignore
git add .
git commit -m "Initial Flask app with Gunicorn"
Here’s what your project should look like in VS Code, for example:
Create a GitHub Repository:
Head to GitHub, create a new repo called email-receipt-processor
, and follow the steps to set it up.
Push Your Code to GitHub:
Back in your terminal, type:
git branch -M main
git remote add origin https://github.com/<your-github-username>/email-receipt-processor.git
git push -u origin main
Replace <your-github-username>
with your GitHub username
Now it’s time to deploy your app! DigitalOcean makes this easy with its App Platform.
Log in to your DigitalOcean dashboard and go to Apps.
Click Create App, select GitHub, and connect your email-receipt-processor
repo.
Click “Connect GitHub Account”: You will be redirected to GitHub to authorize DigitalOcean. If prompted, log into GitHub and grant DigitalOcean access to your repositories.
Select the Repository: Choose the email-receipt-processor
repository you created earlier from the list of available repositories.
<$>[note] Tip: Ensure that you have selected the correct branch (e.g., main
) for deployment.
<$>
Click Save and Continue: This redirects you back to the DigitalOcean App Platform configuration screen.
Select the Repository from the Dropdown: Once back in the App Platform, you’ll see a dropdown listing your connected repositories. Select email-receipt-processor
.
Choose Resources: You can adjust the resource allocations (RAM, CPU, and bandwidth) for your app.
Scroll Down to the Run Command Section: This step ensures DigitalOcean knows how to start your app in the production environment.
Click “Edit”: Replace the default command with the following:
gunicorn --worker-tmp-dir /dev/shm -c gunicorn_config.py app:app
This command uses Gunicorn to serve your Flask app.
The --worker-tmp-dir /dev/shm
flag optimizes performance in containerized environments.
Review Configuration: On the final screen, review your app’s settings. Ensure the resource allocations and run command are correct. Then click Back to get back to Resources.
Environment Variables: On the next screen, you’ll see an option to set environment variables. For now, leave everything as default and click Next.
App Region: Choose a deployment region close to your target audience or leave it as the default.
Create Resources: On the final screen, review your app’s settings. Then click on Create Resources. This starts the deployment process. Sit back while DigitalOcean sets everything up!
Deployment Status: Once your app is deployed, you’ll see a confirmation screen with a link to your app’s public URL.
Test Your App: Visit the URL in your browser. You should see the message: “Welcome to the Email-Based Receipt Processor!”
You did it! Here’s what you accomplished today:
Your app is now up and running—an important milestone in this series. In the next tutorial, you’ll take things further by integrating Postmark to handle email forwarding. Get ready to level up your app—it will be awesome! 🚀
Here is the next tutorial from this series on Day 8: Connecting Postmark to Your Flask App.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!