Skip to content

Commit 2e5bc99

Browse files
committed
2 parents da1340b + ef6d08d commit 2e5bc99

File tree

6 files changed

+80
-80
lines changed

6 files changed

+80
-80
lines changed

‎.devcontainer/devcontainer.json‎

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,53 @@
33
"dockerComposeFile": "docker-compose.yml",
44
"service": "app",
55
"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"
6+
"features":{
7+
},
8+
"customizations":{
9+
"vscode":{
10+
// Add the IDs of extensions you want installed when the container is created.
11+
"extensions": [
12+
"ms-azuretools.azure-dev",
13+
"ms-python.python",
14+
"ms-python.vscode-pylance",
15+
"mtxr.sqltools",
16+
"mtxr.sqltools-driver-pg"
17+
],
18+
"settings":{
19+
"sqltools.connections": [
20+
{
21+
"name": "Container database",
22+
"driver": "PostgreSQL",
23+
"previewLimit": 50,
24+
"server": "localhost",
25+
"port": 5432,
26+
"database": "app",
27+
"username": "app_user",
28+
"password": "app_password"
29+
}
30+
],
31+
"python.pythonPath": "/usr/local/bin/python",
32+
"python.languageServer": "Pylance",
33+
"python.linting.enabled": true,
34+
"python.linting.mypyEnabled": true,
35+
"python.testing.pytestEnabled": true,
36+
"python.formatting.provider": "black",
37+
"python.formatting.blackArgs": [
38+
"--line-length=80"
39+
],
40+
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
41+
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
42+
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
43+
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
44+
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
45+
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
46+
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
47+
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
48+
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint",
49+
"python.testing.pytestPath": "/usr/local/py-utils/bin/pytest"
1750
}
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"
51+
}
3852
},
39-
"features":{
40-
"ghcr.io/devcontainers/features/github-cli:1":{
41-
"version": "latest"
42-
}
43-
},
44-
// Add the IDs of extensions you want installed when the container is created.
45-
"extensions": [
46-
"ms-azuretools.azure-dev",
47-
"ms-python.python",
48-
"ms-python.vscode-pylance",
49-
"mtxr.sqltools",
50-
"mtxr.sqltools-driver-pg"
51-
],
5253
// Use 'forwardPorts' to make a list of ports inside the container available locally.
5354
"forwardPorts": [
5455
5000, 5432

‎.devcontainer/docker-compose.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
dockerfile: .devcontainer/Dockerfile
88
args:
99
# [Choice] Python version: 3, 3.8, 3.7, 3.6
10-
VARIANT: 3.9
10+
VARIANT: "3.10"
1111
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
1212
USER_UID: 1000
1313
USER_GID: 1000

‎README.md‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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. 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.
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/azure/app-service/tutorial-python-postgresql-app) or by using the [Azure Developer CLI (azd)](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview) according to the instructions below.
44

55
## Requirements
66

@@ -16,41 +16,40 @@ The [requirements.txt](./requirements.txt) has the following packages:
1616
|[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. |
1717
|[flask_wtf](https://pypi.org/project/Flask-WTF/)| Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension. |
1818

19-
2019
## Using this project with the Azure Developer CLI (azd)
2120

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),
21+
This project is designed to work well with the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview),
2322
which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
2423

2524
### Local development
2625

27-
This project has devcontainer support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension.
26+
This project has devcontainer support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension.
2827

2928
🎥 [Watch a screencast of running the app in Github Codespaces.](https://www.youtube.com/watch?v=r6Hnp9RXUpY)
3029

31-
Steps for running the server:
30+
Steps for running the server:
3231

3332
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.
3433

3534
2. Install the requirements:
3635

37-
```shell
38-
pip install -r requirements.txt
39-
```
36+
```shell
37+
python3 -m pip install -r requirements.txt
38+
```
4039

4140
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 foryour local PostgreSQL instance. If you'rein the devcontainer, copy the values from `.env.sample.devcontainer`.
4241

4342
4. Run the migrations:
4443

45-
```shell
46-
flask db upgrade
47-
```
44+
```shell
45+
python3 -m flask db upgrade
46+
```
4847

4948
5. Run the local server: (or use VS Code "Run" button and select"Run server")
5049
51-
```shell
52-
flask run
53-
```
50+
```shell
51+
python3 -m flask run
52+
```
5453
5554
### Deployment
5655
@@ -64,21 +63,21 @@ Steps for deployment:
6463
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.)
6564
3. Provision and deploy all the resources:
6665
67-
```shell
68-
azd up
69-
```
66+
```shell
67+
azd up
68+
```
7069
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.
70+
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.
7271
7372
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.
7473
75-
![Screenshot of Flask restaurants website](screenshot_website.png)
74+
![Screenshot of Flask restaurants website](screenshot_website.png)
7675
7776
5. When you've made any changes to the app code, you can just run:
7877
79-
```shell
80-
azd deploy
81-
```
78+
```shell
79+
azd deploy
80+
```
8281
8382
### CI/CD pipeline
8483
@@ -101,4 +100,4 @@ azd monitor --overview
101100
102101
## Getting help
103102
104-
If you're working with this project and running into issues, please post in [Discussions](/discussions).
103+
If you're working with this project and running into issues, please post in [Issues](/issues).

‎azureproject/production.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
ALLOWED_HOSTS= [os.environ['WEBSITE_HOSTNAME']] if'WEBSITE_HOSTNAME'inos.environelse []
77
CSRF_TRUSTED_ORIGINS= ['https://'+os.environ['WEBSITE_HOSTNAME']] if'WEBSITE_HOSTNAME'inos.environelse []
88

9-
# Configure Postgres database; the full username for PostgreSQL flexible server is
10-
# username (not @sever-name).
9+
# Configure Postgres database based on connection string of the libpq Keyword/Value form
10+
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
11+
conn_str=os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']
12+
conn_str_params={pair.split('=')[0]: pair.split('=')[1] forpairinconn_str.split(' ')}
13+
1114
DATABASE_URI='postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
12-
dbuser=os.environ['DBUSER'],
13-
dbpass=os.environ['DBPASS'],
14-
dbhost=os.environ['DBHOST'] +".postgres.database.azure.com",
15-
dbname=os.environ['DBNAME']
15+
dbuser=conn_str_params['user'],
16+
dbpass=conn_str_params['password'],
17+
dbhost=conn_str_params['host'],
18+
dbname=conn_str_params['dbname']
1619
)

‎infra/resources.bicep‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ resource web 'Microsoft.Web/sites@2022-03-01' ={
9292
serverFarmId: appServicePlan.id
9393
siteConfig:{
9494
alwaysOn: true
95-
linuxFxVersion: 'PYTHON|3.9'
95+
linuxFxVersion: 'PYTHON|3.10'
9696
ftpsState: 'Disabled'
9797
appCommandLine: 'startup.sh'
9898
}
@@ -106,10 +106,7 @@ resource web 'Microsoft.Web/sites@2022-03-01' ={
106106
name: 'appsettings'
107107
properties:{
108108
SCM_DO_BUILD_DURING_DEPLOYMENT: 'true'
109-
DBHOST: postgresServer.name
110-
DBNAME: flaskDatabase.name
111-
DBUSER: postgresServer.properties.administratorLogin
112-
DBPASS: databasePassword
109+
AZURE_POSTGRESQL_CONNECTIONSTRING: 'dbname=${flaskDatabase.name} host=${postgresServer.name}.postgres.database.azure.com port=5432 sslmode=require user=${postgresServer.properties.administratorLogin} password=${databasePassword}'
113110
SECRET_KEY: secretKey
114111
FLASK_DEBUG: 'False'
115112
}

‎models.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def validate_rating(self, key, value):
2929
returnvalue
3030

3131
def__str__(self):
32-
returnf"{self.restaurant.name} ({self.review.date:%x})"
32+
returnf"{self.user_name}: {self.review_date:%x}"

0 commit comments

Comments
(0)