liveBook · Manning

Chapter 8: Building a CRUD app with Streamlit

What you will learn in chapter 8 of Build Python Web Apps with Streamlit

This chapter introduces CRUD as the common create, read, update, and delete pattern and previews building a Streamlit CRUD app with persistent storage and user authentication.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces CRUD as the common create, read, update, and delete pattern and previews building a Streamlit CRUD app with persistent storage and user authentication.

Key terms: CRUD, Create, Read, Update, Delete, relational database, persistent storage, SQL, multi-page Streamlit app, shared database connections

8.1 Haiku Haven: A CRUD app in Streamlit

Introduces Haiku Haven, a Streamlit app where users manage their own valid haikus through the four CRUD operations.

Key terms: Haiku Haven, CRUD app, haiku, 5-7-5 syllable rule, CRUD, create, read, update, delete

8.1.1 Stating the concept and requirements

Defines Haiku Haven as a private, account-based haiku manager, specifies its essential CRUD requirements, and limits the chapter's scope to core workflows.

Key terms: Haiku Haven, username, password, CRUD, create, read, data, update, delete, out of scope

8.1.2 Visualizing the user experience

This section sketches Haiku Haven’s multi-page user experience for signup, authentication, and managing haikus.

Key terms: multi-page app, account creation flow, login, logout, CRUD, My haikus page, password, authentication, signup page, login page

8.1.3 Brainstorming the implementation

This section outlines Haiku Haven’s three-part architecture: a Streamlit frontend, a backend divided by responsibility, and a database for permanent storage and retrieval.

Key terms: frontend, backend, database, Figure 8.2, widgets, corresponding function, user actions, creating an account, authenticating a user, haiku-related actions

8.2 Setting up persistent storage

This section identifies persistent storage as necessary for Haiku Haven and chooses a relational database to keep users' haikus beyond an app session.

Key terms: persistent storage, store data outside of the app, Haiku Haven, relational database, data storage

8.2.1 Relational database concepts

This section introduces relational databases as persistent, table-based storage structured by schemas and accessed with SQL.

Key terms: relational database, table, row, record, column, field, pandas dataframes, in memory, persistent storage, database

8.2.2 Haiku Haven’s data model

This section models Haiku Haven’s users and haikus as a relational database schema with a one-to-many foreign-key relationship.

Key terms: entities, relationships, attributes, relational database schema, User, Haiku, username, password, text, author

8.2.3 PostgreSQL: A real relational database

This section introduces PostgreSQL as Haiku Haven’s transactional relational database and walks through installing it locally, creating a limited app user and database, and connecting through psql.

Key terms: PostgreSQL, BigQuery, relational database, cloud-based data warehouse, transactional use cases, analytical use cases, PostgreSQL installer, psql, bin directory, environment variables

8.2.4 A crash course in SQL

This section introduces the SQL commands used to create Haiku Haven’s tables and perform basic create, read, update, and delete operations on their rows.

Key terms: SQL, Structured Query Language, CREATE TABLE, INSERT INTO, SELECT, UPDATE, VARCHAR(100), TEXT, PRIMARY KEY, SERIAL

8.2.5 Connecting PostgreSQL to the app

This section connects Haiku Haven to PostgreSQL by encapsulating Streamlit’s SQL connection in a reusable Database class, configuring credentials securely, and explaining how the connection is shared across users.

Key terms: st.connection, type="sql", SQLAlchemy, psycopg2-binary, PostgreSQL adapter, Database class, __init__, self.conn, execute_query, query

8.3 Creating user accounts

This section introduces user accounts for Haiku Haven, secure password storage, and the upcoming discussion of code organization.

Key terms: Haiku Haven accounts, haikus, passwords, securely, code organization

8.3.1 Splitting the app into services

This section organizes Haiku Haven’s backend into focused service classes coordinated by a Hub rather than one growing monolithic backend class.

Key terms: UserService, HaikuService, service classes, Hub, frontend, backend, coordinator, monolithic approach, separation of concerns, modular

8.3.2 Creating the user service

This section builds secure user creation for Haiku Haven by hashing passwords, encapsulating database access in services, and wiring a Streamlit signup page.

Key terms: User, password_hash, cryptographic hash function, bcrypt, hash_password, authenticate, dataclass, username, @staticmethod, UserService

8.4 Setting up a multi-page login flow

This section introduces Haiku Haven’s planned multi-page user flow, with separate signup, login, and haiku-functionality pages.

Key terms: multi-page app, signup, login, haiku-related functionality

8.4.1 Multi-page apps in Streamlit

This section converts Haiku Haven into a multi-page Streamlit app by routing between login and signup pages while sharing the Hub through session state.

Key terms: multi-page apps, entrypoint file, router, main.py, frontend/signup.py, st.Page, st.navigation, Page objects, .run(), pages dictionary

8.4.2 Implementing login

This section adds UserService authentication and a Streamlit login page that records a successful login in session state.

Key terms: UserService, get_authenticated_user, get_user, authenticate, User, None, login.py, Log In, st.session_state, logged_in

8.4.3 Navigating between pages

This section completes Haiku Haven’s signup, login, and logout navigation by linking pages, changing navigation by session state, and rerunning after authentication changes.

Key terms: st.navigation, logged_in, st.session_state, pages dictionary, st.page_link, st.Page, login page, signup page, st.rerun, home.py

8.5 Creating, reading, updating, and deleting haikus

This section introduces the core Haiku Haven work of implementing haiku CRUD operations, beginning with creation through a HaikuService and corresponding frontend changes.

Key terms: create, read, update, and delete, CRUD, haikus, HaikuService, frontend, haiku creation

8.5.1 Defining a HaikuService class

This section defines the Haiku data model and service layer for creating haikus, then exposes that service to the frontend through the application hub.

Key terms: Haiku dataclass, haikus table, haiku.py, backend/ folder, HaikuService, create_haiku, SQL query, author, text, haiku_id

8.5.2 Enabling users to create haikus

This section adds a modal haiku editor that saves a logged-in user's text through HaikuService and reruns the app after a successful save.

Key terms: haiku_editor, st.dialog, modal dialog, st.text_area, Save Haiku, HaikuService, create_haiku, database, success/failure message, st.rerun

8.5.3 The other CRUD operations: Read, Update, Delete

This section completes Haiku Haven’s CRUD functionality by listing a user’s haikus and enabling them to edit or delete each one.

Key terms: HaikuService, get_haikus_by_author, update_haiku, delete_haiku, haiku_id, UPDATE...SET, display_haiku, Haiku, home.py, st.info

8.6 Deploying Haiku Haven

This section introduces deploying Haiku Haven to Community Cloud and identifies its additional need for a running PostgreSQL database server.

Key terms: Haiku Haven, Community Cloud, productionize, PostgreSQL, PostgreSQL server, database, running server

8.6.1 Setting up a managed PostgreSQL server in production

This section sets up a Neon managed PostgreSQL database for the deployed Haiku Haven app and recreates its local schema in production.

Key terms: Streamlit Community Cloud, managed PostgreSQL service, Neon, project name, database name, connection string, haikudb_owner, username, password, SQL Editor

8.6.2 Deploying to Community Cloud

This subsection deploys Haiku Haven to Streamlit Community Cloud by declaring dependencies and configuring production database secrets.

Key terms: requirements.txt, Streamlit Community Cloud, psycopg2-binary, bcrypt, secrets.toml, Secrets setting, connection string, Neon, Community Cloud

8.7 Summary

This summary recaps the CRUD, relational-database, security, Streamlit navigation, and production-deployment foundations used to build the Haiku Haven app.

Where this is used later

Concepts explained in this chapter

← Chapter 7: The CEO strikes back: Supercharging the dashboard Chapter 9: Creating an AI-powered application →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.