添加 Streamlit 应用程序¶
This topic describes how to include a Streamlit (https://streamlit.io/) app within a Snowflake Native App.
About Streamlit and the Snowflake Native App Framework¶
Streamlit (https://streamlit.io/) is an open-source Python library that makes it easy to create and share custom web apps for machine learning and data science. By using Streamlit you can quickly build and deploy powerful data applications.
For information about the open-source library, see the Streamlit Library documentation (https://docs.streamlit.io/).
Within the Snowflake Native App Framework you can use Streamlit to perform the following:
-
Create a front-end web app that enables consumers to visualize the data provided by your Snowflake Native App.
-
Create a user interface that allows consumers to grant privileges and create references to objects within their account that are used by the Snowflake Native App.
See Create and access objects in a consumer account for more information.
Note
See 不支持的 Streamlit 功能 for information on unsupported Streamlit features.
Considerations for warehouses when using Streamlit in a Snowflake Native App¶
Streamlit apps in a Snowflake Native App run using a Snowflake warehouse. The same warehouse considerations apply to both Streamlit in Snowflake and Streamlit in a Snowflake Native App. See Guidelines for selecting resources in Streamlit in Snowflake for more information.
Note
Streamlit apps in a Snowflake Native App support the USE WAREHOUSE command. However, references to warehouses are not supported.
受支持的 Streamlit 库版本¶
The Snowflake Native App Framework supports the same versions of the Streamlit library as Streamlit in Snowflake. For more information, see Supported versions of the Streamlit library in warehouse runtimes.
在 Streamlit 库的更新版本发布时,将包含对这些版本的支持。
See 为应用程序设置 Streamlit 版本 for information on how to set the version for a Streamlit app.
支持的外部包
By default, a Streamlit app that is included within a Snowflake Native App includes the python, streamlit,
and snowflake-snowpark-python packages pre-installed in the consumer environment. The consumer environment
also has access to the dependencies required by these packages.
不支持的 Streamlit 功能¶
The following Streamlit features are not currently supported when using Streamlit in a Snowflake Native App:
- 不支持自定义组件。
- Using Azure Private Link and Google Cloud Private Service Connect to access a Streamlit app is not supported.
- st.bokeh_chart (https://docs.streamlit.io/library/api-reference/charts/st.bokeh_chart)
- st.cache_data (https://docs.streamlit.io/library/api-reference/performance/st.cache_data)
- st.cache_resource (https://docs.streamlit.io/library/api-reference/performance/st.cache_resource)
- st.camera_input (https://docs.streamlit.io/library/api-reference/widgets/st.camera_input)
- st.download_button (https://docs.streamlit.io/library/api-reference/widgets/st.download_button) (only supported in Streamlit version 1.26 or later)
- st.file_uploader (https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader)
- st.image (https://docs.streamlit.io/library/api-reference/media/st.image)
- st.pyplot (https://docs.streamlit.io/library/api-reference/charts/st.pyplot)
- st.scatter_chart (https://docs.streamlit.io/library/api-reference/charts/st.scatter_chart)
- st.set_page_config (https://docs.streamlit.io/library/api-reference/utilities/st.set_page_config)
The
page_titleandpage_iconproperties of the st.set_page_config (https://docs.streamlit.io/library/api-reference/utilities/st.set_page_config) command are not supported.
- st.video (https://docs.streamlit.io/library/api-reference/media/st.video)
- Custom Components (https://docs.streamlit.io/library/components), including:
- component.html() (https://docs.streamlit.io/library/components/components-api#stcomponentsv1html)
- component.iframe() (https://docs.streamlit.io/library/components/components-api#stcomponentsv1iframe)
- Configuration files (https://docs.streamlit.io/library/advanced-features/configuration)
- The following experimental features:
- st.experimental_set_query_params (https://docs.streamlit.io/library/api-reference/utilities/st.experimental_set_query_params)
- st.experimental_get_query_params (https://docs.streamlit.io/library/api-reference/utilities/st.experimental_get_query_params)
- Network access via the internet
- Anchor links
将 Streamlit应用程序添加到 Snowflake Native App 的工作流程¶
The following workflow describes how to add a Streamlit app to a Snowflake Native App:
-
开发原生应用程序。
This includes adding the data content that you want consumers to access using Streamlit. See Snowflake Native App Framework workflow for more information.
-
Review the following sections to understand the supported version of the Streamlit library and unsupported features:
-
开发 Streamlit 应用程序。
See the Streamlit Library documentation (https://docs.streamlit.io/) for information on using the Streamlit open-source library.
-
为 Streamlit 应用程序创建本地目录结构。
See Streamlit 应用程序的示例目录结构 for recommendations on how to organize your Streamlit files within the structure of your app.
-
向安装脚本添加一条 CREATE STREAMLIT 语句。
When running the CREATE APPLICATION command, the setup script runs the CREATE STREAMLIT statement to create a Streamlit object. This object contains the schema and Python files required by the Streamlit app.
-
Configure the
environment.ymlfile to include additional libraries in your Streamlit app.See 向 Streamlit 应用程序添加额外的包 for more information.
-
Optional: Add the Streamlit object name as an entry in the manifest file to display the Streamlit app as the default app in Snowsight.
See 将 Streamlit 应用程序添加到清单文件 for more information.
-
Upload the Streamlit files,
environment.ymlfile, setup script, and manifest file. files to a named stage. To include Streamlit code files in an application package, the files must be uploaded to a named stage. -
测试应用程序包。
After creating the files required by the application package and Streamlit app, create an application object to test the setup script and manifest file.
See 测试包含 Streamlit 应用程序的应用程序包 for more information.
-
View the Streamlit app in Snowsight.
To test the Streamlit app, view the app in Snowsight. See 在 Snowsight 中测试 Streamlit 应用程序.
Streamlit 应用程序的示例目录结构¶
Like other Python modules, to add a Streamlit app to an application package you must upload your Streamlit code files to a named stage. See PUT for information on how to upload files to a stage.
To account for multiple versions of a Snowflake Native App, consider using a directory structure similar to the following to maintain your Streamlit apps and related application files:
请注意,您创建的目录结构取决于您的应用程序和开发环境的要求。
Note
The environment.yml file must be at the same level as your main file of your Streamlit app.
See Reference external code files for more information on relative paths.
在装脚本中创建 Streamlit 对象¶
The following example shows how to use CREATE STREAMLIT within the setup script of an app.
This example creates a Streamlit object within a schema named app_schema.
The CREATE STREAMLIT command uses the Streamlit app specified by the
MAIN_FILE clause. The directory location is specified by the value of the FROM clause.
See Streamlit 应用程序的示例目录结构 for information on creating the directory structure for a Streamlit app within an application package.
此示例还向应用程序角色授予对架构和 Streamlit 对象的必要权限。
向 Streamlit 应用程序添加额外的包¶
Use the environment.yml file to add additional Python packages to a Streamlit app. For
example, to add the scikit-learn library to a Streamlit app, add the following to the
environment.yml file:
The name and channels properties are both required.
Also, the - snowflake key is required under the channels property. This indicates the
Snowflake Anaconda Channel (https://repo.anaconda.com/pkgs/snowflake/).
Note
You can only install packages listed in the Snowflake Anaconda Channel (https://repo.anaconda.com/pkgs/snowflake/). Snowflake does not support external Anaconda channels in Streamlit.
为应用程序设置 Streamlit 版本¶
The Snowflake Native App Framework supports multiple versions of the Streamlit library. To set the Streamlit version within
a Snowflake Native App add streamlit to the dependencies section of the environment.yml file
as shown in the following example:
Snowflake 建议显式为您的应用程序设置 Streamlit 版本。但如果您目前并未显式设置 Streamlit 库的版本,则系统默认设置 Streamlit 版本 1.22.0。
将 Streamlit 应用程序添加到清单文件¶
To specify the default Streamlit app launched by your app, add the following entries in the manifest file:
The default_streamlit: app_schema.streamlit_app_na entry specifies the location of the
schema containing your Streamlit app.
测试包含 Streamlit 应用程序的应用程序包¶
To test the application package containing the Streamlit app, create an application object using the files on a named stage by running the CREATE APPLICATION as shown in the following example:
Depending on what you need to test, you can create the application object using other forms of the CREATE APPLICATION. For example, you may want to test the Streamlit app as part of a version or upgrade. See Install and test an app locally.
在 Snowsight 中测试 Streamlit 应用程序¶
To test the Streamlit app, view the app in Snowsight by doing the following:
-
Sign in to Snowsight.
-
In the navigation menu, select Catalog » Apps.
-
选择要查看的 Streamlit 应用程序。
The main Streamlit app opens in the Snowsight.
-
Optional: If you are viewing a multipage Streamlit app, select a tab to view additional pages.
Troubleshoot a Streamlit app in the Snowflake Native App Framework¶
如果应用程序显示未知错误,请确保尝试以下部分中描述的解决方案。
确认服务条款
To use Streamlit and packages provided by Anaconda in Snowflake, you must acknowledge the External Offerings Terms. To learn more, see Using third-party packages from Anaconda.
防火墙允许列表
Each Streamlit app uses a unique subdomain. If you use strict firewalls, add *.snowflake.app to your firewall allowlist. Adding this entry to your allowlist allows your apps to communicate with Snowflake servers without any restrictions.