CREATE STREAMLIT¶
Creates a new Streamlit object in Snowflake or replaces an existing Streamlit object in the same schema.
语法¶
CREATE [ OR REPLACE ] STREAMLIT [ IF NOT EXISTS ] <name>
[ FROM <source_location> ]
[ MAIN_FILE = '<filename>' ]
[ QUERY_WAREHOUSE = <warehouse_name> ]
[ RUNTIME_NAME = '<runtime_name>' ]
[ COMPUTE_POOL = <compute_pool_name> ]
[ COMMENT = '<string_literal>' ]
[ TITLE = '<app_title>' ]
[ IMPORTS = ( '<stage_path_and_file_name_to_read>' [ , ... ] ) ]
[ EXTERNAL_ACCESS_INTEGRATIONS = ( <integration_name> [ , ... ] ) ]
[ SECRETS = ( '<snowflake_secret_name>' = <snowflake_secret> [ , ... ] ) ]
以下语法是传统语法:
重要
ROOT_LOCATION is a legacy parameter and may be deprecated in a future release. For Streamlit apps created using ROOT_LOCATION, multi-file editing and Git integration are not supported.
CREATE [ OR REPLACE ] STREAMLIT [ IF NOT EXISTS ] <name>
ROOT_LOCATION = '<stage_path_and_root_directory>'
MAIN_FILE = '<path_to_main_file_in_root_directory>'
[ QUERY_WAREHOUSE = <warehouse_name> ]
[ COMMENT = '<string_literal>' ]
[ TITLE = '<app_title>' ]
[ IMPORTS = ( '<stage_path_and_file_name_to_read>' [ , ... ] ) ]
[ EXTERNAL_ACCESS_INTEGRATIONS = ( <integration_name> [ , ... ] ) ]
必填参数¶
nameSpecifies the identifier (i.e. name) for the Streamlit object. This identifier must be unique for the schema where the object is created.
In addition, the identifier must start with an alphabetic character and can't contain spaces or special characters unless the entire identifier string is enclosed in double quotes (e.g.
"My object"). Identifiers enclosed in double quotes are also case-sensitive.有关更多详细信息,请参阅 标识符要求。
可选参数¶
FROM source_location从指定位置复制应用程序源文件。该位置必须位于内部命名暂存区内。路径可以是相对的,也可以是完全限定的。例如,如果将暂存区命名为
@streamlit_db.streamlit_schema.streamlit_stage,则有效的源位置可以包括:根暂存区的完全限定路径:
FROM '@streamlit_db.streamlit_schema.streamlit_stage'根暂存区的相对路径:
FROM '@streamlit_stage'暂存区内子目录的完全限定路径或相对路径:
FROM '@streamlit_db.streamlit_schema.streamlit_stage/subdir'
执行 CREATE 命令时文件仅复制一次;将来对源位置的更改不会自动更新 Streamlit 应用程序。
如果未指定此参数,Snowflake 会使用
streamlit_app.py入口点文件复制默认应用程序的源文件。MAIN_FILE = 'filename'Specifies the Streamlit entrypoint file. The requirements depend on the runtime type:
Warehouse runtimes: The file must be in the root of the source directory specified in FROM. Only a filename is allowed, not a path.
Container runtimes: The file can be in the root or a subdirectory. You can specify a relative path from the root of the source directory, like
'subdir/my_app.py'.
If you are using ROOT_LOCATION instead of FROM, then MAIN_FILE can be a path relative to ROOT_LOCATION even though ROOT_LOCATION only supports warehouse runtimes.
DEFAULT:
'streamlit_app.py'QUERY_WAREHOUSE = warehouse_nameSpecifies the warehouse used by the Streamlit app. The behavior depends on the runtime type:
Warehouse runtimes: Specifies the warehouse to run the app code and execute SQL queries. This is the code warehouse. It's recommended to manually switch to a different warehouse within your app code for queries.
Container runtimes: Specifies the warehouse to execute SQL queries issued by the app. The app code runs on the compute pool specified by COMPUTE_POOL.
DEFAULT:无值
备注
尽管您可以创建不带此参数的 Streamlit 对象,但在您指定查询仓库之前,该应用程序不会运行。
RUNTIME_NAME = 'runtime_name'Specifies the runtime environment for the Streamlit app. The runtime determines where and how the app executes. Runtime names follow the pattern
SYSTEM$ST_<type>_RUNTIME_PY<version>.Warehouse runtimes: Run the app in a virtual warehouse. Each viewer gets a personal instance of the app. The following warehouse runtimes are valid:
SYSTEM$ST_WAREHOUSE_RUNTIME_PY3_9SYSTEM$ST_WAREHOUSE_RUNTIME_PY3_10SYSTEM$ST_WAREHOUSE_RUNTIME_PY3_11
Container runtimes: Run the app in a Snowpark Container Services compute pool. All viewers share a single, long-running instance of the app. The following container runtimes are valid:
SYSTEM$ST_CONTAINER_RUNTIME_PY3_11
The runtime defaults to the latest warehouse runtime.
DEFAULT:
SYSTEM$ST_WAREHOUSE_RUNTIME_PY3_11COMPUTE_POOL = compute_pool_nameSpecifies the compute pool where the Streamlit app runs. This parameter is required when using a container runtime and is ignored for warehouse runtimes.
DEFAULT:无值
COMMENT = 'string_literal'指定 Streamlit 对象的注释。
DEFAULT:无值
TITLE = 'app_title'Specifies a title for the Streamlit object to display in Snowsight.
DEFAULT:选择使用 时默认使用的角色和仓库。传递给 CREATE STREAMLIT 的 Streamlit 对象的名称。
IMPORTS = ( 'stage_path_and_file_name_to_read' [ , ... ] )The location (stage), path, and name of the file(s) to import. This only applies to warehouse runtimes and is ignored for container runtimes.
DEFAULT:无值
EXTERNAL_ACCESS_INTEGRATIONS = ( integration_name [ , ... ] )Streamlit 应用代码为访问外部网络所需的 外部访问集成名称 名称。
For container runtimes, external access integrations are required to install packages from external package indexes like PyPI. For all runtime types, external access integrations enable the app to make outbound network requests.
DEFAULT:无值
SECRETS = ( 'snowflake_secret_name' = snowflake_secret [ , ... ] )Maps Snowflake secrets to secret names that can be referenced in the Streamlit app code. The secret name (left side) is how you reference the secret in your code, and the secret object (right side) is the identifier of the Snowflake secret.
For example:
SECRETS = ('api_key' = my_database.my_schema.my_secret)Secrets are only available in warehouse runtimes through the
_snowflakemodule and must be associated with an external access integration in EXTERNAL_ACCESS_INTEGRATIONS. In container runtimes, this parameter isn't supported and you must create SQL functions to access secrets instead. For more information, see 管理密钥并配置 Streamlit 应用程序.DEFAULT:无值
ROOT_LOCATION = 'stage_path_and_root_directory'Specifies the path to the named stage containing the Streamlit Python files, media files, and the
environment.ymlfile, for example:ROOT_LOCATION = '@streamlit_db.streamlit_schema.streamlit_stage'
在此示例中,Streamlit 文件位于名为
streamlit_stage的命名暂存区上,而该暂存区在名为streamlit_db的数据库和名为streamlit_schema的架构中。备注
此参数必须指向已命名内部暂存区内的单个目录。
Streamlit in Snowflake 不支持外部暂存区。
如果要在 Snowflake Native App Framework 中创建或替换 Streamlit 应用程序对象,请使用
FROM 'relative_path_from_stage_root_directory'而非ROOT_LOCATION = 'stage_path_and_root_directory'。
访问控制要求¶
如果您的角色没有下表中的对象,则您的角色必须对这些对象具备列出的 权限:
权限 |
对象 |
Notes |
|---|---|---|
CREATE STREAMLIT |
Schema where you create the Streamlit object |
|
READ |
要从中复制 Streamlit 应用程序源文件的暂存区 |
|
USAGE |
Warehouse used by the Streamlit app |
|
USAGE |
Compute pool used by the Streamlit app |
This privilege is only required if your app uses a container runtime. |
USAGE |
External access integrations used by the Streamlit app |
This privilege is only required if your app uses external access integrations. For container runtimes, this privilege is required to install packages from external package indexes like PyPI. |
USAGE |
Secrets used by the Streamlit app |
This privilege is only required if your app uses secrets and only applies to warehouse runtimes. |
CREATE STAGE |
Schema where you create the Streamlit object |
只有使用 ROOT_LOCATION 参数创建 Streamlit 对象时才需要此权限。 |
Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema.
有关创建具有指定权限集的自定义角色的说明,请参阅 创建自定义角色。
使用说明¶
创建应用程序后,必须对其进行初始化。
重要
使用 CREATE STREAMLIT 后,Streamlit 应用程序只有在您执行以下任一操作后才能上线:
对新的 Streamlit 对象执行 ALTER STREAMLIT ... ADD LIVE VERSION FROM LAST。
使用拥有应用程序的角色访问 Snowsight 中的应用程序。
在克隆包含 Streamlit 对象的架构或数据库时,不会克隆 Streamlit 对象。
To specify the packages used by the Streamlit application, include a dependency file in the source files. The format of the dependency file depends on the runtime type:
Warehouse runtime: Use an
environment.ymlfile.Container runtime: Use a
pyproject.tomlorrequirements.txtfile.
For more information, see 管理 Streamlit 应用程序的依赖项.
关于元数据:
注意
客户应确保在使用 Snowflake 服务时,不会将个人数据(用户对象除外)、敏感数据、出口管制数据或其他受监管数据作为元数据输入。有关更多信息,请参阅 Snowflake 中的元数据字段。
The OR REPLACE and IF NOT EXISTS clauses are mutually exclusive. They can't both be used in the same statement.
CREATE OR REPLACE <object> 语句是原子的。也就是说,当对象被替换时,旧对象将被删除,新对象将在单个事务中创建。
示例¶
Create a Streamlit app with default source files¶
To create a container-runtime Streamlit app from built-in default files, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
RUNTIME_NAME = 'SYSTEM$ST_CONTAINER_RUNTIME_PY3_11'
COMPUTE_POOL = my_compute_pool
QUERY_WAREHOUSE = my_warehouse;
By default, apps use the latest warehouse runtime if RUNTIME_NAME isn't specified. To create a warehouse-runtime Streamlit app from built-in default files, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
QUERY_WAREHOUSE = my_warehouse;
Create a Streamlit app from a custom source files¶
To create a container-runtime Streamlit app from custom source files, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
FROM @streamlit_db.streamlit_schema.streamlit_stage
MAIN_FILE = 'streamlit_main.py'
QUERY_WAREHOUSE = my_warehouse
RUNTIME_NAME = 'SYSTEM$ST_CONTAINER_RUNTIME_PY3_11'
COMPUTE_POOL = my_compute_pool;
To create a warehouse-runtime Streamlit app from custom source files, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
FROM @streamlit_db.streamlit_schema.streamlit_stage
MAIN_FILE = 'streamlit_main.py'
QUERY_WAREHOUSE = my_warehouse;
Create a warehouse-runtime Streamlit app with secrets¶
To create a warehouse-runtime Streamlit app with secrets, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
FROM @streamlit_db.streamlit_schema.streamlit_stage
MAIN_FILE = 'streamlit_main.py'
QUERY_WAREHOUSE = my_warehouse
SECRETS = ('api_key' = streamlit_db.streamlit_schema.my_api_secret);
Container-runtime Streamlit apps must use SQL functions to access secrets. For more information, see 管理密钥并配置 Streamlit 应用程序.
Create a Streamlit app from a Git repository¶
To create a Streamlit app from a Git repository, run the CREATE STREAMLIT command as shown in the following example:
CREATE STREAMLIT hello_streamlit
FROM @streamlit_db.streamlit_schema.streamlit_repo/branches/streamlit_branch/
MAIN_FILE = 'streamlit_main.py'
QUERY_WAREHOUSE = my_warehouse;
