Article
streamlitpythonui-builderdata-appsrapid-prototypingllm-uidashboarding
Build an Interactive AI App UI with Streamlit in Minutes
Streamlit turns Python scripts into shareable web apps. This guide shows you how to build a simple interactive app with text input, a button, and dynamic output, demonstrating its core reactive nature for AI prototyping.
beginner15 min5 steps
The play
- Install StreamlitInstall the Streamlit library using pip in your terminal. This command downloads and installs the package and its dependencies into your active Python environment.
- Create Your App ScriptCreate a new Python file (e.g., `app.py`). Import the library and add a title. Every Streamlit app starts with `import streamlit as st`. Functions like `st.title()` render directly to the web UI.
- Run Your AppIn your terminal, navigate to the script's directory and run it using the `streamlit run` command. Streamlit starts a local web server and opens your app in a new browser tab. The app will auto-reload when you save changes to the script.
- Add Interactive WidgetsMake your app interactive. Use `st.text_input()` to create a text box and `st.button()` for a submit button. These functions return the user's input and the button's click state, respectively. Streamlit re-runs the script on each interaction.
- Display Dynamic OutputUse an `if` block to check if the button was clicked. If true, use `st.write()` or `st.success()` to display the processed input. This demonstrates Streamlit's reactive model: script re-runs update the UI based on widget state.
Starter code
import streamlit as st
import time
# Set the page title and icon
st.set_page_config(page_title="Echo Bot", page_icon="🤖")
# Display the title of the app
st.title("🤖 Simple Echo Bot")
# Add a header
st.header("Streamlit Interactive Demo")
st.info("Enter text below and click the button to see it echoed back.")
# Get user input using a text input box.
# The first argument is the label, the second is a default value.
user_input = st.text_input("Your message:", "Hello, Streamlit!")
# Create a button. The code inside the if block runs when it's clicked.
if st.button("Echo it!"):
if user_input:
# Display a spinner while "processing"
with st.spinner('Thinking...'):
time.sleep(1) # Simulate a network or model delay
# Use st.chat_message for a conversational look
with st.chat_message("assistant"):
st.write(f"Echo: {user_input}")
else:
# Show a warning if the user clicks the button with no input
st.warning("Please enter some text first.")
# Add a sidebar for extra information (demonstrates layout features)
st.sidebar.header("About")
st.sidebar.write("""
This is a simple demo of Streamlit's core features, including widgets, state management, and reactive updates.
Perfect for building quick AI/ML prototypes.
""")