在运行时环境之间迁移

您可以通过更新应用程序的 RUNTIME_NAME 和 COMPUTE_POOL 属性,在仓库运行时与容器运行时之间迁移 Streamlit 应用。但是,某些功能仅受一种运行时环境支持,因此在不同运行时环境之间迁移应用程序时需要注意相关限制。

本页提供了一份从仓库运行时迁移到容器运行时的检查清单。每一项都包含简要说明,并在需要时提供指向详细信息的链接。

Note

If a user is viewing an app with a warehouse runtime and the app is altered on another tab to use a container runtime, the warehouse instance continues to run until one of the following events happens: the user navigates away, the tab is closed, the page is refreshed, or the WebSocket times out. In this case, source code changes made from the warehouse view will update the new container instance, but the warehouse preview won’t reflect any changes, even if the user selects Run.

先决条件

在开始之前,请先在原位置调整您的仓库运行时应用程序,为迁移做好准备。

Optional: Back up your app’s code

如果您的应用程序源代码尚未存储在版本控制系统、外部仓库或本地目录中,请先进行备份,以避免在迁移过程中发生潜在的数据丢失。

Ensure that your app wasn’t created with ROOT_LOCATION

使用 ROOT_LOCATION 参数创建的应用程序只能运行在仓库运行时中。如果您的应用程序是使用 ROOT_LOCATION 创建的,请将其升级为使用 FROM 参数。

See: Understanding the different types of Streamlit objects

Upgrade your app to Streamlit 1.50+

请确保您的应用程序及其所有依赖项与 Streamlit 1.50 及以上版本兼容。

See: Manage dependencies for your Streamlit app

Update your app to Python 3.11 only

容器运行时仅支持 Python 3.11,而仓库运行时支持 Python 3.9、3.10 和 3.11。请确保您的应用程序及其所有依赖项与 Python 3.11 兼容。

See: Manage dependencies for your Streamlit app

Optional: Locally install Snowflake CLI 3.14.0+

If you deploy apps using Snowflake CLI, you need version 3.14.0 or later to support the container runtime deployment syntax. Check your version with snow --version. Optionally, you can use versions 3.12.0 - 3.13.1 if you use the --experimental flag.

See: Create your Streamlit app

资源与权限

您的应用程序可以继续使用现有的查询仓库,但需要为容器运行时配置一个计算池。

Create and grant access to a compute pool

应用程序所有者需要在运行容器运行时的计算池上拥有 USAGE 权限。应用程序查看者无需任何计算池权限。

See: Privileges required to create and use a Streamlit app

Create and grant access to an external access integration

容器运行时仅附带最少量的预安装包。如果应用程序需要额外的包或预安装包的不同版本,则必须使用外部包索引,例如 PyPI。要允许应用程序访问外部包索引,必须创建外部访问集成 (EAI),并将 EAI 的 USAGE 权限授予应用程序所有者。

See: External network access in Streamlit in Snowflake

依赖关系管理

Replace environment.yml with pyproject.toml or requirements.txt

If you need to lock any dependency versions or specify additional dependencies, you must add a pyproject.toml or requirements.txt file to the root of your project directory. Packages can have different names between Conda and PyPI, so ensure you use the correct package names for your artifact repository.

See: Manage dependencies for your Streamlit app

Alter your app to set its external access integrations

如果依赖项包含版本说明符,或安装了其他包,必须为应用程序分配外部访问集成。这样应用程序才能访问依赖文件中指定的包索引。PyPI 是默认包索引。

See: Manage dependencies for your Streamlit app

代码更改

Replace get_active_session() with st.connection("snowflake").session()

When you use a container runtime, the Streamlit server handles multiple viewers concurrently. get_active_session() isn’t thread-safe, so you must use st.connection("snowflake") to manage your connection instead.

See: Manage secrets and configure your Streamlit app

Review your code and implement caching

Because container runtimes share disk, compute, and memory resources between viewer sessions, you should use st.cache_resource or st.cache_data to cache expensive computations or data that doesn’t change frequently.

See: Understanding Streamlit’s client-server architecture (https://docs.streamlit.io/develop/concepts/architecture/architecture) and Caching overview (https://docs.streamlit.io/develop/concepts/architecture/caching) in the Streamlit documentation.

Ensure thread-safety

使用容器运行时时,应用程序代码必须线程安全,以便同时处理多个查看者。虽然每个查看者都会获得应用程序脚本的独立实例,但应检查导入代码中是否存在共享状态或全局变量,以避免竞态条件或行为不一致。如果在 Streamlit 应用中引入新线程,请查看 Streamlit 架构,并避免在自定义线程中使用 Streamlit 命令。

See: Multithreading in Streamlit (https://docs.streamlit.io/develop/concepts/design/multithreading) in the Streamlit documentation.

Replace _snowflake usage with native Python equivalents

_snowflake is a private module that is only available in user-defined functions (UDFs) and stored procedures. Warehouse runtimes inherit access to _snowflake, but container runtimes don’t. If your app uses _snowflake, replace it with native Python equivalents, such as the Snowflake Python Connector. If needed, use stored procedures to access secrets.

See: Manage secrets and configure your Streamlit app

Update file paths and organization

源代码位置的根目录即应用程序的工作目录。对于大多数 Python 库,应用程序需使用源代码根目录的相对路径。但某些 Streamlit 命令需要相对于入口文件的路径。如果入口文件位于子目录,请相应检查代码中的路径。

Verify secrets.toml and config.toml locations.

See: Organize your Streamlit app files

应用程序配置变更

Alter your app to set its compute pool, query warehouse, and runtime

When you are ready to switch the runtime type of your app, you can use Snowsight or SQL.

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Streamlit, and then select your app.
  3. In the upper-right corner, select the vertical ellipsis More actions for worksheet menu, and then select App settings.
  4. For the Python environment, select Run on container.
  5. In the Compute pool dropdown, select your compute pool.
  6. In the Query warehouse dropdown, select your query warehouse.
  7. To save your changes and close the dialog, select Save.

应用程序将用几分钟时间重新启动并构建新容器。

See: Runtime environments for Streamlit apps