Skip to content

Commit bb36d72

Browse files
authored
Merge pull request #16 from pamelafox/main
Add azd support to flask-sample-app
2 parents 4da8463 + eea87c7 commit bb36d72

19 files changed

+1877
-11
lines changed

.devcontainer/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ARG VARIANT=3
2+
FROM --platform=amd64 mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}
3+
4+
RUN curl -fsSL https://aka.ms/install-azd.sh | bash
5+
6+
ENV PYTHONUNBUFFERED 1
7+
8+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
9+
&& apt-get -y install --no-install-recommends postgresql-client

.devcontainer/devcontainer.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "Python 3 & PostgreSQL",
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
"settings": {
7+
"sqltools.connections": [
8+
{
9+
"name": "Container database",
10+
"driver": "PostgreSQL",
11+
"previewLimit": 50,
12+
"server": "localhost",
13+
"port": 5432,
14+
"database": "app",
15+
"username": "app_user",
16+
"password": "app_password"
17+
}
18+
],
19+
"python.pythonPath": "/usr/local/bin/python",
20+
"python.languageServer": "Pylance",
21+
"python.linting.enabled": true,
22+
"python.linting.mypyEnabled": true,
23+
"python.testing.pytestEnabled": true,
24+
"python.formatting.provider": "black",
25+
"python.formatting.blackArgs": [
26+
"--line-length=80"
27+
],
28+
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
29+
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
30+
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
31+
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
32+
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
33+
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
34+
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
35+
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
36+
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint",
37+
"python.testing.pytestPath": "/usr/local/py-utils/bin/pytest"
38+
},
39+
"features": {
40+
"ghcr.io/devcontainers/features/azure-cli:1": {
41+
"version": "latest"
42+
},
43+
"ghcr.io/devcontainers/features/github-cli:1": {
44+
"version": "latest"
45+
}
46+
},
47+
// Add the IDs of extensions you want installed when the container is created.
48+
"extensions": [
49+
"ms-azuretools.azure-dev",
50+
"ms-python.python",
51+
"ms-python.vscode-pylance",
52+
"mtxr.sqltools",
53+
"mtxr.sqltools-driver-pg"
54+
],
55+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
56+
"forwardPorts": [
57+
5000, 5432
58+
]
59+
// Use 'postCreateCommand' to run commands after the container is created.
60+
// "postCreateCommand": "",
61+
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
62+
// "remoteUser": "vscode"
63+
}

.devcontainer/docker-compose.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: "3"
2+
3+
services:
4+
app:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
args:
9+
# [Choice] Python version: 3, 3.8, 3.7, 3.6
10+
VARIANT: 3.9
11+
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
12+
USER_UID: 1000
13+
USER_GID: 1000
14+
15+
volumes:
16+
- ..:/workspace:cached
17+
18+
# Overrides default command so things don't shut down after the process ends.
19+
command: sleep infinity
20+
21+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
22+
network_mode: service:db
23+
24+
db:
25+
image: postgres:latest
26+
restart: unless-stopped
27+
volumes:
28+
- postgres-data:/var/lib/postgresql/data
29+
environment:
30+
POSTGRES_DB: app
31+
POSTGRES_USER: app_user
32+
POSTGRES_PASSWORD: app_password
33+
34+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
35+
# (Adding the "ports" property to this file will not forward from a Codespace.)
36+
37+
volumes:
38+
postgres-data:

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FLASK_ENV=development
1+
FLASK_DEBUG=True
22
DBNAME=<database name>
33
DBHOST=<database-hostname>
44
DBUSER=<db-user-name>

.env.sample.devcontainer

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FLASK_DEBUG=True
2+
DBNAME=app
3+
DBHOST=localhost
4+
DBUSER=app_user
5+
DBPASS=app_password

.github/workflows/azure-dev.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Azure Developer CLI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
container:
13+
image: mcr.microsoft.com/azure-dev-cli-apps:latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
18+
- name: Log in with Azure
19+
uses: azure/login@v1
20+
with:
21+
creds: ${{ secrets.AZURE_CREDENTIALS }}
22+
23+
- name: Azure Dev Provision
24+
run: azd provision --no-prompt
25+
env:
26+
AZURE_ENV_NAME: ${{ secrets.AZURE_ENV_NAME }}
27+
AZURE_LOCATION: ${{ secrets.AZURE_LOCATION }}
28+
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
29+
30+
- name: Azure Dev Deploy
31+
run: azd deploy --no-prompt
32+
env:
33+
AZURE_ENV_NAME: ${{ secrets.AZURE_ENV_NAME }}
34+
AZURE_LOCATION: ${{ secrets.AZURE_LOCATION }}
35+
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

.github/workflows/python-test.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Python check
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test_package:
11+
name: Test ${{ matrix.os }} Python ${{ matrix.python_version }}
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: ["ubuntu-20.04"]
17+
python_version: ["3.8", "3.9", "3.10", "3.11"]
18+
services:
19+
postgres:
20+
image: postgres:11
21+
env:
22+
POSTGRES_PASSWORD: postgres
23+
ports:
24+
- 5432:5432
25+
# needed because the postgres container does not provide a healthcheck
26+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
27+
steps:
28+
- uses: actions/checkout@v3
29+
- name: Setup python
30+
uses: actions/setup-python@v2
31+
with:
32+
python-version: ${{ matrix.python_version }}
33+
architecture: x64
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install -r requirements.txt
38+
pip install flake8
39+
- name: Look for major issues with flake8
40+
run: |
41+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
42+
- name: Make sure Python code is all compilable
43+
run: |
44+
python -m compileall . -f
45+
- name: Run Flask server
46+
run: |
47+
flask db upgrade
48+
flask run &
49+
env:
50+
DBNAME: postgres
51+
DBHOST: localhost
52+
DBUSER: postgres
53+
DBPASS: postgres

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.azure

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Run server",
9+
"type": "python",
10+
"request": "launch",
11+
"module": "flask",
12+
"env": {
13+
"FLASK_APP": "app.py",
14+
"FLASK_DEBUG": "1"
15+
},
16+
"args": [
17+
"run",
18+
"--no-debugger",
19+
"--no-reload"
20+
],
21+
"jinja": true,
22+
"justMyCode": true
23+
}
24+
]
25+
}

README.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Deploy a Python (Flask) web app with PostgreSQL in Azure
22

3-
This is a Python web app using the Flask framework and the Azure Database for PostgreSQL relational database service. The Flask app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. For more information on how to use this web app, see the tutorial [*Deploy a Python (Django or Flask) web app with PostgreSQL in Azure*](https://docs.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app).
4-
5-
If you need an Azure account, you can [create on for free](https://azure.microsoft.com/free/).
6-
7-
A Django sample application is also available for the article at https://github.com/Azure-Samples/msdocs-django-postgresql-sample-app.
3+
This is a Python web app using the Flask framework and the Azure Database for PostgreSQL relational database service. The Flask app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. You can either deploy this project by following the tutorial [*Deploy a Python (Django or Flask) web app with PostgreSQL in Azure*](https://docs.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app) or by using the [Azure Developer CLI (azd)](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/overview) according to the instructions below.
84

95
## Requirements
106

@@ -18,4 +14,91 @@ The [requirements.txt](./requirements.txt) has the following packages:
1814
| [Flask-Migrate](https://pypi.org/project/Flask-Migrate/) | SQLAlchemy database migrations for Flask applications using Alembic. Allows functionality parity with Django version of this sample app.|
1915
| [pyscopg2](https://pypi.org/project/psycopg2/) | PostgreSQL database adapter for Python. |
2016
| [python-dotenv](https://pypi.org/project/python-dotenv/) | Read key-value pairs from .env file and set them as environment variables. In this sample app, those variables describe how to connect to the database locally. <br><br> Flask's [dotenv support](https://flask.palletsprojects.com/en/2.1.x/cli/#environment-variables-from-dotenv) sets environment variables automatically from an `.env` file. |
21-
| [flask_wtf](https://pypi.org/project/Flask-WTF/) | Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension. |
17+
| [flask_wtf](https://pypi.org/project/Flask-WTF/) | Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension. |
18+
19+
20+
## Using this project with the Azure Developer CLI (azd)
21+
22+
This project is designed to work well with the [Azure Developer CLI](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/overview),
23+
which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
24+
25+
### Local development
26+
27+
This project has devcontainer support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension.
28+
29+
🎥 [Watch a screencast of running the app in Github Codespaces.](https://www.youtube.com/watch?v=r6Hnp9RXUpY)
30+
31+
Steps for running the server:
32+
33+
1. (Optional) If you're unable to open the devcontainer, [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate that.
34+
35+
2. Install the requirements:
36+
37+
```shell
38+
pip install -r requirements.txt
39+
```
40+
41+
3. Create an `.env` file using `.env.sample` as a guide. Set the value of `DBNAME` to the name of an existing database in your local PostgreSQL instance. Set the values of `DBHOST`, `DBUSER`, and `DBPASS` as appropriate for your local PostgreSQL instance. If you're in the devcontainer, copy the values from `.env.sample.devcontainer`.
42+
43+
4. Run the migrations:
44+
45+
```shell
46+
flask db upgrade
47+
```
48+
49+
5. Run the local server: (or use VS Code "Run" button and select "Run server")
50+
51+
```shell
52+
flask run
53+
```
54+
55+
### Deployment
56+
57+
This repo is set up for deployment on Azure App Service (w/PostGreSQL server) using the configuration files in the `infra` folder.
58+
59+
🎥 [Watch a screencast of deploying and re-deploying the app.](https://www.youtube.com/watch?v=r6Hnp9RXUpY)
60+
61+
Steps for deployment:
62+
63+
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/)
64+
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd). (If you opened this repository in a devcontainer, that part will be done for you.)
65+
3. Provision and deploy all the resources:
66+
67+
```shell
68+
azd up
69+
```
70+
71+
It will prompt you to login and to provide a name (like "flask-app") and 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 (like to "centralus") can help, as there are availability constraints for some of the resources.
72+
73+
4. 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 restaurant review app! 🎉 If you see an error, open the Azure Portal from the URL in the command output, navigate to the App Service, select Logstream, and check the logs for any errors.
74+
75+
![Screenshot of Flask restaurants website](screenshot_website.png)
76+
77+
5. When you've made any changes to the app code, you can just run:
78+
79+
```shell
80+
azd deploy
81+
```
82+
83+
### CI/CD pipeline
84+
85+
This project includes a Github workflow for deploying the resources to Azure
86+
on every push. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:
87+
88+
```shell
89+
azd pipeline config
90+
```
91+
92+
### Monitoring
93+
94+
The deployed resources include a Log Analytics workspace with an Application Insights dashboard to measure metrics like server response time.
95+
96+
To open that dashboard, just run:
97+
98+
```shell
99+
azd monitor --overview
100+
```
101+
102+
## Getting help
103+
104+
If you're working with this project and running into issues, please post in [Discussions](/discussions).

0 commit comments

Comments
 (0)