liveBook · Manning

Streamlit

As explained in AI Agents in Action

Streamlit is an open-source Python framework for rapidly building interactive web applications for data science and machine learning.

Streamlit is a Python framework for rapidly prototyping interactive web interfaces, especially machine learning dashboards and chat applications. It produces a modern React-powered interface and supports quick cloud or standalone deployment. section 7.2

A practical Streamlit chat application follows this flow:

  • Define interface elements with the st. prefix, including titles, chat messages, inputs, and spinners. section 7.2.1
  • Store the model choice and conversation messages in st.session_state, which lasts only for the browser session. section 7.2.1
  • Handle submitted prompts, call the API, display the response, and append both sides of the conversation to message history. section 7.2.1

Streaming chat

A responsive chat interface passes stream=True to the API and sends the result to st.write_stream, displaying generated tokens in real time instead of waiting for the complete response. section 7.2.2

Listing 7.4 chatgpt_clone_response.py (bottom section)
if prompt := st.chat_input("What do you need?"):     #1
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):     #2
        st.markdown(prompt)

    with st.spinner(text="The assistant is thinking..."):    #3
        with st.chat_message("assistant"):
            response = client.chat.completions.create(
                model=st.session_state["openai_model"],
                messages=[
                    {"role": m["role"], "content": m["content"]}
                    for m in st.session_state.messages
                ],      #4
            )
            response_content = response.choices[0].message.content
            response = st.markdown(response_content,
             unsafe_allow_html=True)      #5
    st.session_state.messages.append(
{"role": "assistant", "content": response_content})      #6

The listing shows prompt submission, session-history updates, a waiting spinner, API completion, and response rendering in one runnable interaction. section 7.2.1

How AI Agents in Action covers it

How deep it goes
Explains controls, reruns, temporary session state, chat flow, debugging, and streaming, without detailing long-term database storage.
Examples
Uses a ChatGPT-like clone, prompt input, message history, a waiting spinner, and streamed responses.
What it assumes
Assumes a prepared Python and VS Code environment while teaching Streamlit controls and state usage directly through examples.
What you can do after
Build it from scratch: create basic Python interfaces, connect chat controls to an API, debug execution, and stream responses.
How it connects
Connects Python with React-powered interfaces, session state, chat applications, APIs, and streaming model responses.

Also explained in

Compare how 9 books explain Streamlit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.