The beginning of our project
Finally, we can get to our first Flask project. In order to have a complex project at the end of the book, we will need a simple Flask project to start us off.
In the file named config.py
, add the following:
class Config(object): pass class ProdConfig(Config): pass class DevConfig(Config): DEBUG = True
Now, in another file named main.py
, add the following:
from flask import Flask from config import DevConfig app = Flask(__name__) app.config.from_object(DevConfig) @app.route('/') def home(): return '<h1>Hello World!</h1>' if __name__ == '__main__': app.run()
For anyone who is familiar with the base Flask API, this program is very basic. It will just show Hello World!
on the browser if we navigate to http://127.0.0.1:5000/
. One point that may be unfamiliar to Flask users is config.from_object
, rather than app.config['DEBUG']
. We use from_object
because in future, multiple configurations will be used, and manually changing every variable when we need to switch between configurations is tedious.
Remember to commit these changes in Git:
# The --all flag will tell git to stage all changes you have made # including deletions and new files $ git add --all $ git commit -m "created the base application"
Note
Reminders will no longer be given on when to commit your changes to Git. It is up to readers to develop the habit of committing whenever you reach a stopping point. It is also assumed that you will be operating inside the virtual environment, so all command line prompts will not be prefixed with (env)
.
Using Flask Script
In order to make next chapters easier for the reader, we will use the first of many Flask extensions (packages that extend the functionality of Flask) named Flask Script. Flask Script allows programmers to create commands that act within the Application Context of Flask—that is, the state in Flask that allows modification of the Flask
object. Flask Script comes with some default commands to run the server and a python shell in the Application Context. To install Flask Script with pip
, run this:
$ pip install flask-script
We will cover more advanced usage of Flask Script in Chapter 10, Useful Flask Extensions; for now, let's start with a simple script named manage.py
. First import Flask Script's objects and your app as follows:
from flask.ext.script import Manager, Server from main import app
Then, pass your app to the Manager
object, which will initialize Flask Script:
manager = Manager(app)
Now we add our commands. The server is the same as the normal development server run through main.py
. The make_shell_context
function will create a Python shell that can be run within the app context. The returned dictionary will tell Flask Script what to import by default:
manager.add_command("server", Server()) @manager.shell def make_shell_context(): return dict(app=app)
Note
Running the shell through manage.py
will become necessary later on when the Flask extensions will only initialize when a Flask app is created. Running the default Python shell will cause these extensions to return errors.
Then, end the file with the Python standard way of running only if the user ran this file:
if __name__ == "__main__": manager.run()
You will now be able to run the development server with:
$ python manage.py server
Use the shell with:
$ python manage.py shell # Lets check if our app imported correctly >>> app <Flask 'main'>