Application Setup
Flask apps can take many different forms but once they go beyond one or two pages it’s a good idea to structure them properly. As we know the blog will be several different pages with different functionality it will be setup for this from the start. We also will want automated tests so we will add the base for that as well.
"""Flask App Entry."""import contextlibimport os
from flask import Flask
def create_app(test_config=None): """Creation factory for Flask app.""" app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY="dev", DATABASE=os.path.join(app.instance_path, "flaskapp.sqlite"), )
if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile("config.py", silent=True) else: # load the test config if passed in app.config.from_mapping(test_config)
# ensure the instance folder exists with contextlib.suppress(OSError): os.makedirs(app.instance_path)
# a simple page that says hello @app.route("/hello") def hello(): return "Hello, World!"
return appWhat does this all mean?
-
The
create_appfunction is the application factory which means its a function that will create our application (similar to a factory). -
The
app = Flask(__name__, instance_relative_config=True)creates the Flask instance with__name__being the current module name (flaskapp) andinstance_relative_config=Truetells Flask that configuration files are relative to the instance folder. The instance folder is located outside the flaskapp package and can hold local data that shouldn’t be committed to version control, such as configuration secrets and the database file. It also means we don’t have to worry about where certain things are stored which makes testing a lot easier. -
app.config.from_mappingis used for the core config of our app, we have a secret keyDEVand a database location. The secret key is used to keep the client-side sessions secure. The database location is where the database will be stored.
-
As we know that automated tests are going to be used we add the ability for the tests to override some settings so that it can be tested properly and not break our app.
-
with contextlib.suppress(OSError):is used to suppress the error if the instance folder already exists. This is because we don’t want to overwrite anything that is already there. -
To make sure our application works we create a simple route that returns “Hello, World!” that can be accessed at
/hello.
Running our Application
flask --app flaskapp run --debugYou’ll see output similar to this:
* Serving Flask app "flaskapp"* Debug mode: on* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)* Restarting with stat* Debugger is active!* Debugger PIN: nnn-nnn-nnnVisit http://127.0.0.1:5000/hello in a browser and you should see the “Hello, World!” message. Congratulations, you’re now running your Flask web application!
If another program is already using port 5000, you’ll see OSError: [Errno 98] or OSError: [WinError 10013] when the server tries to start. See Address already in use for how to handle that.