Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Flask Framework Cookbook
  • Table Of Contents Toc
Flask Framework Cookbook

Flask Framework Cookbook - Third Edition

By : Shalabh Aggarwal
5 (10)
close
close
Flask Framework Cookbook

Flask Framework Cookbook

5 (10)
By: Shalabh Aggarwal

Overview of this book

Discover what makes Flask, the lightweight Python web framework, popular, as you delve into its modular design that enables the development of scalable web apps. With this practical guide, you'll explore modern solutions, recommended design patterns, and best practices for Flask web development. Updated to the latest version of Flask and Python, this third edition of the Flask Framework Cookbook moves away from the outdated libraries, updates content to incorporate new coding patterns, and introduces recipes for the latest tools. You'll explore different ways to integrate with GPT to build AI-ready Flask applications. The book starts with an exploration of Flask application configurations and then guides you through working with templates and understanding the ORM and view layers. You’ll also be able to write an admin interface and get to grips with testing using the factory pattern, debugging, and logging errors. Then you’ll discover different ways of using Flask to create, deploy, and manage microservices using AWS, GCP, and Kubernetes. Finally, you’ll gain insights into various deployment and post-deployment techniques for platforms such as Apache, Tornado, and Datadog. By the end of this book, you'll have acquired the knowledge necessary to write Flask applications that cater to a wide range of use cases in the best possible way and scale them using standard industry practices.
Table of Contents (20 chapters)
close
close
1
Part 1: Flask Fundamentals
6
Part 2: Flask Deep Dive
12
Part 3: Advanced Flask

Flask Configurations

This introductory chapter will help us understand the different ways Flask can be configured to suit the various needs of a project. Flask is “The Python micro framework for building web applications” (pallets/Flask, https://github.com/pallets/flask).

So, why is Flask called a microframework? Does this mean Flask lacks functionality, or that it’s mandatory for the complete code of your web application to be contained in a single file? Not really! The term microframework simply refers to the fact that Flask aims to keep the core of its framework small but highly extensible. This makes writing applications or extensions both easy and flexible and gives developers the power to choose the configurations they want for their application without imposing any restrictions on the choice of database, templating engine, admin interface, and so on. In this chapter, you will learn several ways to set up and configure Flask.

Important information

This whole book uses Python 3 as the default version of Python. Python 2 lost its support on December 31, 2019, and is therefore not supported in this book. It is recommended that you use Python 3 while learning from this book, as many of the recipes might not work on Python 2.

Likewise, while writing this book, Flask 2.2.x was the latest version. Although a lot of code in this book can work on earlier versions of Flask, it is recommended that you use versions 2.2.x and above.

Getting started with Flask takes just a couple of minutes. Setting up a simple Hello World application is as easy as pie. Simply create a file, such as app.py, in any location on your computer that can access python or python3 that contains the following script:

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

Now, Flask needs to be installed; this can be done via pip or pip3. You may have to use sudo on a Unix-based machine if you run into access issues:

$ pip3 install Flask

Important

The code and Flask installation example here is just intended to demonstrate the ease with which Flask can be used. To set up a proper development environment, follow the recipes in this chapter.

The preceding snippet is a complete Flask-based web application. Here, an instance of the imported Flask class is a Web Server Gateway Interface (WSGI) (http://legacy.python.org/dev/peps/pep-0333/) application. So, app in this code becomes our WSGI application, and as this is a standalone module, we set the __name__ string to '__main__'. If we save this in a file called app.py, then the application can simply be run using the following command:

$ python3 app.py
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

Now, if we head over to our browser and type http://127.0.0.1:5000/, we can see our application running.

Alternatively, the application can be run by using flask run or Python’s -m switch with Flask. While following this approach, the last two lines of app.py can be skipped. Note that the following commands work only if there is a file named app.py or wsgi.py in the current directory. If not, then the file containing the app object should be exported as an environment variable, namely FLASK_APP. As a best practice, this should be done in either case:

$ export FLASK_APP=app.py
$ flask run
* Running on http://127.0.0.1:5000/

Alternatively, if you decide to use the -m switch, it will look as follows:

$ export FLASK_APP=app.py
$ python3 -m flask run
* Running on http://127.0.0.1:5000/

Tip

Never save your application file as flask.py; if you do, it will conflict with Flask itself while importing.

In this chapter, we will cover the following recipes:

  • Setting up our environment with virtualenv
  • Handling basic configurations
  • Configuring class-based settings
  • Organizing static files
  • Being deployment-specific with the instance folder
  • Compositions of views and models
  • Creating a modular web app with blueprints
  • Making a Flask app installable using setuptools
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Flask Framework Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon