Python Cloud Advocate at Microsoft
Formerly: UC Berkeley, Coursera, Khan Academy, Google
Find Pamela online at:
Mastodon | @pamelafox@fosstodon.org |
@pamelafox | |
GitHub | www.github.com/pamelafox |
Website | pamelafox.org |
To follow along with the exercises, you can do either:
Web apps use databases to store data that needs to be shared across multiple users or computers.
Two main types of databases:
A non-relational database stores data in a very flexible way, often as JSON documents or as key-value stores.
Example document in a MongoDB collection:
{
"username": "Pamela Fox",
"email": "pamelafox@microsoft.com"
}
Example key-value pair in a Redis store:
usersession1234: { "username": "Pamela Fox", "email": "pamelafox@microsoft.com" }
A relational database contains tables.
Each table has columns and rows.
Example table called speakers
:
id | name | title |
---|---|---|
1 | Pamela Fox | Python Cloud Advocate |
2 | Renee Noble | Python Cloud Advocate |
3 | Victor Vazquez | Software Developer |
4 | Dawn Wages | Python Community Advocate |
SQL (Structured Query Language) is a language for querying and modifying relational databases.
SQL commands:
SELECT
- get data
INSERT
- add data
UPDATE
- modify data
DELETE
- remove data
Learn SQL for free with my course on Khan Academy: khanacademy.org/computing/computer-programming/sql
PostgreSQL is a popular open-source relational database that supports JSON, XML, and other data types.
CREATE TABLE cities (
name varchar(80),
location point
);
INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
SELECT name FROM cities WHERE location <@ circle '((0,0), 300)';
There are many popular extensions for PostgreSQL like PostGIS for geospatial data and pgvector for vector similarity search.
Python libraries for PostgreSQL: psycopg (driver), SQLAlchemy (ORM)
Playgrounds: PostgreSQL playground, pgvector playground
Using this repo:
github.com/pamelafox/postgresql-playground
aka.ms/postgres-playground
.env.devcontainer
into a .env
file
CREATE TABLE restaurants (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);
INSERT INTO restaurants (name) VALUES ('Casa Latina');
SELECT * FROM restaurants;
One option is to call SQL directly from a database driver.
import psycopg2
conn = psycopg2.connect(
host=os.environ["PGHOST"],
database=os.environ["PGDATABASE"],
user=os.environ["PGUSSER"],
password=os.environ["PGPASS"],
)
cur = conn.cursor()
cur.execute("CREATE TABLE restaurants (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL)")
conn.commit()
cur.close()
⚠️ Executing raw SQL 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 safer 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 Restaurant(Base):
__tablename__ = "restaurants"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
rating: Mapped[int]
query = select(Restaurant).where(Restaurant.title == "Casa Latina")
results = session.execute(query)
Get by primary key:
session.get(Restaurant, 42)
Get all rows in table:
session.execute(select(Restaurant)).scalars().all()
Finding rows by column value:
session.execute(select(Restaurant).where(
Restaurant.name == "Casa Latina")).scalars().all()
Play with queries in
PostgreSQL Playground
aka.ms/postgres-playground
Flask is a lightweight web framework for Python, which does not come with any built-in database functionality.
A common way to use Flask with SQLAlchemy:
github.com/Azure-Samples/azure-flask-postgres-flexible-appservice
aka.ms/flask-pg-app
Run app server:
python3 -m flask --app src.flaskapp run --debug --reload --port=8000
Migrations are a way to keep track of changes to the database schema.
Using Flask-Migrate:
python3 -m flask --app src.flaskapp db migrate --directory src/flaskapp/migrations
Using Flask-Migrate:
python3 -m flask --app src.flaskapp db upgrade --directory src/flaskapp/migrations
Using this repo:
github.com/Azure-Samples/azure-flask-postgres-flexible-appservice
aka.ms/flask-pg-app
Also check out the Flask tutorial.
Vector search is a way to find similar items in a database, and is used in recommendation systems, search engines, and chatbots.
SQL:
SELECT * FROM items ORDER BY embedding <=> '[3,1,2]' LIMIT 5;
SQLAlchemy:
closest = session.scalars(
select(Item).order_by(Item.embedding.cosine_distance(target_embedding)).limit(5)
)
for item in closest:
print(item.title)
Try it in: github.com/pamelafox/pgvector-playground
We can use LLMs to answer questions about PostgreSQL tables.
Example:
Azure-Samples/rag-postgres-openai-python
aka.ms/rag-postgres
That example uses FastAPI, but it could also use Flask/Quart.
Recorded talks:
Upcoming Posette talks:
Consider:
Option | Description |
---|---|
Azure Database for PostgreSQL – Single Server | Microsoft's original offering. No longer recommended for new apps. |
Azure Database for PostgreSQL – Flexible Server | Microsoft's most recent PostgreSQL offering. Fully managed service with vertical scaling. |
Azure Cosmos DB for PostgreSQL | Distributed database using PostgreSQL and the Citus extension. Can scale horizontally. |
Azure Container Apps PostgresQL service | A sidecar service that runs alongside your containerized app. |
Cosmos DB for PostgreSQL vs. Flex Server 🔗 aka.ms/flex-vs-cosmos
Using this repo:
github.com/Azure-Samples/azure-postgres-pgvector-python
aka.ms/azure-postgres-pgvector
The web app can be hosted on the same server as the database, or on a separate server.
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, App Service is an easy way to get started.
Your app may need more than just a web server and a database.
Networking | DNS Zone, Virtual Network, VPN Gateway, ... |
---|---|
Security | Key Vault, Security Center, ... |
Storage | Blob Storage, Files, Archive Storage, ... |
Caching | CDN, Front Door, ... |
Machine Learning | Translator, Bot Service, Computer Vision, ... |
...and more! |
Using the Azure Dev CLI:
azd up
Using this repo:
github.com/pamelafox/flask-db-quiz-example
aka.ms/flask-db-quiz
azd down
to un-deploy the app (so that you don't waste cloud resources unnecessarily).
When connecting to a database from a web app, consider...
Restricting authentication:
Restricting network access: