Skip to main content

...

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_chat_handler-0.1.3.dev2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_chat_handler-0.1.3.dev2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_chat_handler-0.1.3.dev2.tar.gz.

File metadata

File hashes

Hashes for streamlit_chat_handler-0.1.3.dev2.tar.gz
Algorithm Hash digest
SHA256 484f629901b8987247aeaf751ce96ac090269171c24c8aa420047b2ec9a5cff3
MD5 35558980f56929b758a67f194f8fe705
BLAKE2b-256 e162accd46b47837cdf1f5ea6de3054e93943ab08fb3e7e7d2ef77fca08f5995

See more details on using hashes here.

File details

Details for the file streamlit_chat_handler-0.1.3.dev2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_chat_handler-0.1.3.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 8d09bf89849364f08043661bd33a2c32fc12a4a54878d0534a85a33508274f43
MD5 ca5eded60d46a87d8dec803254b73ad7
BLAKE2b-256 e04db56a9ebb736dba07d977dbec9829074dda3130ad43f27b8cdf5330a20762

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page