liveBook · Manning

Chat_input

As explained in Build Python Web Apps with Streamlit

st.chat_input is a Streamlit widget that collects a conversational message through a bottom-pinned text input and send control.

A chatbot needs a place for people to send messages. `st.chat_input` creates a text box with a Send icon, pinned to the bottom of the screen or its containing widget. Unlike `st.text_input`, it returns a message only when the user clicks Send or presses Enter—not when they click away. section 10.2.3

Send a message to a bot

With a `bot` already available, collect the message, send it to the backend, and show both sides of the exchange: ```python human_message_text = st.chat_input("Chat with me!") if human_message_text: st.chat_message("human").markdown(human_message_text) ai_message_text = bot.chat(human_message_text) st.chat_message("ai").markdown(ai_message_text) ``` The condition keeps the response code tied to a submitted message. `st.chat_message` provides the corresponding human and AI display containers. section 10.2.3

Listing 10.4 A simple chatbot frontend in frontend.py
import streamlit as st
from bot import Bot

if "bot" not in st.session_state:
  api_keys = st.secrets["api_keys"]
  st.session_state.bot = Bot(api_keys)
bot = st.session_state.bot

if human_message_text := st.chat_input("Chat with me!"):
  st.chat_message("human").markdown(human_message_text)
  ai_message_text = bot.chat(human_message_text)
  st.chat_message("ai").markdown(ai_message_text)

The complete small frontend also stores the bot in `st.session_state` and obtains its API keys from `st.secrets`. section 10.2.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines the widget's placement and submission behavior, then wires it into a small chatbot; does not detail widget internals.
Examples
Shows a single-prompt, single-response Streamlit chatbot whose submitted input is passed to `bot.chat`.
What it assumes
Starts with an existing Bot backend while introducing `st.chat_input` and explaining the optional Python walrus operator.
What you can do after
Build it from scratch: connect a Streamlit message field to an existing bot and display the user's message and reply.
How it connects
Connects `st.text_input` behavior, `st.chat_message` display, `st.session_state` bot storage, and a LangGraph-backed response.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.