Connect Streamlit to Tableau

本指南解释了如何从Streamlit社区云安全地访问Tableau上的数据。它使用了tableauserverclient库和Streamlit的Secrets管理

push_pin

注意

如果您已经有一个想要使用的数据库,请随时跳到下一步

为了简化操作,我们在这里使用的是Tableau的云版本,但本指南同样适用于自托管部署。首先,注册Tableau Online或登录。创建一个工作簿或运行“仪表板入门”下的一个示例工作簿。

Tableau screenshot 1

虽然Tableau API允许通过用户名和密码进行身份验证,但在生产应用程序中应使用个人访问令牌

转到您的Tableau Online 主页,创建一个访问令牌并记下令牌名称和密钥。

Tableau screenshot 2
Tableau screenshot 3
push_pin

注意

个人访问令牌如果连续15天未使用将会过期。

您的本地Streamlit应用程序将从应用程序根目录中的文件.streamlit/secrets.toml读取秘密信息。如果此文件尚不存在,请创建此文件,并添加您的令牌、设置期间创建的站点名称以及您的Tableau服务器的URL,如下所示:

# .streamlit/secrets.toml [tableau] token_name = "xxx" token_secret = "xxx" server_url = "https://abc01.online.tableau.com/" site_id = "streamlitexample" # in your site's URL behind the server_url
priority_high

重要

将此文件添加到.gitignore,不要将其提交到您的GitHub仓库!

由于上面的secrets.toml文件没有提交到GitHub,你需要将其内容单独传递给你部署的应用程序(在Streamlit社区云上)。转到应用程序仪表板,在应用程序的下拉菜单中,点击编辑Secrets。将secrets.toml的内容复制到文本区域。更多信息可在Secrets管理中找到。

Secrets manager screenshot

tableauserverclient包添加到您的requirements.txt文件中,最好固定其版本(将x.x.x替换为您想要安装的版本):

# requirements.txt tableauserverclient==x.x.x

将下面的代码复制到您的Streamlit应用程序中并运行它。请注意,此代码仅显示您可以获取的一些数据选项——探索tableauserverclient库以找到更多!

# streamlit_app.py import streamlit as st import tableauserverclient as TSC # Set up connection. tableau_auth = TSC.PersonalAccessTokenAuth( st.secrets["tableau"]["token_name"], st.secrets["tableau"]["personal_access_token"], st.secrets["tableau"]["site_id"], ) server = TSC.Server(st.secrets["tableau"]["server_url"], use_server_version=True) # Get various data. # Explore the tableauserverclient library for more options. # Uses st.cache_data to only rerun when the query changes or after 10 min. @st.cache_data(ttl=600) def run_query(): with server.auth.sign_in(tableau_auth): # Get all workbooks. workbooks, pagination_item = server.workbooks.get() workbooks_names = [w.name for w in workbooks] # Get views for first workbook. server.workbooks.populate_views(workbooks[0]) views_names = [v.name for v in workbooks[0].views] # Get image & CSV for first view of first workbook. view_item = workbooks[0].views[0] server.views.populate_image(view_item) server.views.populate_csv(view_item) view_name = view_item.name view_image = view_item.image # `view_item.csv` is a list of binary objects, convert to str. view_csv = b"".join(view_item.csv).decode("utf-8") return workbooks_names, views_names, view_name, view_image, view_csv workbooks_names, views_names, view_name, view_image, view_csv = run_query() # Print results. st.subheader("📓 Workbooks") st.write("Found the following workbooks:", ", ".join(workbooks_names)) st.subheader("👁️ Views") st.write( f"Workbook *{workbooks_names[0]}* has the following views:", ", ".join(views_names), ) st.subheader("🖼️ Image") st.write(f"Here's what view *{view_name}* looks like:") st.image(view_image, width=300) st.subheader("📊 Data") st.write(f"And here's the data for view *{view_name}*:") st.write(pd.read_csv(StringIO(view_csv)))

看到上面的st.cache_data了吗?如果没有它,Streamlit 每次重新运行应用程序时(例如在小部件交互时)都会运行查询。使用st.cache_data后,它只会在查询更改或10分钟后运行(这就是ttl的作用)。注意:如果您的数据库更新更频繁,您应该调整ttl或删除缓存,以便查看者始终看到最新数据。了解更多信息,请访问Caching

如果一切顺利,你的应用程序应该看起来像这样(可能根据你的工作簿有所不同):

Tableau screenshot 4
forum

还有问题吗?

我们的 论坛 充满了有用的信息和Streamlit专家。