Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Presentation

Building REST APIs with Flask_Create Python Web Services with MySQL

Rating
-
Sold
-
Pages
165
Uploaded on
18-07-2024
Written in
2017/2018

"Develop RESTful web services using the Flask micro-framework and integrate them using MySQL. Use Flask to develop, deploy, and manage REST APIs with easy-to-read and understand Python code. Solve your problem from a choice of libraries. Learn to use MySQL as the web services database for your Flask API using SQLAlchemy ORM. Building REST APIs with Flask provides a primer on Flask, RESTful services, and working with pip to set up your virtual environment. The key differences between NoSQL and SQL are covered, and you are taught how to connect MySQL and Flask using SQLAlchemy. Author Kunal Relan presents best practices for creating REST APIs and guides you in structuring your app and testing REST endpoints. He teaches you how to set up authentication and render HTML using views. You learn how to write unit tests for your REST APIs, and understand mocks, assertions, and integration testing. You will know how to document your REST APIs, deploy your Flask application on all of the major cloud platforms, and debug and monitor your Flask application."

Show more Read less
Institution
Course

Content preview

,1. Beginning with Flask
Kunal Relan1
(1)
New Delhi, Delhi, India

Flask is a BSD licensed, Python microframework based on Werkzeug and Jinja2. Being a
microframework doesn’t make it any less functional; Flask is a very simple yet highly
extensible framework. This gives developers the power to choose the configuration they
want, thereby making writing applications or plugins easy. Flask was originally created by
Pocoo, a team of open source developers in 2010, and it is now developed and maintained
by The Pallets Project who power all the components behind Flask. Flask is supported by an
active and helpful developer community including an active IRC channel and a mailing list.

Introduction to Flask
Flask has two major components, Werkzeug and Jinja2. While Werkzeug is responsible for
providing routing, debugging, and Web Server Gateway Interface (WSGI), Flask leverages
Jinja2 as template engine. Natively, Flask doesn’t support database access, user
authentication, or any other high-level utility, but it does provide support for extensions
integration to add all such functionalities, making Flask a micro- yet production-ready
framework for developing web applications and services. A simple Flask application can fit
into a single Python file or it can be modularized to create a production-ready application.
The idea behind Flask is to build a good foundation for all applications leaving everything
else on extensions.

Flask community is quite big and active with hundreds of open source extensions. The
Flask core team continuously reviews extensions and ensures approved extensions are
compatible with the future releases. Flask being a microframework provides flexibility to the
developers to choose the design decisions appropriate to their project. It maintains a registry
of extensions which is regularly updated and continuously maintained.

Starting Flask

Flask, just like all other Python libraries, is installable from the Python Package Index
(PPI) and is really easy to setup and start developing with, and it only takes a few minutes to
getting started with Flask. To be able to follow this book, you should be familiar with Python,
command line (or at least PIP), and MySQL.

As promised, Flask is really easy to start with, and just five lines of code lets you get
started with a minimal Flask application.
from flask import Flask
app = Flask(__name__)
@app.route('/')

, def hello_world():
return 'Hello, From Flask!'
if __name__== '__main__':
app.run()
Listing 1-1

Basic Flask Application

The preceding code imports the Flask library, initiates the application by creating an
instance of the Flask class, declares the route, and then defines the function to execute when
the route is called. This code is enough to start your first Flask application.

The following code launches a very simple built-in server, which is good enough for
testing but probably not when you want to go in production, but we will cover that in the
later chapters.

When this application starts, the index route upon request shall return “Hello From
Flask!” as shown in Figure 1-1.




Figure 1-1

Flask minimal application

Flask Components Covered in This Book

Now that you have been introduced to Flask, we will discuss the components that we’ll cover
in Flask REST API development in this book.

This book will serve as a practical guide to REST API development using Flask, and we’ll
be using MySQL as the backend database. As already discussed, Flask doesn’t come with
native database access support, and to bridge that gap, we’ll use a Flask extension called
Flask-SQLAlchemy which adds support for SQLAlchemy in Flask. SQLAlchemy is essentially

, a Python SQL toolkit and Object Relational Mapper which provides the developers the full
power and flexibility of SQL.

SQLAlchemy provides full support for enterprise-level design patterns and is designed
for high-performing database access while maintaining efficiency and ease of use. We’ll build
a user authentication module, CRUD (Create, Read, Update, and Delete) REST APIs for object
creation, retrieval, manipulation, and deletion. We’ll also integrate a documentation utility
called Swagger for creating API documentation, write unit and integration tests, learn
application debugging, and, finally, check out different methods of deploying and monitoring
our REST APIs on cloud platforms for production use.

For unit tests, we’ll use pytest which is a full-featured Python testing tool—pytest is easy
to write tests with and yet is scalable to support complex use cases. We’ll also use Postman
which is a complete REST API Platform—Postman provides integration tools for every stage
of the API lifecycle, making API development easier and more reliable.

API deployment and monitoring are critical parts of REST API development;
development paradigm changes drastically when it comes to scaling the APIs for production
use cases, and for the sake of this book, we’ll deploy our REST APIs using uWSGI and Nginx
on a cloud Ubuntu server. We’ll also deploy our REST APIs on Heroku which is a cloud
platform that facilitates Flask app deployment and scaling out of the box.

Last but not least, we’ll discuss debugging common Flask errors and warnings and
debugging Nginx requests and check out Flask application monitoring ensuring least amount
on the downtime for production use.

Introduction to RESTful Services
Representational State Transfer (REST) is a software architectural style for web services that
provides a standard for data communication between different kinds of systems. Web
services are open standard web applications that interact with other applications with a
motive of exchanging data making it an essential part of client server architecture in modern
web and mobile applications. In simple terms, REST is a standard for exchanging data over
the Web for the sake of interoperability between computer systems. Web services which
conform to the REST architectural style are called RESTful web services which allow
requesting systems to access and manipulate the data using a uniform and predefined set of
stateless operations.

Since its inception in 2000 by Roy Feilding, RESTful architecture has grown a lot and has
been implemented in millions of systems since then. REST has now become one of the most
important technologies for web-based applications and is likely to grow even more with its
integration in mobile and IoT-based applications as well. Every major development language
has frameworks for building REST web services. REST principles are what makes it popular
and heavily used. REST is stateless, making it straightforward for any kind of system to use
and also making it possible for each request to be served by a different system.

Written for

Course

Document information

Uploaded on
July 18, 2024
Number of pages
165
Written in
2017/2018
Type
PRESENTATION
Person
Unknown

Subjects

$4.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
RobertCuong

Get to know the seller

Seller avatar
RobertCuong Telecommunication
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
225
Last sold
-
GPON and WiFi

+ SDH solution based on Fujitsu/Alcatel/Huawei devices in deployment and troubleshoot + Switching and Routing network fundamental and advance + GPON solution with deep knowledge of PLOAM/OMCI, activation procedure. Analysis of Private/Public OMCI + WiFi solution with WiFi Management/Control/Data. WiFi bandsteering, WiFi mesh, and WiFi 6, 6E, 7, ...

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions