Classroom "rules":
Tell us about yourself:
Online development: Github account
Local development:
Local development (option 2):
A web app with a Python backend must handle requests to different URLs and return different responses.
An example Flask app:
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def index():
return render_template("hello.html",
name=request.args.get("name"))
Routes may return different responses based off query parameters, path parameters, or headers. Routes can use templates to generate HTML.
Web apps store data in databases that needs to be shared across multiple users/computers.
A database contains tables.
Each table has columns and rows.
Example table called organizers
:
id | name | title |
---|---|---|
1 | Katie Brydon | GDI Community Director |
2 | Katie Franco | GDI Executive Director |
3 | Jeseekia Vaughn | Subject Matter Expert |
4 | Alex Vaughn | Coordinator |
Web apps need to be able to access and modify data in databases.
One option is to use SQL directly in the web app code.
import sqlite3
conn = sqlite3.connect("my_database.db")
cursor = conn.cursor()
cursor.execute("SELECT name, title FROM organizers")
results = cursor.fetchall()
⚠️ Executing raw SQL queries makes your app vulnerable to SQL injection attacks.
What you expect:
user_input = "Bobby"
query = "SELECT name, id FROM users where name = " + user_input
What a hacker could do:
user_input = "Bobby); DROP TABLE students;"
query = "SELECT name, title id users where name = " + user_input
From: xkcd.com/327
A better approach is to use an ORM (Object-Relational Mapper) to interact with the database.
An ORM represents table rows as Python objects, and provides methods for querying and modifying data.
A SQLAlchemy example:
class Organizer(Base):
__tablename__ = "organizers"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
title: Mapped[str]
query = select(User).where(User.title == "Coordinator")
results = session.execute(query)
Get by primary key:
session.get(User, 42)
Get all rows in table:
session.execute(select(User)).scalars().all()
Finding rows by column value:
session.execute(select(User).where(
User.title == "Coordinator")).scalars().all()
Play with queries in:
PostgreSQL SQLAlchemy Playground
github.com/Azure-Samples/azure-flask-postgres-flexible-appservice
Run database migrations and server:
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 gunicorn 'src.flaskapp:create_app()' --reload
Migrations are a way to keep track of changes to the database schema.
Using Flask-Migrate:
python3 -m flask --app src.flaskapp db migrate
Using Flask-Migrate:
python3 -m flask --app src.flaskapp db upgrade
Using this repo:
github.com/Azure-Samples/azure-flask-postgres-flexible-appservice
🙋🏼♀️🙋🏾♀️🙋🏽♀️ Let us know if you need any help! 🙋🏻♀️🙋🏽♂️🙋🏿♀️
Also check out the Flask tutorial.
Django, an external library, is a fairly "opinionated" framework for server-side code. Includes an ORM for database interaction.
Apps written in Django:
github.com/Azure-Samples/azure-django-postgres-flexible-appservice
Important server files:
models.py
, urls.py
, views.py
, admin.py
Run DB migrations and server:
python3 src/manage.py migrate
python3 src/manage.py loaddata src/seed_data.json
python3 src/manage.py collectstatic
python3 -m gunicorn project.wsgi:application --reload --pythonpath src
Django includes a built-in admin interface for managing data.
python3 src/manage.py createsuperuser
Using this repo:
github.com/Azure-Samples/azure-django-postgres-flexible-appservice
🙋🏼♀️🙋🏾♀️🙋🏽♀️ Let us know if you need any help! 🙋🏻♀️🙋🏽♂️🙋🏿♀️
Also check out the Django tutorial.
When your website is hosted on a server, it means other users on the Internet can access it.
Many possible hosts:
Consider:
Azure Container Apps | Azure Functions | |||
Azure Kubernetes Service | Container Management | Azure App Service | Serverless | |
Environment | Containers | PaaS | ||
Cloud | Azure |
For Flask/Django, App Service is easiest way to get started.
Databases | PostGreSQL, MySQL, CosmosDB, ... |
---|---|
Storage | Blob Storage, Files, Archive Storage, ... |
Networking | DNS Zone, Virtual Network, VPN Gateway, ... |
Caching | CDN, Front Door, ... |
Security | Key Vault, Security Center, ... |
Machine Learning | Translator, Bot Service, Computer Vision, ... |
...and more! |
Using the Azure Dev CLI:
azd up
azd up
. If prompted, login to your Azure account.
azd down
to un-deploy the app (so that you don't waste cloud resources unnecessarily).