Extra-Streamlit-Components 使用教程
1、项目介绍
Extra-Streamlit-Components
是一个开源项目,旨在为 Streamlit 提供额外的复杂或默认不可用的组件。该项目由 Mohamed Abdou 开发,支持 Python 3.6 及以上版本,并采用 MIT 许可证。通过这个项目,开发者可以轻松地扩展 Streamlit 的功能,添加自定义组件,如标签栏、动态图像等。
2、项目快速启动
安装
首先,通过 pip 安装 extra-streamlit-components
:
pip install extra-streamlit-components
示例代码
以下是一个简单的示例,展示如何在 Streamlit 应用中使用 extra-streamlit-components
:
import streamlit as st
import extra_streamlit_components as stx
# 创建标签栏
chosen_id = stx.tab_bar(data=[
stx.TabBarItemData(id=1, title="ToDo", description="Tasks to take care of"),
stx.TabBarItemData(id=2, title="Done", description="Tasks taken care of"),
stx.TabBarItemData(id=3, title="Overdue", description="Tasks missed out")
], default=1)
# 显示选中的标签
st.info(f"Chosen ID: {chosen_id}")
# 显示动态图像
image_urls = [
"https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.svg",
"https://streamlit.io/images/brand/streamlit-logo-secondary-lightmark-lighttext.png",
"https://ruslanmv.com/assets/images/posts/2020-10-17-Web-Application-Classification/streamlit_logo.png"
]
stx.bouncing_image(image_source=image_urls[int(chosen_id) - 1], animate=True, animation_time=1500, height=200, width=600)
3、应用案例和最佳实践
应用案例
- 任务管理应用:使用标签栏组件来管理不同状态的任务(如待办、已完成、逾期)。
- 动态图像展示:利用动态图像组件展示不同状态下的图像,增强用户交互体验。
最佳实践
- 模块化设计:将自定义组件封装成独立的模块,便于管理和复用。
- 缓存优化:使用
st.cache_resource
装饰器缓存资源,提高应用性能。
4、典型生态项目
- Streamlit-Authenticator:一个用于 Streamlit 应用的身份验证组件,增强应用的安全性。
- Streamlit-Aggrid:提供在 Streamlit 中使用 Ag-Grid 的功能,增强数据表格的交互性。
通过这些生态项目,可以进一步扩展 Streamlit 的功能,构建更强大和丰富的应用。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考