系列目录
- 学习streamlit-1,简介
- 学习streamlit-2,st.write输出
- 学习streamlit-3,其它输出显示方法
- 学习streamlit-4,滑块
- 学习streamlit-5,图表
- 学习streamlit-6,选项框
- 学习streamlit-7,复选框
- 学习streamlit-8,组件
- 学习streamlit-9,密钥
- 学习streamlit-10,文件上传
- 学习streamlit-11,股票蜡烛图
- 学习streamlit-12,页面布局
- 学习streamlit-13,BMI计算应用
Streamlit Components
今天学习streamlit中定制组件(插件)的使用。
streamlit支持组件扩展,允许开发者开发自己的组件来扩展功能。组件是开发者编写的第三方python模块。
目前已经有大量第三方组件可供使用,比如streamlit官方收录的典型组件和streamlit开发者Fanilo维护的组件列表。
那我们该如何使用这些组件呢?非常简单,只需要pip install
。
接下来我们演示一个非常常用的组件,streamlit_pandas_profiling
的使用。
首先,我们需要安装这个组件:
pip install streamlit_pandas_profiling
代码:
import streamlit as st
import pandas as pd
import pandas_profiling
from streamlit_pandas_profiling import st_profile_report
st.header('`streamlit_pandas_profiling`')
df = pd.read_csv('https://raw.githubusercontent.com/dataprofessor/data/master/penguins_cleaned.csv')
pr = df.profile_report()
st_profile_report(pr)
这段代码使用 pandas-profiling
库来生成一个数据报告,并将其在 Streamlit 应用程序中展示。
首先,导入了必要的库:Streamlit、Pandas 和 pandas-profiling。接下来,使用 st.header()
函数创建了一个标题,标题文本为 “streamlit_pandas_profiling”。
然后,使用 pd.read_csv()
函数从远程 CSV 文件加载数据集,并将其存储在名为 df
的 Pandas DataFrame 中。这个数据集是关于企鹅种类的,包含了每个企鹅的生理特征和物种分类。
接下来,使用 profile_report()
函数来生成一个数据报告。该函数会对 DataFrame 进行描述性统计分析、缺失值和重复数据的检测,并生成一份详细的 HTML 报告。生成的报告包含有关数据的各种信息,例如每个变量的统计数据、相关性、分布等。
最后,使用 st_profile_report()
函数将生成的报告展示在 Streamlit 应用程序中。这个函数可以将 pandas-profiling 生成的报告直接展示在 Streamlit 应用程序中,而不需要额外的 HTML 代码。
可以点击下方按钮查看程序运行效果:
这个应用程序是一个简单的演示,它展示了如何在 Streamlit 中使用 pandas-profiling 库来生成数据报告,并将报告展示在 Web 应用程序中。它可以帮助数据分析人员更轻松地了解他们的数据集,发现数据集中的问题,并快速生成有关数据集的详细信息。
可以通过下面两个视频来学习如何创建自定义组件来扩展streamlit功能。
How to build a Streamlit component - Part 1: Setup and Architecture
How to build a Streamlit component - Part 2: Make a Slider Widget
公众号 | FunIO
微信搜一搜 “funio”,发现更多精彩内容。