Book Image

Mastering Flask

By : Jack Stouffer
Book Image

Mastering Flask

By: Jack Stouffer

Overview of this book

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you’ll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.
Table of Contents (20 chapters)
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Flask Script


In Chapter 1, Getting Started, we created a basic manage script with the Flask extension Flask Script to allow easy running of the server and debugging with the shell. In this chapter, we will cover the features that were not covered in that basic introduction.

In Flask Script, you can create custom commands to be run within the application context. All that is needed is to create a command to decorate a normal Python function with a decorator function provided by Flask Script. For example, if we wanted a task that would return the string "Hello, World!" we would add the following to manage.py:

@manager.command
def test():
    print "Hello, World!"

From the command line, the test command can now be run using the following:

$ python manage.py test
Hello, World!

Delete the test command, and let's create a simple command to help set up new developers on our application by creating their SQLite database and filling it with test data. This command is partially lifted from the script...