Flask initial settings

Introduction

This section describes the initial settings when you start Flask.

Setting __init__.py

The __init__.py is as follows, although it varies slightly depending on the database and application used.

__init__.py


import os

from flask import Flask

from . import db, auth, views


def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY = 'dev', #For development
        DATABASE = os.path.join(app.instance_path, 'Project name.sqlite'),
    )

    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)
    else:
        app.config.from_mapping(test_config)

    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    
    db.init_app(app)

    app.register_blueprint(auth.bp)
    app.register_blueprint(views.bp)

    app.add_url_rule('/', endpoint='index')

    return app

The SECRET KEY is only set for development for the time being and needs to be changed during deployment. Also, ʻinit_app is a method for initializing the application. I will explain it in a later article. ʻApp.register_blueprint describes ʻauth and views as an example here, but it is necessary to describe each application that sets blueprint. In ʻadd_url_rule, the URL of the index page is specified in this example.

Summary

Here, I explained the initial settings when starting a Flask project. Database and application settings will be covered at a later date.

Recommended Posts

Flask initial settings
Flask application settings
Flask database settings
[Linux] [Initial Settings] System Settings
Flask HTML template settings
[Linux] [Initial Settings] Getting Started
Anaconda 4.4.0 Initial Settings for Windows
[Linux] [Initial Settings] Install & Uninstall
Initial settings from Kubuntu installation
flask
[Linux] [Initial Settings] [Flutter] Summary
flask
Raspbian initial settings (Raspberry Pi 4)
[Linux] [Initial Settings] Backup & Recovery
[Linux] [Initial Settings] Table of Contents
Initial settings for Mac (for data analysts)
WEB application development using Django [Initial settings]
First Flask
samba settings
Flask memo
VIM settings
Create initial settings and staff apps in Django