整理 Streamlit 应用程序文件¶
When you deploy an app to Streamlit in Snowflake, the app’s entrypoint file can have any name that follows standard file naming conventions and can be located anywhere in the app’s source directory. The app’s source directory can contain additional files, such as page scripts, Python modules, media files, and configuration files.
The root of your app’s source directory is Streamlit’s working directory. If you develop and run
your app locally, this means you must execute the streamlit run command from the root of
your source directory to ensure that all paths are correct.
When you initialize a Streamlit app from Snowsight or use CREATE STREAMLIT without specifying a source location, the embedded stage of the Streamlit object contains an entrypoint file in its root. You can use the file explorer to add additional files. If you need to rename or move your entrypoint file, you must use SQL commands to update your Streamlit object’s MAIN_FILE value.
Note
If you use the CREATE STREAMLIT command with the ROOT_LOCATION parameter, your app can only use a warehouse runtime and is subject to additional limitations. This page covers apps created with the FROM parameter. For more information, see Understanding the different types of Streamlit objects.
容器运行时文件结构
When you use a container runtime, your entrypoint file can have any name that follows
standard file naming conventions and can be located anywhere in your source
directory. However, with the introduction of st.navigation_ in Streamlit v1.36, the most common
practice is to use streamlit_app.py as the entrypoint file because page names don’t have to be
inferred from the file names.
Snowflake executes the streamlit run command from the root of the source directory, so
you must handle paths accordingly.
- 入口点文件可以具有任何名称,并且可以位于来源目录中的任何位置。
- Your dependency files can be in any directory between the root of your source directory and directory containing your entrypoint file. For more information, see Manage dependencies for your Streamlit app.
- You can have one or more
.streamlit/directories between the root of your source directory and the directory containing your entrypoint file. - 来源目录的根目录是 Streamlit 的工作目录。
以下目录结构对容器运行时 Streamlit 应用程序有效:
The following directory structure shows two apps in one source directory, each with its own entrypoint file
and dependencies. In this example, two different Streamlit objects exist. Both Streamlit objects set FROM to the location
represented by source_directory, but each object sets MAIN_FILE to a different streamlit_app.py file.
The first app uses a pyproject.toml file for dependencies, while the second app uses a requirements.txt file.
Important
某些 Streamlit 功能需要相对于工作目录的路径,而其他功能则需要相对于入口点文件的路径。
通常,应用程序中图像和其他媒体的路径应相对于工作目录(来源目录的根目录)。但是,多页面应用程序中其他页面的路径与入口点文件的位置相关。
To avoid confusion, consider organizing your app files so that the entrypoint file
is in the root of your source directory. You can save multiple apps in one Git
repository and pass a subdirectory to the FROM parameter when you create the
Streamlit object. That subdirectory is then your app’s source directory. In the
previous example, this means using source_directory/app_one and
source_directory/app_two in the FROM parameter. Although in that case, the apps
would lose access to the shared modules in source_directory/utils.
仓库运行时文件结构
When you use a warehouse runtime, your entrypoint file can have any name but must be located in the root of your
source directory. Your Python version and dependencies are specified in an environment.yml file in the root
of your source directory. If you don’t include an environment.yml file, your app will run on the latest
version of Python and latest version of Streamlit that are currently supported in Streamlit in Snowflake. If you use the
package picker in Snowsight to add packages, the
environment.yml file is automatically updated or created for you.
以下目录结构对仓库运行时 Streamlit 应用程序有效:
从其他暂存区导入模块和文件
The CREATE STREAMLIT and ALTER STREAMLIT commands support the IMPORTS parameter, which allows you to import additional files from other stages into your app’s source directory. If you have a set of common modules or files that you want to share across multiple apps, you can store them in a stage and import them into each app using the IMPORTS parameter. However, this is only supported for apps using a warehouse runtime.
多页面应用程序
Streamlit 支持两种创建多页面应用程序的方法:
- Using
st.navigation: You can use thest.navigationcommand to create a custom navigation structure within your app. This allows you to define pages programmatically and control the navigation flow. The entrypoint file acts like a page router and the pages of your app can be defined as functions or Python scripts anywhere in your source directory. This is the recommended method for creating multipage apps, because it provides the most flexibility. - Using a
pages/directory: You can create a directory namedpages/adjacent to your app’s entrypoint file. The entrypoint file is treated as the home page of your app. Each Python file in thepages/directory is treated as an additional page in the app. Page names are derived from the filenames.
You can’t mix the two methods for creating multipage apps. For more information on multipage apps, see Overview of multipage apps (https://docs.streamlit.io/develop/concepts/multipage-apps/overview) in the Streamlit documentation.
Note
When you host multipage apps in Streamlit in Snowflake, URL pathnames are prefixed with /!. For example, if the relative path to a page is /page2
in a multipage app, its relative path in Streamlit in Snowflake becomes /!/page2 as shown in the following URL: https://app.snowflake.cn/org/account_name/#/streamlit-apps/DB.SCHEMA.APP_NAME/!/page_2
更新入口点文件
如果重命名或移动入口点文件,则必须使用 SQL 命令更新 Streamlit 对象以使用新的入口点文件。如果将入口点文件移动到子目录,则必须使用容器运行时。
-
Use the ALTER STREAMLIT command to change the MAIN_FILE parameter of your Streamlit object, as shown in the following example:
This example changes the entrypoint file of the
my_streamlit_appStreamlit object tosubdir/new_entrypoint.py.