Getting started with Streamlit in Snowflake¶
This topic walks you through deploying your first Streamlit in Snowflake app in under five minutes using a container runtime. After that, two hands-on examples show you how to build real apps that query data, personalize the experience for each viewer, and write back to Snowflake.
先决条件
Before you can create a Streamlit app, ensure that your administrator has completed the essential security setup for Streamlit apps.
您的角色必须拥有以下权限:
| Privilege | Object | Notes |
|---|---|---|
| USAGE | Database where you create the Streamlit app | |
| CREATE STREAMLIT, USAGE | Schema where you create the Streamlit app | |
| USAGE | Compute pool that runs the Streamlit app | For all accounts, Snowflake configures a general-purpose compute pool that typical users will have access to. For more information, see Configuring your own preferred compute pools for Streamlit apps. |
| USAGE | Warehouse that runs queries in the Streamlit app |
For more information, see Privileges required to create and use a Streamlit app.
Deploy your first Streamlit in Snowflake app¶
最快速的入门方式是使用默认的入门代码创建一个 Streamlit 应用。当您在不指定源文件的情况下创建应用程序时,Snowflake 会自动提供示例代码。
- Sign in to Snowsight.
- In the navigation menu, select Projects » Streamlit.
- Select + Streamlit App.
- 输入应用程序的名称。
- 选择要在其中创建应用程序的数据库和架构。
- Select Run on container.
- 选择计算池和查询仓库。
- Select Create.
Snowsight redirects you to the app editor. Your app will be ready within a few minutes. Then, you can view and edit it immediately.
在 SQL 会话中,运行以下 SQL 命令:
To view your app, sign in to Snowsight, then In the navigation menu, select Projects » Streamlit, and select your app.
Note
Snowflake CLI version 3.14.0 or later is required. Version 3.14+ uses the modern CREATE STREAMLIT syntax by default.
-
初始化一个新的 Streamlit 项目:
-
导航到项目目录:
-
Edit the
snowflake.ymlfile to use a container runtime: -
部署应用程序并在浏览器中打开它:
编辑应用程序
部署后,您可以编辑应用程序代码以对其进行自定义。快速测试方法如下:
- Sign in to Snowsight.
- In the navigation menu, select Projects » Streamlit, and then select your app.
- Select Edit.
- Modify the code in
streamlit_app.py. - Select Run to see your changes.
-
获取应用程序的源位置:
-
将更新后的文件复制到该位置:
- Edit
streamlit_app.pyin your local project directory. - 重新部署:
For more information, see Edit your Streamlit app.
下一步是什么?
现在您已经有了一个正在运行的应用程序,请尝试以下实践示例之一:
- Example: Build a personalized data dashboard: Build a dashboard that queries Snowflake data and personalizes
the display for each viewer using
st.connectionandst.user. - Example: Build a form that writes to Snowflake: Build a form that writes user input back to a Snowflake table,
demonstrating
st.form, dependency management, andst.user.
要了解有关特定主题的更多信息,请执行以下操作:
- Create your Streamlit app: Detailed instructions for creating apps from Snowsight, SQL, or the CLI.
- Manage dependencies for your Streamlit app: Add Python packages to your app.
- Runtime environments for Streamlit apps: Understand container and warehouse runtimes.
- External network access in Streamlit in Snowflake: Connect your app to external services.