Develop Chat UI with Streamlit

Python

Published at: 5/11/2025

Creating Web Applications with Streamlit

Streamlit is a powerful framework that allows you to create web applications easily using Python. It's particularly useful for data scientists and machine learning engineers.

Key Features of Streamlit

  • Create interactive web applications with just a few lines of code
  • Easy data visualization
  • Real-time updates
  • Development using only Python knowledge

Installation

pip install streamlit

Creating a Simple Chat UI

Here's a sample code to create a simple chat interface:

import streamlit as st
 
# Page configuration
st.set_page_config(page_title="Sample Chat", page_icon="💬")
 
# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = []
 
# Title
st.title("Sample Chat")
 
# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])
 
# User input
if prompt := st.chat_input("Enter your message"):
    # Add user message
    st.session_state.messages.append({"role": "user", "content": prompt})
    
    # Display user message
    with st.chat_message("user"):
        st.write(prompt)
    
    # Bot response (simple echo in this example)
    bot_response = f"Your message: {prompt}"
    
    # Add bot message
    st.session_state.messages.append({"role": "assistant", "content": bot_response})
    
    # Display bot message
    with st.chat_message("assistant"):
        st.write(bot_response)

Running the Application

streamlit run app.py

Key Features Explained

  1. st.session_state: Functionality to maintain application state
  2. st.chat_message(): Container for displaying chat messages
  3. st.chat_input(): Display chat input field

Conclusion

Streamlit allows you to create interactive web applications easily using just Python knowledge. It can be used for various purposes such as data visualization and machine learning model demonstrations.

References