You want to launch an app. This could be your opportunity to smoke the competition and win big. Or you’re looking to learn how it’s done.
My previous post introduced how to set up for building an app using the Python Flask Framework. My example app, Herbly Teaminder, provides guidance for users to make a great cup of loose leaf tea.1
I chose to use the Python Flask framework to get started. There are many ways to build a web app, including no-code tools.
This isn’t about sneaking booze into a concert venue. I chose Flask because of my experience with Python and the advantages listed below.
Why Python Flask?
Python is an elegant and simple language. It’s easy to learn and taught in college programs. Flask is a lightweight Python web framework. It offers several benefits for building simple apps: 23
Easy to learn and use: Flask has a small codebase and flexible structure. This makes it easy to learn and use even with limited experience.
Flexible and modular: Flask allows developers to build modular applications. Modular applications are easier to add and remove features to as needed. This makes the application better maintainable in the future.
Rapid development: Flask is more lightweight than other frameworks like Django or React. This enables fast development and prototyping.
Extensive tools: Flask has a large collection of available tools and libraries. This makes it easy to add features and functionality to your app.
Here’s a quick example of how you can get started with the Flask framework. This code creates a simple Flask app that displays a home page. Save it as a Python (.py extension) file. Then run it with “python <script>.py” at the command-line. You can then navigate to http://127.0.0.1:5000 in the browser to view the application web page.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to the home page"
if __name__ == '__main__':
app.run(debug=True)
Flask Code Structure
Now you’ve decided on Python Flask as your web framework. My previous post detailed how to set up your environment. Now it’s time to build out the code structure. Let’s dive in to the structure for Flask best practices.
Notice that my app integrates with Amazon Web Services (AWS) cloud. This is not necessary to start building. But consider where your app will run in production. Find my code on GitHub.4
The app folder contains our application’s core logic.
The app/auth directory contains logic for user authentication.
The app/config directory defines configuration for various environments and databases. Store secrets such as credentials and API keys in environment variables or encrypted storage. An example is GitHub secrets.
The app/dao directory defines logic for database access using database access objects.
The app/models directory models the objects that our application interacts with. See the Model View Controller (MVC) organization method.5
The app/routes directory defines web routing for our web application. Routing defines how the application responds to various requests in the browser. This is done through Hypertext Transfer Protocol (HTTP).6
The app/security directory holds security scripts for our application.
The app/static directory holds static assets including front-end JavaScript and CSS styling.
The app/templates directory holds templates. The app uses these to create database objects or HTML pages using Jinja2.
The app/utils and app/services directories hold helper classes for the application. These include custom Python decorators, database services, helper utilities, and logging settings.
The config.py file defines configurations for development and production environments. Note that this relies on environment variables. Set these using scripts such as the examples in scripts/templates.
The application.py file is the main entry point for the application. The manage.py file runs the application locally in development debug mode. The Procfile defines how the application runs in production.
The requirements.txt file defines the required dependencies to run the project. Import these using pip. The requirements-dev.txt file defines dependencies for local development only.
The scripts folder holds utility scripts. These set up and test the local environment and database.
The tests folder define unit and integration tests. Building tests first puts quality at the forefront of your application design. This test-driven development (TDD) approach reduces long-term maintenance costs of software projects.7
Start Building Today
Following this program structure will help you get started building with Python Flask. Feel free to schedule some time with me if you have questions. I’d love to hear what you’re building in the comments!
Thanks for reading. Stay hungry, stay foolish.
Setup for Building a Scalable Web App
I recently began building my first full-stack web application: Herbly TeaMinder. The purpose is to help users make a great cup of loose-leaf tea by providing recommendations and tracking tea steeps.