创建 Streamlit 应用程序

先决条件

在使用 Snowflake CLI 创建 Streamlit 应用程序之前,应满足以下先决条件:

引导 Streamlit 应用程序

snow init 命令用于创建本地目录,其中包含一组示例文件,可帮助您开始创建 Streamlit 应用程序。当您执行此命令时,Snowflake CLI 会创建以下目录结构:

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
Copy

小心

执行其他 snow streamlit 命令时,项目目录中的文件会被 Snowflake CLI Snowflake 处理,并且可以上传到 Snowflake。在将任何敏感信息放入项目目录中的文件中时应小心谨慎。

有关文件结构的更多信息,请参阅 在本地文件系统上创建 Streamlit 文件

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

Snowflake 中的每个 Streamlit 应用程序都必须包含一个 snowflake.yml 项目定义文件。Streamlit 限制为每个项目定义文件对应一个应用程序。

下面显示的是一个 snowflake.yml 项目定义文件示例:

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
Copy

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

Streamlit 项目定义属性

属性

定义

identifier

optionalstring

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

  • 字符串标识符文本

    identifier: my-streamlit-id
    
    Copy

    支持不带引号和带引号的标识符。要使用带引号的标识符,请在 YAML 值中包含周围的引号(例如 ’”My Streamlit Application”’)。

  • 对象

    identifier:
      name: my-streamlit-id
      schema: my-schema # optional
      database: my-db # optional
    
    Copy

    备注

    如果指定 schemadatabase,并在 name 属性中使用完全限定名称(例如 mydb.schema1.my-app),则会发生错误。

type

optionalstring

必须是 streamlit

comment

optionalstring

对 Streamlit 仪表板的注释。

title

optionalstring

Streamlit 仪表板的可读标题。

stage

optionalstring

存储应用程序工件的暂存区。默认值:None。

query_warehouse

requiredstring

用于托管应用程序的 Snowflake 仓库。

main_file

optionalstring

streamlit 应用程序的 ` 入口点文件 <https://docs.streamlit.io/get-started/tutorials/create-an-app (https://docs.streamlit.io/get-started/tutorials/create-an-app)>`_。默认值:“streamlit_app.py”。

pages_dir

optionalstring

Streamlit ` 页面 <https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app (https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app)>`_。默认值:“pages”。

external_access_integrations

optionalstring sequence

此 Streamlit 应用程序代码访问外部网络所需的 外部访问集成 的名称。有关更多详细信息,请参阅 CREATE STREAMLIT 的可选参数

secrets

optionaldictionary

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

imports

optionalstring sequence

要导入的先前已上传文件的暂存区和路径。有关更多详细信息,请参阅 CREATE STREAMLIT 的可选参数

artifacts

requiredstring sequence

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

  • src: Path to the code source file or files.

  • dest:要部署工件的目录的路径。

    引用目录的目标路径必须以 / 结尾。如果 glob 模式的目标不是以 / 结尾,则会导致错误。如果省略,dest 默认为与 src 字符串相同。

    您还可以为每个项目传递一个字符串,而不是 dict,在这种情况下,该值将视为 srcdest

如果 src 仅引用一个文件(而不是 glob),dest 可以引用目标 <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.

语言: 中文