11/30/2021

Flask Python

Flask-SQLAlchemy is fun to use, incredibly easy for basic applications, andreadily extends for larger applications. For the complete guide, checkoutthe API documentation on the SQLAlchemy class.

To run https functionality or SSL authentication in flask application you first install 'pyOpenSSL' python package. Next step is to create 'cert.pem' and 'key.pem' using following command on terminal: openssl req -x509 -newkey rsa:4096. Copy generated 'cert.pem' and 'kem.pem' in you flask.

Example
  1. Welcome to Flask¶ Welcome to Flask’s documentation. Get started with Installation and then get an overview with the Quickstart. There is also a more detailed Tutorial that shows how to create a small but complete application with Flask. Common patterns are described in the Patterns for Flask section.
  2. Flask is one of the web development frameworks written in Python. Through flask, a loop can be run in the HTML code using jinja template and automatically HTML code can be generated using this. The code will be stored in Directories in the format of Flask. So we will be making two directories, static – For static Files like images, css, js.

Install Flask Python

Python

A Minimal Application¶

For the common case of having one Flask application all you have to do isto create your Flask application, load the configuration of choice andthen create the SQLAlchemy object by passing it the application.

Once created, that object then contains all the functions and helpersfrom both sqlalchemy and sqlalchemy.orm. Furthermore itprovides a class called Model that is a declarative base which can beused to declare models:

To create the initial database, just import the db object from aninteractive Python shell and run theSQLAlchemy.create_all() method to create thetables and database:

Flask python rest

Boom, and there is your database. Now to create some users:

But they are not yet in the database, so let’s make sure they are:

Accessing the data in database is easy as a pie:

Note how we never defined a __init__ method on the User class?That’s because SQLAlchemy adds an implicit constructor to all modelclasses which accepts keyword arguments for all its columns andrelationships. If you decide to override the constructor for anyreason, make sure to keep accepting **kwargs and call the superconstructor with those **kwargs to preserve this behavior:

Simple Relationships¶

SQLAlchemy connects to relational databases and what relational databasesare really good at are relations. As such, we shall have an example of anapplication that uses two tables that have a relationship to each other:

First let’s create some objects:

As you can see, there is no need to add the Post objects to thesession. Since the Category is part of the session all objectsassociated with it through relationships will be added too. It doesnot matter whether db.session.add()is called before or after creating these objects. The association canalso be done on either side of the relationship - so a post can becreated with a category or it can be added to the list of posts ofthe category.

Let’s look at the posts. Accessing them will load them from the databasesince the relationship is lazy-loaded, but you will probably not noticethe difference - loading a list is quite fast:

While lazy-loading a relationship is fast, it can easily become a majorbottleneck when you end up triggering extra queries in a loop for morethan a few objects. For this case, SQLAlchemy lets you override theloading strategy on the query level. If you wanted a single query toload all categories and their posts, you could do it like this:

Flask Python

Flask Python 2

If you want to get a query object for that relationship, you can do sousing with_parent(). Let’s excludethat post about Snakes for example:

Flask Python Projects

Road to Enlightenment¶

The only things you need to know compared to plain SQLAlchemy are:

  1. SQLAlchemy gives you access to the following things:

    • all the functions and classes from sqlalchemy andsqlalchemy.orm

    • a preconfigured scoped session called session

    • the metadata

    • the engine

    • a SQLAlchemy.create_all() and SQLAlchemy.drop_all()methods to create and drop tables according to the models.

    • a Model baseclass that is a configured declarative base.

  2. The Model declarative base class behaves like a regularPython class but has a query attribute attached that can be used toquery the model. (Model and BaseQuery)

  3. You have to commit the session, but you don’t have to remove it atthe end of the request, Flask-SQLAlchemy does that for you.