You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Deploy a Python (Flask) web app with PostgreSQL in Azure
2
2
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.
8
4
9
5
## Requirements
10
6
@@ -18,4 +14,91 @@ The [requirements.txt](./requirements.txt) has the following packages:
18
14
|[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.|
19
15
|[pyscopg2](https://pypi.org/project/psycopg2/)| PostgreSQL database adapter for Python. |
20
16
|[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
+

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