Book Image

Building Web Applications with Flask

By : Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia
Book Image

Building Web Applications with Flask

By: Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia

Overview of this book

Table of Contents (17 chapters)
Building Web Applications with Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

One can hardly develop new applications in our "now world" without duct-taping a lot of technologies together, be it new trend databases, messaging systems, or languages of all kinds. When talking about web development, things might get slightly more complicated as not only do you have to mix a lot of technologies together, but they must also work well with the applications accessing them (also known as web browsers). They should also be compatible with your deployment server, which is another story in itself!

In the Python world, where people deliver amazing desktop software following great guidelines such as the Zen of Python and PEP8, we have, at our disposal, a wide range of libraries and frameworks for creating great web applications, each with its own philosophy. Django, for example, is a bundle solution; it makes choices for you on how your project should look, what should it have, and how things should be done. Web2py is another framework solution that goes beyond and bundles even the IDE with it. These are great concepts, but if you want to create something simple, I would suggest you to do it somewhere else. They're usually good choices, but sometimes they're just too much (the latest Django version seems decisive in changing that; let's keep an eye on further developments).

Flask positions itself, not as a full-power out-of-the-box solution like both Django and Web2py, but as a minimalistic solution where you're given the bare minimum to work with and choose all the other stuff. That's very helpful when you want granular control of your application, when you want to precisely pick your components, or when your solution is simple (not simplistic, okay?).

This book is a response to that scenario of beautiful code and many options in the Web world. It tries to walk through the main concerns regarding web development, from security to content delivery and from session management to REST services and CRUD. Important modern concepts such as overengineering, quality and the development process are covered, so as to achieve better results from day one. To make the learning process smooth, subjects are presented without rush and followed by commented examples. The book also sets out to give readers real-world advice on how to prevent common problems with code.

Come learn how to create great Flask applications, delivering value to your projects and customers!

What this book covers

Chapter 1, Flask in a Flask, I Mean, Book, introduces you to Flask, explaining what it is, what it is not, and how it positions itself in the web framework world.

Chapter 2, First App, How Hard Could it Be?, covers the very first step toward Flask development, including environment setup, your very own "Hello World" app, and how templates enter into this equation. A fluffy chapter it is!

Chapter 3, Man, Do I Like Templates!, deals with face tags and filters progresses through the Jinja2 template engine and how it integrates with Flask. Things start to get a little serious from here!

Chapter 4, Please Fill in This Form, Madam, discusses how to handle forms (as forms are a fact in the web development life) with all the care they need using WTForms in all its glory!

Chapter 5, Where Do You Store Your Stuff?, introduces you to the concepts of relational and non-relational databases, covering how to handle both cases, and also when to.

Chapter 6, But I Wanna REST Mom, Now!, is a chapter on creating REST services (as the REST hype must be satisfied), manually and using the amazing Flask-Restless.

Chapter 7, If Ain't Tested, It Ain't Game, Bro!, is our quality-centric chapter where you learn to deliver quality through proper testing, the TDD and BDD way!

Chapter 8, Tips and Tricks or Flask Wizardry 101, is a dense chapter covering good practices, architecture, blueprints, debugging, and session management.

Chapter 9, Extensions, How I Love Thee, covers all those great Flask extensions not covered so far that will help you achieve the productivity the real world requires from you.

Chapter 10, What Now?, ends our development trip covering all the basics for a healthy deployment, and points you toward your next steps in the Flask world.

What you need for this book

To make the most of your reading experience, the reader is expected to have a machine with Ubuntu 14.x or superior installed as the examples are designed for this setup, a basic knowledge of Python (if you don't have this, refer to http://learnxinyminutes.com/docs/python/ first), and a text editor with highlights of your likings (LightTable, Sublime, Atom). Other required software will be discussed through the chapters.

Who this book is for

This book targets Python developers, with some or no experience with web development, who wish to create minimalistic web applications. It is focused on those who want to become web developers as all the basics are covered to some extent, and also on those who already are familiar with web development using other frameworks, be it Python-based frameworks such as Django, Bottle, or Pyramid, or frameworks from other languages.

It is also important that you have a basic understanding of web technologies used to construct web pages, as is the case for CSS, JavaScript, and HTML. If that is not your background, please check out the W3Schools website (http://w3schools.com/) as it covers the basics of using these technologies. Also, if you're skilled with the Linux terminal, your life will be much easier throughout the whole book; try the link https://help.ubuntu.com/community/UsingTheTerminal if this is not the case.

Nonetheless, be assured that, if you have a basic knowledge of Python, you're more than capable of understanding the examples and the chapters; at the end of the book, you will be creating amazing web applications that perform well and are easy to maintain.

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: "Enter the new project folder and create the main.py file".

A block of code is set as follows:

# coding:utf-8
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

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

sudo pip install virtualenvwrapper

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: "Have you ever imagined what happens when you fill in a form on a website and click on that fancy Send button at the end of it?".

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.