创建 Streamlit 应用程序

先决条件

Before creating a Streamlit app with Snowflake CLI, you should meet the following prerequisites:

引导 Streamlit 应用程序

The snow init command creates a local directory with a sample set of files that help you get started creating a Streamlit app. When you execute this command, Snowflake CLI creates the following directory structure:

example_streamlit/            - project name (default: example_streamlit)
  snowflake.yml               - configuration for snow streamlit commands
  environment.yml             - additional config for Streamlit, for example installing packages
  streamlit_app.py            - entrypoint file of the app
  pages/                      - directory name for Streamlit pages (default pages)
  common/                     - example shared library

要初始化 Streamlit 应用程序,请输入以下命令:

snow init new_streamlit_project --template example_streamlit -D query_warehouse=dev_warehouse -D stage=testing

Caution

Files inside a project directory are processed by Snowflake CLI and could be uploaded to Snowflake when executing other snow streamlit commands. You should use caution when putting any sensitive information inside files in a project directory.

For more information about the file structure, see Organize your Streamlit app files.

为 Streamlit 应用程序创建项目定义

Each Streamlit app in Snowflake must include a snowflake.yml project definition file. Streamlit is limited to one application per project definition file.

The following shows a sample snowflake.yml project definition file:

definition_version: 2
entities:
  my_streamlit:
    type: streamlit
    identifier: streamlit_app
    stage: my_streamlit_stage
    query_warehouse: my_streamlit_warehouse
    main_file: streamlit_app.py
    pages_dir: pages/
    external_access_integrations:
      - test_egress
    secrets:
      dummy_secret: "db.schema.dummy_secret"
    imports:
      - "@my_stage/foo.py"
    artifacts:
      - common/hello.py
      - environment.yml
    grants:
      - privilege: USAGE
        role: streamlit_role

下表描述了 Streamlit 项目定义的属性。

Streamlit project definition properties

PropertyDefinition

identifier

optionalstring

实体的可选 Snowflake 标识符。该值可以采用以下形式:

  • 字符串标识符文本
identifier: my-streamlit-id

Both unquoted and quoted identifiers are supported. To use quoted identifiers, include the surrounding quotes in the YAML value (e.g. ’”My Streamlit Application”’).

  • 对象
identifier:
  name: my-streamlit-id
  schema: my-schema # optional
  database: my-db # optional

Note

An error occurs if you specify a schema or database and use a fully-qualified name in the name property (such as mydb.schema1.my-app).

type

optionalstring

Must be streamlit.

comment

optionalstring

Comment for the Streamlit dashboard.

title

optionalstring

Human-readable title for the Streamlit dashboard.

stage

optionalstring

Stage in which the app’s artifacts will be stored. Default: None.

query_warehouse

requiredstring

Snowflake warehouse to host the app.

main_file

optionalstring

Entrypoint file (https://docs.streamlit.io/get-started/tutorials/create-an-app) of the streamlit app. Default: “streamlit_app.py”.

pages_dir

optionalstring

Streamlit pages (https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app). Default: “pages”.

external_access_integrations

optionalstring sequence

Names of external access integrations needed for this Streamlit application code to access external networks. See the optional parameters for CREATE STREAMLIT for more details.

secrets

optionaldictionary

将密钥的名称分配给变量,以便在从应用程序代码中的密钥中检索信息时,可以使用这些变量引用密钥。

imports

optionalstring sequence

Stage and path to previously uploaded files you want to import. See the optional parameters for CREATE STREAMLIT for more details.

artifacts

requiredstring sequence

要添加到部署根的文件源和目标对的列表。您可以使用以下工件属性:

  • src: Path to the code source file or files.
  • dest: Path to the directory to deploy the artifacts.

Destination paths that reference directories must end with a /. A glob pattern’s destination that does not end with a / results in an error. If omitted, dest defaults to the same string as src.

You can also pass in a string for each item instead of a dict, in which case the value is treated as both src and dest.

If src refers to just one file (not a glob), dest can refer to a target <path> or a <path/name>.

You can also pass in a string for each item instead of a dict; in that case, the value is treated as both src and dest.

grants

optional, grant sequence

Grants that should be given for the Streamlit app. Each grant must specify the privilege and target role. For more details, see the optional parameters for CREATE STREAMLIT.