Overview
Flask is a great, light-weight web framework for Python. It's a relatively new framework in the world of programming as it's first stable release came out in 2010. It was developed by Armin Ronacher as an April Fool's joke but the idea continued to develop and rose in popularity among web developers. Flask has a built in debugging shell and it supports RESTful routing. SQLAlchemy is a great ORM to use with Flask and this article will give instructions under the assumption that you are using SQLAlchemy for interacting with your database, though any different SQL library or ORM will work fine as well. I'd like to elaborate how to start a Flask application using just a few simple steps:
Step 1.
First, you have to create a new directory and initialize your database:
mkdir flask-app
flask db init
cd server
Most of the work you'll do will be in the server folder
Step 2.
Create a file where you will define your models. The models file will contain all of your models that will act as templates for tables in your database. You can also create validations in the model file and customize how data will be retrieved from your database using serialization methods. An example might be a model for a User:
class User():
__tablename__ = users
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
name = db.Column(db.String)
In a database, the code above would translate to a table called users, with columns and rows of id, email and name data.
Step 3.
Create a main app file where you will define your RESTful routes. In this file you can define classes and methods within those classes that contain your routes, shaping how the data from your database will be retrieved and how it can be accessed. An example from my own group project is a class for real estate properties, and am index route that enables you to retrieve data for all of the properties stored in the database:
class Properties(Resource):
def get(self):
all_props = [prop.to_dict() for prop in
Property.query.all()]
return make_response(all_props, 200)
Step 4.
Create a seed file where you can create some instances of your class so you have some data to work with:
prop = Property(address="838 Mt. Rd", city="Logan", state="UT", image="image2.png", bedrooms=6, bathrooms=3, floors=2, garage=True, pool=False)
db.session.add(prop)
db.commit()
At this point you can instantiate your database with the data from your seed file by running
flask db migrate -m "message"
flask db upgrade
Step 5.
Use the debugger by entering flask shell
in the terminal from within your server folder. This will allow you to actively debug and test your application. You can create a front-end with your preferred framework to make your application full stack.
Important side-notes.
In your seed file, you need to define the context of your app so the data can be seeded correctly by putting with app.app_context():
at the top of your seed file and then creating your instances within that.
In your models file it is helpful to make a reference to whatever ORM your using and store it in a variable called db like so: db = SQLAlchemy()
Within your main app file, define a reference to your app and set up some environment configurations
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.json.compact = False
migrate = Migrate(app, db)
db.init_app(app)
You will also need to import some libraries
from flask import Flask, request, make_response, jsonify, abort
flask_migrate import Migrate
flask_restful import Api, Resource
Top comments (0)