-
-
Save kaniket7209/88da17a33f4df2631be30479c131de0b to your computer and use it in GitHub Desktop.
How to use Flask's app factory Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. What is Flask? | |
Answer: Flask is a light-weight web framework. With Flask, you get to pick-and-choose what components and extension needed for your site. I like this aspect since the modular nature allows you to build everything up, without too much clutter. | |
2. What is app Factory Flask? | |
Answer: The Flask Application Factory refers to a common "pattern" for solving this dilemma. ... For Flask to recognize the data models we have in models.py , our Blueprints, or anything else, Flask needs to be told that these things exist after the app is "created" with app = Flask(__name__) . | |
3. How can we use Flask's app Factory pattern in our code? | |
Answer: Just wrap your code under the following command | |
def create_app(): | |
#Your app.py code | |
return app | |
4. Show the code for a minimal app using Flask Application | |
Answer: A Flask application can be created with the following few lines of code: | |
# app/__init__.py | |
from flask import Flask | |
app = Flask(__name__) | |
from app import views | |
if __name__ == "__main__": | |
app.run() | |
5. How to import the app object and use it . Show with code. | |
Answer: Many other files in the application will need access to the app object created in the __init__.py file, such as registering routes, logging or accessing config values. | |
To do so, we must import it first: | |
# app/views.py | |
from app import app | |
from flask import render_template | |
@app.route("/") | |
def index(): | |
app.logger.debug(app.config.get("ENV")) | |
return render_template("index.html") | |
6. What is the need of the application factory? | |
Answer: As your Flask application grows, you'll often find the need to register blueprints, dynamically load configuration, add request handlers etc.. | |
7. What is the application Factory? | |
Answer: The application factory is a function that wraps the creating of the app object and returns it. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. What is the correct way of returning app while using Flask's app factory Pattern? | |
a) return app() | |
b) return app | |
c) return App | |
d) return App() | |
Answer: b) | |
2. What will happen if we wrapped up the code using app factory pattern but not returned app? | |
a) App will run regardless of return statement | |
b) Will throw an error | |
c) App will open in server but loading occurs | |
d) None of the above | |
Answer: b) | |
3. The application factory is a | |
a) function | |
b) API | |
c) tool | |
d) module | |
Answer: a) | |
4. Which of the following statement from below is not correct | |
a) Flask is a function | |
b) It wraps the creating of the app and returns it | |
c) Dynamically load configuration | |
d) With this Api you can add request handlers | |
Answer: d) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment