Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Flask

You're reading from   Mastering Flask Gain expertise in Flask to create dynamic and powerful web applications

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher Packt
ISBN-13 9781784393656
Length 288 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jack Stouffer Jack Stouffer
Author Profile Icon Jack Stouffer
Jack Stouffer
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started 2. Creating Models with SQLAlchemy FREE CHAPTER 3. Creating Views with Templates 4. Creating Controllers with Blueprints 5. Advanced Application Structure 6. Securing Your App 7. Using NoSQL with Flask 8. Building RESTful APIs 9. Creating Asynchronous Tasks with Celery 10. Useful Flask Extensions 11. Building Your Own Extension 12. Testing Flask Apps 13. Deploying Flask Apps Index

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'>
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime