8 Building a CRUD app with Streamlit

 

This chapter covers

  • Setting up a relational database for persistent storage
  • Performing CRUD operations using SQL
  • Developing a multi-page Streamlit app
  • Creating shared database connections in a Streamlit app
  • Authenticating users

In 1957, science fiction author Theodore Sturgeon famously said, "Ninety percent of everything is crud." While this was originally a cynical defense of the science fiction genre—the point being that it was no different from anything else in that regard—the adage has since taken on a different meaning, becoming the worst-kept secret in software engineering: Ninety percent of everything is CRUD.

By CRUD, I'm referring to Create, Read, Update, and Delete, the four mundane operations that appear repeatedly in almost any notable piece of software.

Think about it. Social media platforms like Facebook revolve around creating posts, reading feeds, updating profiles, and deleting content. E-commerce sites manage products, orders, customer accounts, and reviews through similar operations. Even something as simple as Notepad on Windows centers around creating, reading, updating, and deleting text files.

Mastering CRUD operations is essential for building a strong foundation in software design, as their implementation often involves tackling non-trivial challenges. In this chapter, we'll create a CRUD application with Streamlit, implementing these operations from scratch while covering related topics such as user authentication.

8.1 Haiku Haven: A CRUD app in Streamlit

8.1.1 Stating the concept and requirements

8.1.2 Visualizing the user experience

8.1.3 Brainstorming the implementation

8.2 Setting up persistent storage

8.2.1 Relational database concepts

8.2.2 Haiku Haven's data model

8.2.3 PostgreSQL: A real relational database

8.2.4 A crash course in SQL

8.2.5 Connecting PostgreSQL to our app

8.3 Creating user accounts

8.3.1 Splitting our app into services

8.3.2 Creating the user service

8.4 Setting up a multi-page login flow

8.4.1 Multi-page apps in Streamlit

8.4.2 Implementing login

8.4.3 Navigating between pages

8.5 Creating, reading, updating, and deleting haikus

8.5.1 Defining a HaikuService class

8.5.2 Enabling users to create haikus

8.5.3 The other CRUD operations: Read, Update, Delete

8.6 Deploying Haiku Haven

8.6.1 Setting up a managed PostgreSQL server in production

8.6.2 Deploying to Community Cloud

8.7 Summary