Reader small image

You're reading from  Building Web Applications with Flask

Product typeBook
Published inJun 2015
Reading LevelBeginner
Publisher
ISBN-139781784396152
Edition1st Edition
Languages
Tools
Right arrow
Authors (4):
Italo M Campelo Maia
Italo M Campelo Maia
author image
Italo M Campelo Maia

Italo Maia is a full-stack developer with 10 years of experience in creating software for the mobile, Web, and desktop environments, having dedicated most of the last few years to development with Python and web technologies. Author of Flask-Empty, a popular skeleton for Flask projects that aggregates good practices and recipes for quick prototyping, he is active in the Brazilian Python communities, having open source tools and libraries available in GitHub and Bitbucket.
Read more about Italo M Campelo Maia

Jack Stouffer
Jack Stouffer
author image
Jack Stouffer

Jack Stouffer is a programmer who has several years of experience in designing web applications. He switched to Flask three years ago for all his projects. He currently works for Apollo America in Auburn Hills, Michigan, and writes internal business tools and software using Python, Flask, and JavaScript. Jack is a believer and supporter of open source technology. When he released his Flask examples with the recommended best practices on GitHub (https://github.com/JackStouffer), it became one of the most popular Flask repositories on the site. Jack has also worked as a reviewer for Flask Framework Cookbook, Packt Publishing.
Read more about Jack Stouffer

Gareth Dwyer
Gareth Dwyer
author image
Gareth Dwyer

Gareth Dwyer hails from South Africa but now lives in Europe. He is a software engineer and author and is currently serving as the CTO at the largest coding education provider in Africa. Gareth is passionate about technology, education, and sharing knowledge through mentorship. He holds four university degrees in computer science and machine learning, with a specialization in natural language processing. He has worked with companies such as Amazon Web Services and has published many online tutorials as well as the book Flask by Example.
Read more about Gareth Dwyer

Italo Maia
Italo Maia
author image
Italo Maia

Italo Maia is a full-stack developer with 10 years of experience in creating software for the mobile, Web, and desktop environments, having dedicated most of the last few years to development with Python and web technologies. Author of Flask-Empty, a popular skeleton for Flask projects that aggregates good practices and recipes for quick prototyping, he is active in the Brazilian Python communities, having open source tools and libraries available in GitHub and Bitbucket.
Read more about Italo Maia

View More author details
Right arrow

Chapter 6. But I Wanna REST Mom, Now!

REST is an architectural style that has been gaining momentum these last few years due to its many features and architectural constraints such as cacheability, stateless behavior, and its interface requirement.

Our focus in this chapter will be on RESTful Web Services and APIs—that is, Web services and Web APIs following the REST architecture. Let's start at the beginning: what is a Web service?

A Web service is a Web application that can be consulted by your application as if it was an API, improving the user experience. If your RESTful Web service does not need to be called from a traditional UI interface, and may be used standalone, then what you have is a RESTful Web Service API, "RESTful API" for short, that works just like a regular API, but through...

Beyond GET


So far we've had a few cozy examples with Ajax and RESTful Web services but we have yet to record data in our database using a service. How about trying that now?

Recording to the database using Web services is not much different from what we have done in the previous chapter. We'll receive data from an Ajax request, we will check which HTTP method was used in order to decide what to do, then we'll validate the sent data and save everything if no error was found. In Chapter 4, Please Fill in This Form, Madam, we talked about CSRF protection and its importance. We'll keep validating our data against CSRF with our Web service. The trick is to add the CSRF token to the form data being submitted. See the attached code provided with the eBook for the example HTML.

This is how our view looks like with POST, PUT, and REMOVE method support:

@app.route("/articles/", methods=["GET", "POST"])
@app.route("/articles/<int:article_id>", methods=["GET", "PUT", "DELETE"])
def articles(article_id...

Flask-Restless


Flask-Restless is an extension capable of auto-generating a whole RESTful API for your SQLAlchemy models with support for GET, POST, PUT, and DELETE. Most Web services won't need more than that. Another advantage to using Flask-Restless is the chance to extend the auto-generated methods with authentication validation, custom behavior, and custom queries. This is a must-learn extension!

Let's see how our Web service would look with Flask-Restless. We'll also have to install a new library for this example:

pip install Flask-Restless

And then:

# coding:utf-8

from flask import Flask, url_for
from flask.ext.restless import APIManager
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/employees.sqlite'

db = SQLAlchemy(app)


class Article(db.Model):
    __tablename__ = 'articles'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable...

Summary


In this chapter, we learned what REST is, its advantages, how to create Flask RESTful Web Services and APIs, and how to use Flask-RESTless to make the whole thing work well. We also had an overview on what jQuery is and how to use it to send Ajax requests to consult our services. These chapter examples were pretty intense. Try to code the examples yourself, to assimilate them better.

In the next chapter, we'll be talking about the one way in which you can assure software quality: tests! We'll learn how to test our Web applications in the many ways that they may be tested and how to integrate these tests into our very coding routines. See you there!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Web Applications with Flask
Published in: Jun 2015Publisher: ISBN-13: 9781784396152
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Italo M Campelo Maia

Italo Maia is a full-stack developer with 10 years of experience in creating software for the mobile, Web, and desktop environments, having dedicated most of the last few years to development with Python and web technologies. Author of Flask-Empty, a popular skeleton for Flask projects that aggregates good practices and recipes for quick prototyping, he is active in the Brazilian Python communities, having open source tools and libraries available in GitHub and Bitbucket.
Read more about Italo M Campelo Maia

author image
Jack Stouffer

Jack Stouffer is a programmer who has several years of experience in designing web applications. He switched to Flask three years ago for all his projects. He currently works for Apollo America in Auburn Hills, Michigan, and writes internal business tools and software using Python, Flask, and JavaScript. Jack is a believer and supporter of open source technology. When he released his Flask examples with the recommended best practices on GitHub (https://github.com/JackStouffer), it became one of the most popular Flask repositories on the site. Jack has also worked as a reviewer for Flask Framework Cookbook, Packt Publishing.
Read more about Jack Stouffer

author image
Gareth Dwyer

Gareth Dwyer hails from South Africa but now lives in Europe. He is a software engineer and author and is currently serving as the CTO at the largest coding education provider in Africa. Gareth is passionate about technology, education, and sharing knowledge through mentorship. He holds four university degrees in computer science and machine learning, with a specialization in natural language processing. He has worked with companies such as Amazon Web Services and has published many online tutorials as well as the book Flask by Example.
Read more about Gareth Dwyer

author image
Italo Maia

Italo Maia is a full-stack developer with 10 years of experience in creating software for the mobile, Web, and desktop environments, having dedicated most of the last few years to development with Python and web technologies. Author of Flask-Empty, a popular skeleton for Flask projects that aggregates good practices and recipes for quick prototyping, he is active in the Brazilian Python communities, having open source tools and libraries available in GitHub and Bitbucket.
Read more about Italo Maia