# 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:
```bash
pip install streamlit-chat-handler
```
## Quick Start
Here's a quick example to get started with using `Streamlit Chat Handler` in your Streamlit app:
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "streamlit-chat-handler",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "sample, setuptools, development",
"author": null,
"author_email": "Eduardo Messias de Morais <emdemor415@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/16/3e/c1a8183439a97a08ca4adb5947b8b4dc19ea710a96fd58da1bf842ed3849/streamlit_chat_handler-0.1.4.tar.gz",
"platform": null,
"description": "# Streamlit Chat Handler\n\nStreamlit 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.\n\n## Features\n\n- **Session Management**: Uses a singleton pattern to ensure a unique chat handler instance per session.\n- **Dynamic Rendering**: Seamlessly append and render chat elements as user or assistant within the Streamlit session.\n- **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.\n\n## Installation\n\nYou can install the `Streamlit Chat Handler` via pip:\n\n```bash\npip install streamlit-chat-handler\n```\n\n## Quick Start\n\nHere's a quick example to get started with using `Streamlit Chat Handler` in your Streamlit app:\n\n```python\nimport uuid\nfrom time import sleep\nimport streamlit as st\nfrom streamlit_chat_handler import StreamlitChatHandler\n\n# Initialize session\nif \"session_id\" not in st.session_state:\n st.session_state[\"session_id\"] = str(uuid.uuid4())\n\n# Create a chat handler instance\nchat_handler = StreamlitChatHandler(\n st.session_state,\n session_id=st.session_state[\"session_id\"],\n).render()\n\n# Process user input\nif prompt := st.chat_input(\"What is up?\"):\n chat_handler.append(role=\"user\", type=\"markdown\", content=prompt, render=True)\n\n with st.spinner(\"Processing...\"):\n sleep(1)\n chat_handler.append(role=\"assistant\", type=\"markdown\", content=\"answer\", render=True)\n\n```\n\n## Documentation\n\n### Class: StreamlitChatHandler\n\nThis 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.\n\n#### Methods\n\n- `append`: Adds a new chat element to the session.\n- `render`: Renders all chat elements stored in the session.\n- `render_last`: Renders the last added chat element.\n\n### Class: StreamlitChatElement\n\nDefines the structure of a chat element, handling its rendering through Streamlit's API.\n\n#### Attributes\n\n- `role`: Specifies whether the message is from a user or an assistant.\n- `type`: Defines the Streamlit widget type for rendering (`text`, `markdown`, etc.).\n- `content`: The content to be rendered, depends on the `type`.\n\n#### Method\n\n- `render`: Renders the chat element using the specified Streamlit widget.\n\n## Contributing\n\nContributions are welcome! Please fork the repository and submit pull requests to contribute.\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n",
"bugtrack_url": null,
"license": null,
"summary": "...",
"version": "0.1.4",
"project_urls": null,
"split_keywords": [
"sample",
" setuptools",
" development"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "dc6e8f3e9d376e84052ad4c19bbcc2343f9c15f96d6bf2a91890b57e6c289d45",
"md5": "56cb35440a16df0641e35d98a6d5c826",
"sha256": "7d7b5c2ad60c5efed05b46f6265bc5ffd07d1be27bb9d216a401ebd5f47c22ad"
},
"downloads": -1,
"filename": "streamlit_chat_handler-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "56cb35440a16df0641e35d98a6d5c826",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 8908,
"upload_time": "2024-04-29T02:58:31",
"upload_time_iso_8601": "2024-04-29T02:58:31.411559Z",
"url": "https://files.pythonhosted.org/packages/dc/6e/8f3e9d376e84052ad4c19bbcc2343f9c15f96d6bf2a91890b57e6c289d45/streamlit_chat_handler-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "163ec1a8183439a97a08ca4adb5947b8b4dc19ea710a96fd58da1bf842ed3849",
"md5": "5701adb21752fec4adbe1056a899889e",
"sha256": "24e47f410058a2e124d224e7d6f9d9eb1d498e992a16e44432bcf3741da62d7d"
},
"downloads": -1,
"filename": "streamlit_chat_handler-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "5701adb21752fec4adbe1056a899889e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 8598,
"upload_time": "2024-04-29T02:58:33",
"upload_time_iso_8601": "2024-04-29T02:58:33.372700Z",
"url": "https://files.pythonhosted.org/packages/16/3e/c1a8183439a97a08ca4adb5947b8b4dc19ea710a96fd58da1bf842ed3849/streamlit_chat_handler-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-29 02:58:33",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "streamlit-chat-handler"
}