Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22880

Real Python: Python REST APIs With Flask, Connexion, and SQLAlchemy – Part 2

$
0
0

Most modern web applications are powered by a REST API under the hood. That way, developers can separate the front-end code from the back-end logic, and users can interact with the interface dynamically. In this three-part tutorial series, you’re building a REST API with the Flask web framework.

You’ve created a foundation with a basic Flask project and added endpoints, which you’ll connect to a SQLite database. You’re also testing your API with Swagger UI API documentation that you’re building along the way.

In the first part, you used Flask and Connexion to create a REST API providing CRUD operations to an in-memory structure called PEOPLE. By doing so, you learned how the Connexion module helps you build a nice REST API and interactive documentation.

In the second part of this tutorial series, you’ll learn how to:

  • Write SQL commands in Python
  • Configure a SQLite database for your Flask project
  • Use SQLAlchemy to save Python objects to your database
  • Leverage the Marshmallow library to serialize data
  • Connect your REST API with your database

After finishing the second part of this series, you’ll move on to the third part, where you’ll extend your REST API with the functionality to add notes to a person.

You can download the code for the second part of this project by clicking the link below:

Source Code:Click here to download the free source code that you’ll use to continue building a REST API with the Flask web framework.

Demo

In this three-part tutorial series, you’re building a REST API to keep track of notes for people that may visit you throughout the year. You’ll create people like the Tooth Fairy, the Easter Bunny, and Knecht Ruprecht.

Ideally, you want to be on good terms with all three of them. That’s why you’ll send them notes, to increase the chance of getting valuable gifts from them.

You can interact with your application by leveraging the API documentation. Along the way, you’re also building a basic front end that reflects the contents of your database:

In the second part of this series, you’ll enhance the back end of your application by adding a proper database. That way, you’ll persist your data even when you restart your app:

With your Swagger UI documentation, you’ll be able to interact with your REST API and make sure that everything works as intended.

Planning Part Two

In the first part of this tutorial series, you worked with a PEOPLE dictionary to store your data. The dataset looked like this:

PEOPLE={"Fairy":{"fname":"Tooth","lname":"Fairy","timestamp":"2022-10-08 09:15:10",},"Ruprecht":{"fname":"Knecht","lname":"Ruprecht","timestamp":"2022-10-08 09:15:13",},"Bunny":{"fname":"Easter","lname":"Bunny","timestamp":"2022-10-08 09:15:27",}}

This data structure was handy to get your project up to speed. However, any data that you added with your REST API to PEOPLE got lost when you restarted your app.

In this part, you’ll be translating your PEOPLE data structure into a database table that’ll look like this:

idlnamefnametimestamp
1FairyTooth2022-10-08 09:15:10
2RuprechtKnecht2022-10-08 09:15:13
3BunnyEaster2022-10-08 09:15:27

You won’t make any changes to your REST API endpoints in this tutorial. But the changes that you’ll make in the back end will be significant, and you’ll end up with a much more versatile codebase to help scale your Flask project up in the future.

Getting Started

In this section, you’ll check in with the Flask REST API project that you’re working on. You want to make sure that it’s ready for the next steps in this tutorial series.

Read the full article at https://realpython.com/flask-connexion-rest-api-part-2/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 22880

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>