Best Flask open-source libraries and packages

Flask surveys container app

An example Flask app for public surveys (no user auth) designed to be run inside Docker and deployed to Azure Container Apps with the Azure Developer CLI.
Updated 3 months ago

Open in GitHub Codespaces Open in Remote - Containers

Flask surveys container app

This repository includes a Flask surveys app that uses SQLAlchemy (via Flask-SQLAlchemy and Flask-Migrate) to interact with a PostgreSQL database.

Screenshot of surveys app, showing a survey about ice cream with four options and percentage bars

The app is organized using Flask Blueprints, tested with pytest, linted with ruff, and formatted with black. Code quality issues are all checked with both pre-commit and Github actions.

The repository is designed for use with Docker containers, both for local development and deployment, and includes infrastructure files for deployment to Azure Container Apps. 🐳

Opening the project

This project has Dev Container support, so it will be be setup automatically if you open it in Github Codespaces or in local VS Code with the Dev Containers extension.

If you're not using one of those options for opening the project, then you'll need to:

  1. Create a Python virtual environment and activate it.

  2. Install requirements:

    python3 -m pip install --user -r requirements-dev.txt
  3. Install the pre-commit hooks:

    pre-commit install

Local development with Docker

Since this app depends on a database, there's a docker-compose.yaml file that creates two containers (one for the app, one for the DB) as well as a volume to store the database data.

  1. Install Docker Desktop. If you opened this inside Github Codespaces or a Dev Container in VS Code, installation is not needed. ⚠️ If you're on an Apple M1/M2, you won't be able to run docker commands inside a Dev Container; either use Codespaces or do not open the Dev Container.

  2. Create an .env file by copying .env.example.

  3. Start the services with this command:

    docker-compose up --build
  4. Try creating a new survey and answering your newly created survey. 📋 🎉

Deployment

This repo is set up for deployment on Azure Container Apps with a PostGreSQL server using the Dockerfile and the configuration files in the infra folder.

Architecture diagram for Azure Container Apps, Azure Container Registry, and PostgreSQL Flexible Server

Steps for deployment:

  1. Sign up for a free Azure account and create an Azure Subscription.

  2. Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)

  3. Login to Azure:

    azd auth login
  4. Provision and deploy all the resources:

    azd up

    It will prompt you to provide an azd environment name (like "flask-app"), select a subscription from your Azure account, and select a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location can help, as there may be availability constraints for some of the resources.

  5. When azd has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the front page of the app! 🎉

  6. When you've made any changes to the app code, you can just run:

    azd deploy

CI/CD pipeline

This project includes a Github workflow for deploying the resources to Azure on every push to main. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:

azd pipeline config

Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. Some of the Azure resources used in this infrastructure are on usage-based pricing tiers, but the Azure Database for PostgreSQL flexible server and Container Registry have fixed hourly costs.

You can try the Azure pricing calculator for the resources:

  • Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
  • Azure Container Registry: Basic tier. Pricing
  • Azure Database for PostgreSQL flexible server - Burstable tier (B1ms). Pricing
  • Key Vault - Standard tier. Pricing based on number of operations, only a few are used on each deploy. Pricing
  • Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing

⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use, either by deleting the resource group in the Portal or running azd down.

Getting help

If you're working with this project and running into issues, please post in Discussions.