CRUD Application with Python & Flask + Example

CRUD Application with Python & Flask + Example

Create, Read, Update and Delete items using the back-end framework Flask.

What is Flask?

Flask is a micro-framework in Python. It makes it easier to start a server, and when combined with other modules it can build sophisticated and complex applications.

But why is it a micro-framework?? Well, it's considered a micro-framework because it doesn't need any particular tool or libraries in order to work properly.

Flask is a great tool to work with back-end applications, and it's widely used for big companies, such as Reddit, Airbnb, Red Hat, Mozilla, and others.

What is a CRUD Application?

CRUD Applications are the applications that are able to:

  • Create- like adding a book to a catalog.
  • Read- being able to see all the books in the catalog.
  • Update- change a book's name.
  • Delete- delete a book from the catalog.

All of these actions seem to be simple when separated, but they add very important functionality to the application and can be a little bit tricky to implement in the beginning.

These 4 'simple' actions are vital to modern web development, once almost (if not all) big applications like social networks and streaming services are all using them and could not exist without them!

Key Definitions

  • Resource- A noun that we operate on. An application can have many resources, but each resource will have its own set of routes and CRUD operations. Users and Tweets are both different resources of Twitter.
  • HTTP- The protocol that clients and servers use to communicate through with its request method:
    • GET- Used for retrieving information or sending information via the query string.
    • POST- Used for sending information via the body of the request.
    • PUT- Used of updating an entire resource.
    • PATCH- Used for updating a part of a resource.
    • DELETE- Used for deleting a resource.
  • Idempotent- An operation is idempotent if calling it multiple times yields the same result. GET requests should be Idempotent.

GET vs POST

GET is usually faster.

POST is always more secure if the information isn't transmitted in the query string.

GET is Idempotent, but POST is not.

Example

Check out on Github an example of CRUD application that can create snacks (Create), show all of the snacks in the list (Read), change some details about the snack (Update), and delete them (Delete).

Link: Github repository


0.jpeg localhost:5000/snacks


1.jpeg localhost:5000/snacks/create


2.jpeg localhost:5000/snacks/banana


3.jpeg localhost:5000/snacks/banana/edit