Objectives
By the end of this module, you will be able to:- Understand the role of Streamlit in rapid frontend prototyping.
- Connect a frontend application to a hosted FastAPI backend running at
https://api.domain.com. - Perform full CRUD operations (Create, Read, Update, Delete) from a GUI.
- Use the
requestslibrary in Python to communicate with REST APIs. - Build interactive tables, input forms, and status notifications in Streamlit.
Topics Covered
In this module, you’ll learn:- Introduction to Streamlit
- How the Frontend Interacts with the Backend
- Step-by-Step Streamlit App Code
- Running the Frontend Locally
Try Yourself: 💻 VS Code | 🚀 Colab | 📥 Download
Verify Solutions: 💻 VS Code | 🚀 Colab | 📥 Download
1. Introduction to Streamlit
Streamlit is an open-source Python framework that allows developers to build interactive web applications quickly, without needing to write HTML, CSS, or JavaScript. For backend developers, it is a powerful tool to:- Create functional admin panels or customer portals.
- Build user interfaces to demo APIs.
- Visualize database records dynamically.
Streamlit Fundamentals
To build effective user interfaces with Streamlit, you must understand a few core concepts:1. The Execution Model
Unlike standard web frameworks that use callback events, Streamlit operates on a top-to-bottom execution model.- Every time a user interacts with a widget (clicks a button, selects a dropdown, types text), Streamlit reruns the entire Python script from top to bottom.
- Streamlit maintains the states of input fields automatically across these reruns.
2. Basic UI & Display Elements
st.write(): The “Swiss Army knife” of Streamlit. It can print text, formatted markdown, tables, or dictionaries.st.title()/st.header()/st.subheader(): Used to create structured text headings.st.divider(): Draws a clean horizontal separator line.
3. Input Widgets (User Actions)
Widgets allow users to pass parameters to your app:st.text_input("Label"): Returns the text typed by the user as a Python string.st.number_input("Label"): Captures numbers (with min/max constraints).st.selectbox("Label", options_list): Renders a dropdown menu and returns the selected item.st.button("Click Me"): ReturnsTrueonly during the rerun triggered by the user clicking it.
4. Layouts & Containers
st.columns([weights]): Divides the screen horizontally into parallel sections.st.tabs([names]): Groups elements into tabs to keep the UI clean.st.form("form_name"): Groups input fields together so that the page doesn’t rerun until the user clicks the “Submit” button inside the form.
5. Feedback Callouts
st.success("Message")/st.error("Message"): Displays colored alert banners.st.toast("Short message"): Shows a brief self-dismissing pop-up toast in the corner.st.rerun(): Programmatically forces the app to immediately rerun from the top (useful after updating or deleting records).
2. How the Frontend Interacts with the Backend
Our frontend application is completely decoupled from the FastAPI backend. It runs as a separate process and communicates via HTTP requests: We will use Python’s popularrequests library to trigger the endpoints of our deployed API.
3. Step-by-Step Streamlit App Code
Create a new folder namedstudent_frontend/ and initialize it with uv:
app.py in the root of the frontend project:
4. Running the Frontend Locally
Start the Streamlit application using theuv tool runner:
Practice
To reinforce what you’ve learned in this section, practice with the interactive follow-along notebook:Follow-Along Practice
Practice setting up Streamlit application frames, columns layout structures, input widget variables, callout feedback controls, and connecting requests backends.💻 VS Code | 🚀 Colab | 📥 Download
Summary
You have successfully:- Initialized a Streamlit project using
uv. - Connected Python actions directly to the cloud backend at api.domain.com.
- Implemented user input forms, selectable tables, and delete buttons.
- Triggered backend CRUD operations safely.