chapter eight

8 Building a Web UI with Streamlit

 

This chapter covers

  • What Streamlit is and how it transforms Python scripts into web applications
  • Building your first Streamlit app with a Hello World example
  • Using core components: text display, user input, buttons, sidebars, and layouts
  • Running Streamlit apps and understanding the development workflow

You have been building toward a voice-powered AI chat application that runs entirely on your own machine. You have the LLM backend (Ollama), the Python API layer, and soon you will add speech recognition. But you still need a way for users to interact with all of this -- a web interface. That is what Streamlit provides.

Traditional web development requires learning HTML, CSS, and JavaScript -- three separate languages that work together to create the structure, styling, and interactivity of a web page. For beginners, this means months of learning before producing anything meaningful. Streamlit eliminates that barrier entirely. You write Python -- just Python -- and Streamlit converts your code into a fully interactive web application.

8.1 What Is Streamlit?

Streamlit is a Python framework that converts Python scripts into interactive web applications. Instead, you call Python functions like st.title() and st.text_input(), and Streamlit automatically generates the web page for you.

Figure 8.1 shows what happens behind the scenes when you run a Streamlit app:

8.2 Your First Streamlit App

8.2.1 Step 1: Install Streamlit

8.2.2 Step 2: Create the App File

8.2.3 Step 3: Run the App

8.2.4 Step 4: Add Interactivity

8.2.5 How Streamlit's Execution Model Works

8.3 Core Streamlit Components

8.3.1 Text Display

8.3.2 User Input

8.3.3 Buttons and Actions

8.3.4 The Sidebar

8.3.5 Columns Layout

8.3.6 Components Demo

8.4 Running Streamlit Apps

8.4.1 Starting and Stopping

8.4.2 Auto-Reload

8.4.3 Ports and Multiple Apps

8.4.4 The Development Workflow

8.4.5 Helpful Command-Line Options

8.5 Putting It All Together

8.6 Summary

8.7 Exercises