Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Flask is a lightweight web application microframework written in Python. It makes use of the flexibility of Python to provide a relatively simple template for web application development. Flask makes it possible to write simple one-page applications, but it also has the power to scale them and build larger applications without any issues.

Flask has excellent documentation and an active community. It has a number of extensions, each of which have documentation that can be rated from good to excellent. There are a few books also available on Flask; they are great and provide a lot of insight into the framework and its applications. This book tries to take a different approach to explain the Flask framework and multiple aspects of its practical uses and applications as a whole.

This book takes you through a number of recipes that will help you understand the power of Flask and its extensions. You will start by seeing the different configurations that a Flask application can make use of. From here, you will learn how to work with templates, before learning about the ORM and view layers, which act as the foundation of web applications. Then, you will learn how to write RESTful APIs with Flask, after learning various authentication techniques. As you move ahead, you will learn how to write an admin interface followed by the debugging and logging of errors in Flask. You will also learn how to make your applications multilingual and gain an insight into the various testing techniques. Finally, you will learn about the different deployment and post-deployment techniques on platforms such as Apache, Tornado, Heroku, and AWS Elastic Beanstalk.

By the end of this book, you will have all the necessary information required to make the best use of this incredible microframework to write small and big applications and scale them with industry-standard practices.

A good amount of research coupled with years of experience has been used to develop this book, and I really wish that this book will benefit fellow developers.

What this book covers

Chapter 1, Flask Configurations, helps in understanding the different ways in which Flask can be configured to suit various needs as per the demands of the project. It starts by telling us how to set up our development environment and moves on to the various configuration techniques.

Chapter 2, Templating with Jinja2, covers the basics of Jinja2 templating from the perspective of Flask and explains how to make applications with modular and extensible templates.

Chapter 3, Data Modeling in Flask, deals with one of the most important part of any application, that is, its interaction with the database systems. We will see how Flask can connect to database systems, define models, and query the databases for the retrieval and feeding of data.

Chapter 4, Working with Views, talks about how to interact with web requests and the proper responses to be catered for these requests. It covers various methods of handling the requests properly and designing them in the best way.

Chapter 5, Webforms with WTForms, covers form handling, which is an important part of any web application. As much as the forms are important, their validation holds equal importance, if not more. Presenting this information to the users in an interactive fashion adds a lot of value to the application.

Chapter 6, Authenticating in Flask, deals with authentication, which sometimes acts as a thin red line between the application being secure and insecure. This chapter deals with social logins in detail.

Chapter 7, RESTful API Building, helps in understanding REST as a protocol and then talks about writing RESTful APIs for Flask applications.

Chapter 8, Admin Interface for Flask Apps, focuses on writing admin views for Flask applications. First, we will write completely custom-made views and then write them with the help of an extension.

Chapter 9, Internationalization and Localization, expands the scope of Flask applications and covers the basics of how to enable support for multiple languages.

Chapter 10, Debugging, Error Handling, and Testing, moves on from being completely development-oriented to testing our application. With better error handling and tests, the robustness of the application increases manifold and debugging aids in making the lives of developers easy.

Chapter 11, Deployment and Post Deployment, covers the various ways and tools using which the application can be deployed. Then, you will learn about application monitoring, which helps in keeping track of the performance of the application.

Chapter 12, Other Tips and Tricks, is a collection of some handy tricks that range from full-text search to caching. Then finally, we will go asynchronous with certain tasks in Flask applications.

What you need for this book

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

Who this book is for

If you are a web developer who wants to learn more about developing applications in Flask and scale them with industry-standard practices, this is the book for you. This book will also act as a handy tool if you are aware of Flask's major extensions and want to make the best use of them.

It is assumed that you have knowledge of Python and a basic understanding of Flask. If you are completely new to Flask, reading the book from the first chapter and going forward will help in getting acquainted with Flask as you go ahead.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "After that, create a new file called run.py in the topmost folder."

A block of code is set as follows:

MESSAGES = {
    'default': 'Hello to the World of Flask!',
    'great': 'Flask is great!!',
}

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 wtforms import FileField

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')

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

$ python setup.py install

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Fill up the form and click on Submit."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.