Skip to main content

Streamlit Chat Handler

Streamlit Chat Handler is a Python library designed to manage and render chat elements within a Streamlit application. This library simplifies the process of creating, storing, and dynamically rendering messages in a chat interface, supporting interactive communication scenarios in Streamlit.

Features

  • Session Management: Uses a singleton pattern to ensure a unique chat handler instance per session.
  • Dynamic Rendering: Seamlessly append and render chat elements as user or assistant within the Streamlit session.
  • Customizable Chat Elements: Supports different types of messages, such as text and markdown, with the flexibility to pass additional arguments and keyword arguments for rendering.

Installation

You can install the Streamlit Chat Handler via pip:

pip install streamlit-chat-handler

Quick Start

Here's a quick example to get started with using Streamlit Chat Handler in your Streamlit app:

import uuid
from time import sleep
import streamlit as st
from streamlit_chat_handler import StreamlitChatHandler

# Initialize session
if "session_id" not in st.session_state:
    st.session_state["session_id"] = str(uuid.uuid4())

# Create a chat handler instance
chat_handler = StreamlitChatHandler(
    st.session_state,
    session_id=st.session_state["session_id"],
).render()

# Process user input
if prompt := st.chat_input("What is up?"):
    chat_handler.append(role="user", type="markdown", content=prompt, render=True)

    with st.spinner("Processing..."):
        sleep(1)
        chat_handler.append(role="assistant", type="markdown", content="answer", render=True)

Documentation

Class: StreamlitChatHandler

This class handles the state and rendering of chat elements within a Streamlit session. It is designed to manage chat elements dynamically, maintaining a unique instance per session through a singleton pattern.

Methods

  • append: Adds a new chat element to the session.
  • render: Renders all chat elements stored in the session.
  • render_last: Renders the last added chat element.

Class: StreamlitChatElement

Defines the structure of a chat element, handling its rendering through Streamlit's API.

Attributes

  • role: Specifies whether the message is from a user or an assistant.
  • type: Defines the Streamlit widget type for rendering (text, markdown, etc.).
  • content: The content to be rendered, depends on the type.

Method

  • render: Renders the chat element using the specified Streamlit widget.

Contributing

Contributions are welcome! Please fork the repository and submit pull requests to contribute.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for streamlit-chat-handler 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-chat-handler 0.1.4
File Size Uploaded
streamlit_chat_handler-0.1.4.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-chat-handler 0.1.4
File Interpreter ABI Platform
streamlit_chat_handler-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / streamlit_chat_handler-0.1.4.tar.gz

Download URL streamlit_chat_handler-0.1.4.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
24e47f410058a2e124d224e7d6f9d9eb1d498e992a16e44432bcf3741da62d7d
BLAKE2b-256 checksum
How to use checksums
163ec1a8183439a97a08ca4adb5947b8b4dc19ea710a96fd58da1bf842ed3849
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.7

Release files / streamlit_chat_handler-0.1.4-py3-none-any.whl

Download URL streamlit_chat_handler-0.1.4-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d7b5c2ad60c5efed05b46f6265bc5ffd07d1be27bb9d216a401ebd5f47c22ad
BLAKE2b-256 checksum
How to use checksums
dc6e8f3e9d376e84052ad4c19bbcc2343f9c15f96d6bf2a91890b57e6c289d45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page