Flask Flashcards
What’s ‘create_app’?
create_app is the application factory function
What’s the point of __name__ in the following:
app = Flask(__name__, instance_relative_config=True)
_name__ is the name of the current Python module. The app needs to know where it’s located to set up some paths, and __name__ is a convenient way to tell it that.
What’s the point of instance_relative_config=True in the following:
app = Flask(__name__, instance_relative_config=True)
instance_relative_config=True tells the app that configuration files are relative to the instance folder. The instance folder is located outside the flaskr package and can hold local data that shouldn’t be committed to version control, such as configuration secrets and the database file
What’s the app.config.from_mapping() do?
sets some default configuration that the app will use
What’s the arguments for app.config.from_mapping()?
SECRET_KEY is used by Flask and extensions to keep data safe. It’s set to ‘dev’ to provide a convenient value during development, but it should be overridden with a random value when deploying.
DATABASE is the path where the SQLite database file will be saved. It’s under app.instance_path, which is the path that Flask has chosen for the instance folder. You’ll learn more about the database in the next section.
what’s app.config.from_pyfile()?
overrides the default configuration with values taken from the config.py file in the instance folder if it exists. For example, when deploying, this can be used to set a real SECRET_KEY.
what does the test_config argument do for app.config.from_pyfile()?
test_config can also be passed to the factory, and will be used instead of the instance configuration. This is so the tests you’ll write later in the tutorial can be configured independently of any development values you have configured.
what the purpose of os.makedirs() within a flask app?
ensures that app.instance_path exists. Flask doesn’t create the instance folder automatically, but it needs to be created because your project will create the SQLite database file there.