Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from Cruft #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .cruft.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"template": "https://github.com/kjaymiller/cookiecutter-relecloud/",
"commit": "e7c6d743ed8bd795bc2fd8befda01b7980933dbb",
"template": "https://github.com/Azure-Samples/Azure-Python-Standardization-Template-Generator",
"commit": "0178d866789d5eef0d9b6769673640cbada8b3b9",
"checkout": null,
"context": {
"cookiecutter": {
Expand All @@ -11,7 +11,7 @@
"project_host": "aca",
"web_port": "8000",
"__repo_name": "azure-flask-postgres-addon-aca",
"__src_folder_name": "azure_flask_postgres_addon_aca",
"__src_folder_name": "azure-flask-postgres-addon-aca",
"__project_short_description": "Create a relecloud demo application with flask and postgres-addon",
"_copy_without_render": [
".github/workflows/azure-dev.yml",
Expand All @@ -26,7 +26,7 @@
"lstrip_blocks": true,
"trim_blocks": true
},
"_template": "https://github.com/kjaymiller/cookiecutter-relecloud/"
"_template": "https://github.com/Azure-Samples/Azure-Python-Standardization-Template-Generator"
}
},
"directory": null
Expand Down
5 changes: 3 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
"name": "azure_flask_postgres_addon_aca",
"name": "azure-flask-postgres-addon-aca",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
Expand Down Expand Up @@ -32,7 +32,8 @@
"files.exclude": {
".coverage": true,
".pytest_cache": true,
"__pycache__": true
"__pycache__": true,
".ruff_cache": true
},
"[python]": {
"editor.formatOnSave": true,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/azure-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ jobs:
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-dev.txt
python3 -m playwright install --with-deps
python3 -m playwright install chromium --with-deps
python3 -m pytest --exitfirst src/tests/smoke/smoketests.py --live-server-url $URI
83 changes: 51 additions & 32 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,57 @@ jobs:
strategy:
fail-fast: false
matrix:
os: ["ubuntu-20.04"]
os: ["ubuntu-latest", "macos-latest", "macos-latest-xlarge", "windows-latest"]
python_version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
services:
db:
image: postgres:14
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
exclude:
- os: macos-latest-xlarge
python_version: 3.8
- os: macos-latest-xlarge
python_version: 3.9
- os: macos-latest-xlarge
python_version: "3.10"
steps:
- uses: actions/checkout@v3
- name: Setup python
uses: actions/setup-python@v2
with:
- name: Checkout
uses: actions/checkout@v3
- name: Check for MacOS Runner
if: matrix.os == 'macos-latest-xlarge'
run: brew install postgresql@14
- name: Setup postgres
uses: ikalnytskyi/action-setup-postgres@v4
- name: Setup python
uses: actions/setup-python@v2
with:

python-version: ${{ matrix.python_version }}
architecture: x64
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-dev.txt
playwright install --with-deps
python3 -m pip install -e src
- name: Seed data and run Pytest tests
run: |
python3 -m flask --app src.flaskapp db upgrade --directory src/flaskapp/migrations
python3 -m flask --app src.flaskapp seed --filename src/seed_data.json
python3 -m pytest
env:
POSTGRES_HOST: localhost
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASE: postgres
python-version: ${{ matrix.python_version }}
architecture: x64
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-dev.txt
playwright install chromium --with-deps
python3 -m pip install -e src
- name: Seed data
run: |
python3 -m flask --app src.flaskapp db upgrade --directory src/flaskapp/migrations
python3 -m flask --app src.flaskapp seed --filename src/seed_data.json
env:
POSTGRES_HOST: localhost
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASE: postgres
- name: Run tests Windows
if: runner.os == 'windows'
run: python3 -m pytest --ignore=src/tests/local/test_gunicorn.py
env:
POSTGRES_HOST: localhost
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASE: postgres
- name: Run tests
if: runner.os != 'windows'
run: python3 -m pytest
env:
POSTGRES_HOST: localhost
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DATABASE: postgres
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
],
"jinja": true,
"justMyCode": false
},
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"env": {"PYTEST_ADDOPTS": "--no-cov"}
}
]
}
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
---
page_type: sample
languages:
- azdeveloper
- python
- bicep
- html
- css
- scss
products:
- azure
- azure-container-apps
- azure-postgresql
urlFragment: azure-flask-postgres-addon-aca
name: Deploy Flask Application with PostgreSQL on Azure Container Apps (Python)
description: This project deploys a web application for a space travel agency using Flask with Python, and is set up for easy deployment with the Azure Developer CLI.
---
<!-- YAML front-matter schema: https://review.learn.microsoft.com/en-us/help/contribute/samples/process/onboarding?branch=main#supported-metadata-fields-for-readmemd -->

# Deploy Flask Application with PostgreSQL via Azure Container Apps

This project deploys a web application for a space travel agency using Flask. The application can be deployed to Azure with Azure Container Apps using the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview).
Expand Down Expand Up @@ -46,7 +65,7 @@ export POSTGRES_PASSWORD=<YOUR PASSWORD>
If you're running the app inside VS Code or GitHub Codespaces, you can use the "Run and Debug" button to start the app.

```sh
python3 -m flask --app src.flaskapp run --reload --port=8000
python3 -m flask --app src.flaskapp run --debug --reload --port=8000
```


Expand All @@ -56,7 +75,7 @@ python3 -m flask --app src.flaskapp run --reload --port=8000

```sh
python3 -m pip install -r requirements-dev.txt
python3 -m playwright install --with-deps
python3 -m playwright install chromium --with-deps
```

3. Run the tests:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ known-first-party = ["flaskapp"]

[tool.pytest.ini_options]
addopts = "-ra -vv"

[tool.coverage.report]
show_missing = true
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pip-tools
pytest
ephemeral-port-reserve
pytest-playwright
coverage
pytest-cov
axe-playwright-python

# Linters
ruff
2 changes: 0 additions & 2 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get update && apt-get install -y xdg-utils \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip

WORKDIR /demo-code

COPY requirements.txt .
Expand Down
13 changes: 5 additions & 8 deletions src/flaskapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os

import click
from azure.monitor.opentelemetry import configure_azure_monitor
from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
from opencensus.trace.samplers import ProbabilitySampler
from sqlalchemy.orm import DeclarativeBase


Expand All @@ -17,8 +15,12 @@ class BaseModel(DeclarativeBase):
db = SQLAlchemy(model_class=BaseModel)
migrate = Migrate()


def create_app(test_config=None):
# create and configure the app
if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
configure_azure_monitor()

app = Flask(__name__, static_folder="../static", template_folder="../templates")

# Load configuration for prod vs. dev
Expand All @@ -27,11 +29,6 @@ def create_app(test_config=None):
app.config.from_object("flaskapp.config.development")
else:
app.config.from_object("flaskapp.config.production")
FlaskMiddleware(
app,
exporter=AzureExporter(connection_string=os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING", None)),
sampler=ProbabilitySampler(rate=1.0),
)

# Configure the database
if test_config is not None:
Expand Down
4 changes: 2 additions & 2 deletions src/flaskapp/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def cruise_detail(pk: int):
)


@bp.get("/info_request/")
@bp.get("/info_request")
def info_request():
all_cruises = db.session.execute(db.select(models.Cruise)).scalars().all()

return render_template("info_request_create.html", cruises=all_cruises, message=request.args.get("message"))


@bp.post("/info_request/")
@bp.post("/info_request")
def create_info_request():
name = request.form["name"]
db_info_request = models.InfoRequest(
Expand Down
3 changes: 1 addition & 2 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ name = "flaskapp"
version = "1.0.0"
description = "Create a relecloud demo application with flask and postgres-addon"
dependencies = [
"azure-monitor-opentelemetry",
"Flask==2.3.2",
"opencensus-ext-azure==1.1.9",
"opencensus-ext-flask==0.8.2",
"SQLAlchemy==2.0.17",
"psycopg2-binary==2.9.6",
"Flask-Migrate==4.0.4",
Expand Down
30 changes: 30 additions & 0 deletions src/static/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "ReleCloud Space Tourism",
"short_name": "ReleCloud",
"start_url": ".",
"display": "standalone",
"background_color": "#fff",
"description": "ReleCloud Space Tourism",
"icons": [
{
"src": "/static/res/img/favicon.ico",
"sizes": "32x32",
"type": "image/x-icon"
},
{
"src": "/static/res/img/favicon-120-precomposed.png",
"sizes": "120x120",
"type": "image/png"
},
{
"src": "/static/res/img/favicon-152-precomposed.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "/static/res/img/favicon-192.png",
"sizes": "192x192",
"type": "image/png"
}
]
}
17 changes: 11 additions & 6 deletions src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
{% if prod %}
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
{% endif %}

<!--main CSS theme file -->
<link rel="stylesheet" href="{{ url_for('static', filename='res/css/theme.css') }}" />

<style>
/* Accessibility overrides */
.btn-primary {
background-color: #006ee5;
}
</style>
<!-- For new browsers - multisize ico -->
<link rel="icon" type="image/x-icon" sizes="16x16 32x32" href="{{ url_for('static', filename='res/img/favicon.ico') }}">

<!-- For iPad with high-resolution Retina display running iOS ≥ 7: -->
<link rel="apple-touch-icon" sizes="152x152" href="{{ url_for('static', filename='res/img/favicon-152-precomposed.png') }}">

<!-- For iPhone with high-resolution Retina display running iOS ≥ 7: -->
<link rel="apple-touch-icon" sizes="120x120" href="{{ url_for('static', filename='res/img/favicon-120-precomposed.png') }}">

<!-- Chrome for Android -->
<link rel="manifest" href="manifest.json">
<link rel="manifest" href="{{ url_for('static', filename='manifest.json') }}">
<link rel="icon" sizes="192x192" href="{{ url_for('static', filename='res/img/favicon-192.png') }}">

<title>
Expand Down
4 changes: 2 additions & 2 deletions src/templates/info_request_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ <h1 id="page-title">Request information</h2>
<p>Fill out the form below to request information about our cruises</p>

<form method="post">
<form method="post" action="/info_request/">
<form method="post" action="/info_request">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="cruise_id">Cruise:</label>
<select name="cruise_id">
<select name="cruise_id" id="cruise_id">
{% for cruise in cruises %}
<option value="{{ cruise.id }}">{{ cruise.name }}</option>
{% endfor %}
Expand Down
4 changes: 3 additions & 1 deletion src/tests/local/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import multiprocessing
import os
import pathlib
import sys

import ephemeral_port_reserve
import pytest
Expand All @@ -9,7 +10,8 @@
from flaskapp import create_app, db, seeder

# Set start method to "fork" to avoid issues with pickling on OSes that default to "spawn"
multiprocessing.set_start_method("fork")
if sys.platform != "win32":
multiprocessing.set_start_method("fork")


def run_server(app: Flask, port: int):
Expand Down
Loading
Loading