Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Building Web Applications with Flask
Building Web Applications with Flask

Building Web Applications with Flask: Use Python and Flask to build amazing web applications, just the way you want them!

Arrow left icon
Profile Icon Italo Maia Profile Icon Italo M Campelo Maia Profile Icon Gareth Dwyer Profile Icon Jack Stouffer
Arrow right icon
£15.99 £23.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (1 Ratings)
eBook Jun 2015 160 pages 1st Edition
eBook
£15.99 £23.99
Paperback
£29.99
Subscription
Free Trial
Renews at £16.99p/m
Arrow left icon
Profile Icon Italo Maia Profile Icon Italo M Campelo Maia Profile Icon Gareth Dwyer Profile Icon Jack Stouffer
Arrow right icon
£15.99 £23.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (1 Ratings)
eBook Jun 2015 160 pages 1st Edition
eBook
£15.99 £23.99
Paperback
£29.99
Subscription
Free Trial
Renews at £16.99p/m
eBook
£15.99 £23.99
Paperback
£29.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Building Web Applications with Flask

Chapter 2. First App, How Hard Could it Be?

After a full chapter without a single line of code, you need this, right? In this chapter, we will write our first app explained line by line; we will also cover how to set up our environment, what tools to use for development, and how to work with HTML in our app.

Hello World

The first app one writes when learning a new technology is usually a Hello World app, which consists of the minimum possible code necessary to start a simple application and show the text "Hello World!". Let's do exactly that using Flask.

This book is optimized for Python 2.x, so, that's the version I advise you to use from now on. All the examples and code are aimed at that Python version, which is the default in most Linux distributions.

Prerequisites and tools

First, let's make sure our environment is properly configured. For this course, I assume you are using a Debian-like Linux distribution, such as Mint (http://www.linuxmint.com/) or Ubuntu (http://ubuntu.com/). All the instructions will be geared towards these systems.

Let's begin by installing the required Debian packages with apt-get as follows:

sudo apt-get install python-dev python-pip

This will install the Python development tools and libraries required for compiling Python packages, and pip: a neat tool you can use to install Python packages from the command line. On with it! Let's install our virtual environment managing tool:

sudo pip install virtualenvwrapper
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

To explain what we just did: sudo tells our OS that we want administrative privileges to run the next command, and pip is the default Python package management tool and helps us install the virtualenvwrapper package...

Setting up a virtual environment

A virtual environment is the way Python isolates full package environments from one another. This means you can easily manage dependencies. Imagine you want to define the minimum necessary packages for a project; a virtual environment would be perfect to let you test and export the list of needed packages. We will discuss it later on. Now, create a new terminal pressing Ctrl + Shift + T on your keyboard and create our hello world environment like this:

mkvirtualenv hello
pip install flask

The first line creates our environment with the name "hello". You will also automatically load that environment into the current terminal. You can deactivate your virtual environment by typing deactivate and you can load it again with the following command:

workon hello  # substitute hello with the desired environment name if needed

The second line tells pip to install the Flask package in the current virtual environment, hello in this case.

Understanding the "Hello World" app

Given the environment set, what should we use to write our beautiful code? An editor or an IDE? If you're working on a low budget, try Light Table editor (http://lighttable.com/). Free, fast, and easy to use (Ctrl + Spacebar gives you access to all available options), it also has workspace support! Can't get any better for the money. If you're a lucky one with $200 to spare (or if you have a free license https://www.jetbrains.com/pycharm/buy/), just fork out for the PyCharm IDE which is pretty much the best IDE for Python Web development. Now let's move on.

Create a folder that will hold your project files (you don't need to but people will like you more if you do), as follows:

mkdir hello_world

Enter the new project folder and create the main.py file:

cd hello_world
touch main.py

The main.py file will have the whole "Hello World" application in it. Our main.py content should be like this:

# coding:utf-8
from flask...

Serving HTML pages

First, to make our hello function respond with HTML, all we have to do is change it like this:

def hello():
    return "<html><head><title>Hi there!</title></head><body>Hello World!</body></html>", 200

In the preceding example, hello is returning a HTML formatted string and a number. The string will be parsed as HTML by default while 200 is an optional HTTP code indicating a successful response. 200 is returned by default.

If you refresh your browser with F5, you'll notice that nothing has changed. That's why the Flask development server is not reloading when the source changes. That only happens when you run your application in debug mode. So let's do that:

app = Flask(__name__)
app.debug=True

Now go to the terminal where your application is running, type Ctrl + C then restart the server. You will notice a new output besides the URL where your server is running—something about "stat&quot...

Hello World


The first app one writes when learning a new technology is usually a Hello World app, which consists of the minimum possible code necessary to start a simple application and show the text "Hello World!". Let's do exactly that using Flask.

This book is optimized for Python 2.x, so, that's the version I advise you to use from now on. All the examples and code are aimed at that Python version, which is the default in most Linux distributions.

Left arrow icon Right arrow icon

Description

If you are a Python web developer who wants to learn more about developing applications in Flask and scaling them with industry-standard practices, this is the book for you.

Who is this book for?

If you are a Python web developer who wants to learn more about developing applications in Flask and scaling them with industry-standard practices, this is the book for you.

What you will learn

  • Create single page applications with Flask
  • Use macros, filters, tags, and control structures to render rich responses to user requests
  • Receive forms securely through formbased classes and WTForms
  • Utilize NoSQL or SQL databases to store user data seamlessly
  • Generate simple, yet powerful, REST services from your data models
  • Learn how to perform testdriven and behaviordriven developments in your Flask projects
  • Use Flask components to create maintainable nonflat projects
  • Harvest the power of extensions to build robust authorization and permission policies

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 26, 2015
Length: 160 pages
Edition : 1st
Language : English
ISBN-13 : 9781784390549
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 26, 2015
Length: 160 pages
Edition : 1st
Language : English
ISBN-13 : 9781784390549
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
£169.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts
£234.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 103.97
Learning Flask Framework
£36.99
Mastering Flask
£36.99
Building Web Applications with Flask
£29.99
Total £ 103.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Flask in a Flask, I Mean, Book Chevron down icon Chevron up icon
2. First App, How Hard Could it Be? Chevron down icon Chevron up icon
3. Man, Do I Like Templates! Chevron down icon Chevron up icon
4. Please Fill in This Form, Madam Chevron down icon Chevron up icon
5. Where Do You Store Your Stuff? Chevron down icon Chevron up icon
6. But I Wanna REST Mom, Now! Chevron down icon Chevron up icon
7. If Ain't Tested, It Ain't Game, Bro! Chevron down icon Chevron up icon
8. Tips and Tricks or Flask Wizardry 101 Chevron down icon Chevron up icon
9. Extensions, How I Love Thee Chevron down icon Chevron up icon
10. What Now? Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(1 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Po-An, Yang Oct 10, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
It can be batter with python 3.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.