gasiltaxi.blogg.se

Flask blueprint example
Flask blueprint example













flask blueprint example
  1. #Flask blueprint example software#
  2. #Flask blueprint example code#

If FLASK_ENV environment variable is not set the Flask application will run in the debug mode ( i.e app.debug = True ). The _init_.py creates the application instance and initializes extensions. login_view = 'login' # import views from. get ( 'FLASK_ENV' ) or 'config.DevelopementConfig' ) # initializes extensions db = SQLAlchemy ( app ) mail = Mail ( app ) migrate = Migrate ( app, db ) login_manager = LoginManager ( app ) login_manager. To read configurations from a class use from_object() method as follows:įrom flask import Flask from flask_migrate import Migrate, MigrateCommand from flask_mail import Mail, Message from flask_sqlalchemy import SQLAlchemy from flask_script import Manager, Command, Shell from flask_login import LoginManager import os, config # create application instance app = Flask ( _name_ ) app. This method is particularly useful when you have some sensitive data that you don't want to hardcode in the application itself. We are also providing default values in case environment variables are not set. get ( 'PRODUCTION_DATABASE_URI' ) or that for the first time we are reading values of some configurations from the environment variables. get ( 'TESTING_DATABASE_URI' ) or class ProductionConfig ( BaseConfig ): DEBUG = False SQLALCHEMY_DATABASE_URI = os. get ( 'DEVELOPMENT_DATABASE_URI' ) or class TestingConfig ( BaseConfig ): DEBUG = True SQLALCHEMY_DATABASE_URI = os. get ( 'MAIL_PASSWORD' ) or 'password' MAIL_DEFAULT_SENDER = MAIL_USERNAME class DevelopementConfig ( BaseConfig ): DEBUG = True SQLALCHEMY_DATABASE_URI = os. get ( 'MAIL_USERNAME' ) or MAIL_PASSWORD = os. get ( 'SECRET_KEY' ) or 'A SECRET KEY' SQLALCHEMY_TRACK_MODIFICATIONS = False # Flask-Mail configurations # MAIL_SERVER = '' MAIL_PORT = 587 MAIL_USE_TLS = True MAIL_USERNAME = os.

flask blueprint example

dirname ( _file_ )) class BaseConfig : SECRET_KEY = os.

#Flask blueprint example code#

The environment-specific classes can override or add environment-specific configurations.Ĭreate a new file named config.py inside flask_app directory and add the following code in it: Start off by defining default configurations in the base class and then create environment-specific classes which inherit from the base class. We can implement such a configuration system using classes. You will also notice that no matter which environment you are in some configurations always remains the same.

#Flask blueprint example software#

Class-Based Configurations #Ī software project usually runs in three different environments:Īs the project evolves you will encounter a need to specify different configuration options for different environment. In the rest of the lesson, we will convert our project to conform to this directory structure. The entry point for your Flask application. The config.py contains settings and configuration for your Flask application. The views.py contains routes and view functions. The templates directory contains templates.

flask blueprint example

The static directory contains the static files of the project. The _init_.py tells the Python that app directory is a Python package. The app directory is a Python package which holds views, templates, and static files. The app_dir is the root directory of your Flask project.















Flask blueprint example