Flask blueprint how to#
The FLASK_APP environment variable instructs Flask on how to load the app. In a terminal, you can set the FLASK_APP and FLASK_DEBUG values:
Flask blueprint update#
You will revisit this code later and update it with the desired functionality. You will also have routes for handling the POST requests from login and signup. route ( '/logout' ) def logout ( ) : return 'Logout'įor the time being, define login, signup, and logout with text returns. route ( '/signup' ) def signup ( ) : return 'Signup'. route ( '/login' ) def login ( ) : return 'Login'. Project/auth.py from flask import BlueprintĪuth = Blueprint ( 'auth', _name_ ). You will need to initialize SQLAlchemy, set some configuration values, and register the blueprints here:
At the moment, this will not do much, but it will be needed for the rest of the app. This file will have the function to create the app, which will initialize the database and register the blueprints. In a real app, you can break down the functionality in any way you like, but the solution covered here will work well for this tutorial. Another blueprint handles everything auth-related. One blueprint handles the regular routes, which include the index and the protected profile page. This app will use the Flask app factory pattern with blueprints. The first file will be the _init_.py file for the project: Let’s start by creating a project directory: Now that you have installed the packages, you are ready to create the main app file.
Flask blueprint install#
Run the following command from your virtual environment to install the needed packages: This command will activate the virtual environment. This will create a new directory containing bin, include, and lib subdirectories.
This command will execute the module venv to create a new virtual environment named auth. Note: You can consult the tutorial relevant to your local environment for setting up venv.ĭepending on how Python was installed on your machine, your command will look similar to: You will want to create a Python environment if you don’t have one. You will be using SQLite to avoid having to install any extra dependencies for the database.įirst, start with creating the project directory: Flask-SQLAlchemy: to represent the user model and interface with the database.Flask-Login: to handle the user sessions after authentication.There are three main packages you need for your project: This tutorial was verified with sqlite3 v3.36.0, python v3.9.8, flask v2.0.2, flask-login v0.5.0, and flask-sqlachemy v2.5.1.
├── base.html # contains common layout and linksĪs you progress through the tutorial, you will create these directories and files. ├── main.py # the non-auth routes for the app ├── auth.py # the auth routes for the app Here is a diagram to provide a sense of what the file structure of the project will look like once you have completed the tutorial.