Lecture 8.1 (March 8)
- Working on A2 and P3
- Q3 is after spring break
- Make log-in buttons be buttons
- Drop excess cookie nonsense
- Make user available in all templates
sessionis directly available in Flask
- There's also a 'Flask globals' object
flask.gwhere you can stash stuff
Modularity in Python
So far, we have written all code in one big file.
Python lets you split code into separate files, called modules.
We import code from other modules.
By default, our current directory is searched, along with other Python code.
We're going to split our application into several modules:
- This module contains the
appobject, along with other similar globals.
- This module contains our
@app.routeURL handlers. It imports from
from init import app
- A main module that pulls everything together:
And, of course,
We can create other modules too, however we want to organize our code. Just a couple of rules:
- The myapp module needs to import every module that defines URL handlers, to make sure that they all get defined.
- To make
manage.pywork, it needs to also expose the
PyCharm will complain about unused imports; this is fine.
If you read the Flask documentation on large applications, they suggest a somewhat different layout. Their layout breaks a common Python rule (with decent reason), but also requires more advanced understanding of the Python module system to understand, and does funny things to file paths.
We're now going to start using databases.
To do this, we are going to use SQLAlchemy and its Flask bridge. This allows us to write Python code to do basic (and many advanced) database operations.
First, we need to install it:
pip install flask-sqlalchemy
Then, we need to initialize SQLAlchemy in
from flask_sqlalchemy import SQLAlchemy # … other init code db = SQLAlchemy(app)
We now can define the kinds of data we want to store. We will create a module,
models, that contains our database models.
If you've heard of model-view-controller (MVC) architecture, this is it! The data is our model, the URL handlers are views, and effectively Flask is our controller.
In order to set up our data, we need to specify the different kinds of data we need to track, such as animals.
We define each kind of data as a Python class. It will then be used to create a database table.
class Animal(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50)) location = db.Column(db.String(50))
This defines a data type
Animal with three fields: an id (our objects should generally always have one of these), a name, and an animal.
There are a few rules:
- Always have an
idcolumn that is a primary key, to identify your objects.
- No lists - just numbers, strings, etc.
We'll see in a bit how to link data types together.
Now we will update the Flask app to query data from us:
- Get animals by calling
- Animals now have IDs, not just numbers — fix
- Get an animal with
- Search animals with
We can add:
Create an object
animal = Animal() animal.name = name animal.location = home
animal.idhas the animal ID
We can modify data too — just modify the object you got, then commit.
Work through how to create users and use