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
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Python API Development Fundamentals
Python API Development Fundamentals

Python API Development Fundamentals: Develop a full-stack web application with Python and Flask

Arrow left icon
Profile Icon Chan Profile Icon Ray Chung Profile Icon Jack Huang
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (7 Ratings)
Paperback Nov 2019 372 pages 1st Edition
eBook
₱1113.99 ₱1591.99
Paperback
₱1989.99
Subscription
Free Trial
Arrow left icon
Profile Icon Chan Profile Icon Ray Chung Profile Icon Jack Huang
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (7 Ratings)
Paperback Nov 2019 372 pages 1st Edition
eBook
₱1113.99 ₱1591.99
Paperback
₱1989.99
Subscription
Free Trial
eBook
₱1113.99 ₱1591.99
Paperback
₱1989.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Python API Development Fundamentals

2. Starting to Build Our Project

Learning Objectives

By the end of this chapter, you will be able to:

  • Build a Restful API service efficiently using the Flask-Restful package
  • Build an extendable Flask project
  • Perform CRUD operations using the model
  • Test RESTful APIs using curl, httpie, and Postman

In this chapter, we will start to work on the food recipe-sharing platform and learn how to create a RESTful API application.

Introduction

Now that we've introduced APIs and learned a bit about HTTP and REST, we will work on building an application (the recipe-sharing app known as Smilecook). In this chapter, we aim to kick-start the actual project development. This is a recipe-sharing platform in which users can create accounts and share their own recipes with other users. As you can imagine, it will contain a lot of API endpoints for our users so that they can manage their recipes. We will be using the Flask-RESTful package to efficiently develop our RESTful API.

This chapter will talk about the CRUD (Create, Read, Update, Delete) of these recipes, as well as how to set the publish status of the recipe.

What is Flask-RESTful?

Flask-RESTful is a Flask extension that allows us to quickly develop RESTful APIs. Compared to the built-in wrapper, @app.route('/'), which we discussed in the previous chapter, Flask-RESTful allows us to maintain and structure the API endpoints in a much better and easier way.

In this chapter, we will develop our project using this Flask extension so that you will see how we can structure our endpoints.

Using Flask-RESTful to Develop Our Recipe-Sharing Platform, "Smilecook"

In this book, we are going to develop a recipe-sharing platform called Smilecook. Beginning with this chapter, we will start adding functions to it. We believe this approach will help you learn about the key concepts and skills you will need so that you can develop this application and help it reach its full potential, while at the same time helping you understand the entire development workflow.

First, we will build the basic CRUD functions of the recipes...

Virtual Environment

PyCharm will help us create a virtual environment. We want to develop our project in its own virtual environment in order to keep it isolated. Due to this, we will have absolute control over the versions of the packages that we are going to use.

The best way to learn is through practice. Let's get our hands dirty now!

Exercise 5: Creating a Development Project in PyCharm

Before you start developing the Python application, you'll need to create a development project in PyCharm. PyCharm manages things using projects. In this exercise, you will learn how to create a new development project in PyCharm called Smilecook. You will also need to install the necessary packages for this project. Let's get started:

  1. Create the project and name it smilecook:
    Figure 2.2: Creating a project
  2. Check the project structure and ensure that the virtual environment has been created. Once the module has been created, we will be able to see the project...

Creating a Recipe Model

As you can imagine, a recipe may have several attributes. To save every detail of these attributes, we will model the recipe using a class. This recipe class is going to have several essential attributes.

Here is a brief description of the attributes that we will define in the recipe class:

  • name: The name of the recipe.
  • description: The description of the recipe.
  • num_of_servings: The number of servings.
  • cook_time: The cooking time required. This is an integer whose units are in seconds.
  • directions: The directions.
  • is_publish: The publish status of the recipe; the default is draft.

In the next exercise, we will show you how to code the recipe class so that it has these attributes.

Exercise 6: Creating the Recipe Model

In this exercise, we will code the recipe model, step by step. The recipe class will contain the attributes that we discussed previously. The code file for this exercise can be found in Lesson2/Exercise06...

Configuring Endpoints

Now that we have defined all our resources, we will set up some endpoints so that users can send requests to them. These endpoints can be accessed by the users and are connected to specific resources. We will be using the add_resource method on the API object to specify the URL for these endpoints and route the client HTTP request to our resources.

For example, the api.add_resource(RecipeListResource, '/recipes') syntax is used to link the route (relative URL path) to RecipeListResource so that HTTP requests will be directed to this resource. Depending on the HTTP verb (for example, GET, and POST), the request will be handled by the corresponding methods in the resource accordingly.

Exercise 10: Creating the Main Application File

In this exercise, we will create our app.py file, which will be our main application file. We will set up Flask and initialize our flask_restful.API there. Finally, we will set up the endpoints so that users can send...

Making HTTP Requests to the Flask API using curl and httpie

Now, we are going to use the httpie and curl commands to test our API endpoints. We will test the functions for getting all the recipes back from the server and create/update/delete, publish, and unpublish the recipes. The best way to learn this is to complete a hands-on exercise. Let's get started!

Exercise 11: Testing the Endpoints Using curl and httpie

In this exercise, we are going to use the httpie and curl commands to send requests to the endpoints so that we can create our first recipe. We want you to get comfortable using the httpie and curl command-line testing tool. Let's get started:

  1. Open the Terminal in PyCharm and type in the following commands. You can use either the httpie or curl command. The following is the httpie command (= is for string and := is for non-string):
    http POST localhost:5000/recipes name="Cheese Pizza" description="This is a lovely cheese pizza" num_of_servings...

Summary

In this chapter, we built the RESTful API using the Flask-RESTful package. By doing this, you have seen how simple and easy it is to perform such tasks. We are building our project in a structural manner, which allows us to easily extend the project in the subsequent chapters. In this chapter, we created the models and resources folder; we will be developing more models and resources later in this book. So far, our food recipe-sharing platform, Smilecook, is capable of performing CRUD, as well as setting the publish status of the recipe. We have also tested the application to make sure it is working properly. Finally, you started to realize the power of Postman, which greatly automates the whole testing process. In the next chapter, we will learn about how to perform data validation.

Left arrow icon Right arrow icon

Key benefits

  • Delve deep into the principle behind RESTful API
  • Learn how to build a scalable web application with the RESTful API architecture and Flask framework
  • Know what are the exact tools and methodology to test your applications and how to use them

Description

Python is a flexible language that can be used for much more than just script development. By knowing the Python RESTful APIs work, you can build a powerful backend for web applications and mobile applications using Python. You'll take your first steps by building a simple API and learning how the frontend web interface can communicate with the backend. You'll also learn how to serialize and deserialize objects using the marshmallow library. Then, you'll learn how to authenticate and authorize users using Flask-JWT. You'll also learn how to enhance your APIs by adding useful features, such as email, image upload, searching, and pagination. You'll wrap up the whole book by deploying your APIs to the cloud. By the end of this book, you'll have the confidence and skill to leverage the power of RESTful APIs and Python to build efficient web applications.

Who is this book for?

This book is ideal for aspiring software developers who have a basic-to-intermediate knowledge of Python programming and who want to develop web applications using Python. Knowledge of how web applications work will be beneficial but is not essential.

What you will learn

  • Understand the concept of a RESTful API
  • Build a RESTful API using Flask and the Flask-Restful extension
  • Manipulate a database using Flask-SQLAlchemy and Flask-Migrate
  • Send out plaintext and HTML format emails using the Mailgun API
  • Implement a pagination function using Flask-SQLAlchemy
  • Use caching to improve API performance and efficiently obtain the latest information
  • Deploy an application to Heroku and test it using Postman

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 22, 2019
Length: 372 pages
Edition : 1st
Language : English
ISBN-13 : 9781838983994
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 22, 2019
Length: 372 pages
Edition : 1st
Language : English
ISBN-13 : 9781838983994
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 ₱260 each
Feature tick icon Exclusive print discounts
$279.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 ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 6,481.97
Hands-On Docker for Microservices with Python
₱2245.99
Python API Development Fundamentals
₱1989.99
Python GUI Programming Cookbook
₱2245.99
Total 6,481.97 Stars icon

Table of Contents

10 Chapters
1. Your First Step Chevron down icon Chevron up icon
2. Starting to Build Our Project Chevron down icon Chevron up icon
3. Manipulating a Database with SQLAlchemy Chevron down icon Chevron up icon
4. Authentication Services and Security with JWT Chevron down icon Chevron up icon
5. Object Serialization with marshmallow Chevron down icon Chevron up icon
6. Email Confirmation Chevron down icon Chevron up icon
7. Working with Images Chevron down icon Chevron up icon
8. Pagination, Searching, and Ordering Chevron down icon Chevron up icon
9. Building More Features Chevron down icon Chevron up icon
10. Deployment Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(7 Ratings)
5 star 14.3%
4 star 14.3%
3 star 28.6%
2 star 42.9%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




TIffany York Dec 03, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have only gotten through the first chapter and I am quite happy with it. I have read a lot regarding APIs but having a book that walked me through the creation of one helped solidify the knowledge. Context: I am a Network Engineer trying to break into network automation.
Amazon Verified review Amazon
Yosef Cohen Jun 15, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book is good , but there are small mistakes here and there. There is a problem too that when you upload your app to heroku it don't support uploading images , and I was very disappointed from it.
Amazon Verified review Amazon
Phillip F Norris Jun 23, 2023
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I like that the book provides the necessary ingredients for building a website from scratch, but one of the libraries that it uses extensively has evolved. Of course, I could have used the library versions they use (that are a few years old at this point, if they are still available), but I've wanted to learn the current tech. Until now, I've been able to figure out what needs to change in order to work, like jwt_optional has become jwt_required(optional=True). The internet is not overflowing with those types of corrections, and the library's documentation doesn't connect the dots without effort . At the moment, I have no idea on the current implementation of get_raw_jwt. Would be great if the authors provided updates on their code page.
Amazon Verified review Amazon
Shun-Chi Lo Oct 14, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I found this book is useful and well structured, but some sample codes have errors. Maybe it's good for debugging practice but can take you some time.
Amazon Verified review Amazon
Anthony K. Jan 27, 2020
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I was very excited to receive this book, but when I started reading, I found that most illustrations in the book are blurry! a little bit disappointed!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.