Skip to content

Commit fadc357

Browse files
docs: add README code sample for Flask-SQLAlchemy and FastAPI (#432)
1 parent 0a7a8d2 commit fadc357

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,98 @@ connector.connect(
290290
)
291291
```
292292

293+
### Using the Python Connector with Python Web Frameworks
294+
The Python Connector can be used alongside popular Python web frameworks such
295+
as Flask, FastAPI, etc, to integrate Cloud SQL databases within your
296+
web applications.
297+
298+
#### Flask-SQLAlchemy
299+
[Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/)
300+
is an extension for [Flask](https://flask.palletsprojects.com/en/2.2.x/)
301+
that adds support for [SQLAlchemy](https://www.sqlalchemy.org/) to your
302+
application. It aims to simplify using SQLAlchemy with Flask by providing
303+
useful defaults and extra helpers that make it easier to accomplish
304+
common tasks.
305+
306+
You can configure Flask-SQLAlchemy to connect to a Cloud SQL database from
307+
your web application through the following:
308+
309+
```python
310+
from flask import Flask
311+
from flask_sqlalchemy import SQLAlchemy
312+
from google.cloud.sql.connector import Connector, IPTypes
313+
314+
315+
# Python Connector database connection function
316+
def getconn():
317+
with Connector() as connector:
318+
conn = connector.connect(
319+
"project:region:instance-name", # Cloud SQL Instance Connection Name
320+
"pg8000",
321+
user="my-user",
322+
password="my-password",
323+
db="my-database",
324+
ip_type= IPTypes.PUBLIC # IPTypes.PRIVATE for private IP
325+
)
326+
return conn
327+
328+
329+
app = Flask(__name__)
330+
331+
# configure Flask-SQLAlchemy to use Python Connector
332+
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql+pg8000://"
333+
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
334+
"creator": getconn
335+
}
336+
337+
db = SQLAlchemy(app)
338+
```
339+
340+
For more details on how to use Flask-SQLAlchemy, check out the
341+
[Flask-SQLAlchemy Quickstarts](https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/#)
342+
343+
#### FastAPI
344+
[FastAPI](https://fastapi.tiangolo.com/) is a modern, fast (high-performance),
345+
web framework for building APIs with Python based on standard Python type hints.
346+
347+
You can configure FastAPI to connect to a Cloud SQL database from
348+
your web application using [SQLAlchemy ORM](https://docs.sqlalchemy.org/en/14/orm/)
349+
through the following:
350+
351+
```python
352+
from sqlalchemy import create_engine
353+
from sqlalchemy.ext.declarative import declarative_base
354+
from sqlalchemy.orm import sessionmaker
355+
from google.cloud.sql.connector import Connector, IPTypes
356+
357+
# Python Connector database connection function
358+
def getconn():
359+
with Connector() as connector:
360+
conn = connector.connect(
361+
"project:region:instance-name", # Cloud SQL Instance Connection Name
362+
"pg8000",
363+
user="my-user",
364+
password="my-password",
365+
db="my-database",
366+
ip_type= IPTypes.PUBLIC # IPTypes.PRIVATE for private IP
367+
)
368+
return conn
369+
370+
SQLALCHEMY_DATABASE_URL = "postgresql+pg8000://"
371+
372+
engine = create_engine(
373+
SQLALCHEMY_DATABASE_URL , creator=getconn
374+
)
375+
376+
# create SQLAlchemy ORM session
377+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
378+
379+
Base = declarative_base()
380+
```
381+
382+
To learn more about integrating a database into your FastAPI application,
383+
follow along the [FastAPI SQL Database guide](https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-database-models).
384+
293385
### Async Driver Usage
294386
The Cloud SQL Connector is compatible with
295387
[asyncio](https://docs.python.org/3/library/asyncio.html) to improve the speed

0 commit comments

Comments
 (0)