Book Image

Flask Framework Cookbook - Second Edition

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook - Second Edition

By: Shalabh Aggarwal

Overview of this book

Flask, the lightweight Python web framework, is popular thanks to its powerful modular design that lets you build scalable web apps. With this recipe-based guide, you’ll explore modern solutions and best practices for Flask web development. Updated to the latest version of Flask and Python 3, this second edition of Flask Framework Cookbook moves away from some of the old and obsolete libraries and introduces new recipes on cutting-edge technologies. You’ll discover different ways of using Flask to create, deploy, and manage microservices. This Flask Python book starts by covering the different configurations that a Flask application can make use of, and then helps you work with templates and learn about the ORM and view layers. You’ll also be able to write an admin interface and get to grips with debugging and logging errors. Finally, you’ll learn a variety of deployment and post-deployment techniques for platforms such as Apache, Tornado, and Heroku. By the end of this book, you’ll have gained all the knowledge you need to confidently write Flask applications and scale them using standard industry practices.
Table of Contents (15 chapters)

To get the most out of this book

In most cases, you will just need a computer system with an average configuration to run the code in this book. Usually, any OS will do, but Linux and macOS are preferred over Windows.

It is assumed that you have some knowledge of Python. It would be useful to have some knowledge of Python 3 beforehand, or at least Python 3 should be installed on the machine on which the code samples from this book are being used.

Please refer to the references and links provided in the book to learn more about the libraries and tools used.

Do not copy and paste code from the book, as there can be formatting issues. It is always advisable to type in the code manually as it will prevent unnecessary issues and facilitate better learning.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/Flask-Framework-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

The code bundle is also available on https://github.com/PacktPublishing/Flask-Framework-Cookbook-Second-Edition.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "In Flask, a configuration is done on an attribute named config of the Flask object."

A block of code is set as follows:

from flask import Flask 
app = Flask(__name__) 
 
@app.route('/') 
def hello_world(): 
    return 'Hello to the World of Flask!' 
 
if __name__ == '__main__': 
    app.run() 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from flask_wtf.file import FileField, FileRequired 
 
class Product(db.Model): 
    image_path = db.Column(db.String(255)) 
 
    def __init__(self, name, price, category, image_path): 
        self.image_path = image_path 
 
class ProductForm(NameForm): 
 image = FileField('Product Image', validators=[FileRequired()]) 

Any command-line input or output is written as follows:

  $ pip3 install Flask

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "It can also handle the Remember me feature, account recovery features, and so on."

Warnings or important notes appear like this.
Tips and tricks appear like this.