创建笔记本

This topic describes how to create a Snowflake notebook on Warehouse Runtime. You can also create a notebook on Container Runtime. For details, see Notebooks on Container Runtime.

Snowflake Notebooks provide an interactive, cell-based development environment within Snowsight. They enable you to work with Snowflake data using SQL and Python in a single interface, making it easier to build and iterate on workflows for data exploration, transformation, and machine learning.

You can access notebooks through Snowsight, where you can 创建新笔记本 or 打开现有笔记本. You can also create a notebook using SQL. For more information, see CREATE NOTEBOOK.

先决条件

运行时

在使用仓库运行时创建笔记本时,您需要指定名称、位置和仓库。在此预览中,您还可以为笔记本选择特定的预配置运行时环境。使用 Snowflake 默认运行时环境可确保您的笔记本在一致的设置下运行,从而支持可重现的结果。此设置无需初始配置,可立即使用。

Snowflake 仓库运行时环境由以下组件组成:

Snowflake 仓库运行时版本Python 运行时Streamlit 版本
1.03.91.39.1
2.03.101.39.1

所有新笔记本都默认使用 Python 3.9 运行时(仓库运行时 1.0)。

Note

如果您在 Snowflake 运行时基础上安装软件包,Snowflake 将无法再保证整个环境的兼容性。

创建新笔记本

You can create a new notebook by selecting + Notebook, or you can import a file with the *.ipynb extension. This could be a notebook file created from an application outside of Snowflake.

To create a new notebook, follow these steps:

  1. Sign in to Snowsight.

  2. In the navigation menu, select Projects » Notebooks.

  3. Select + Notebook.

  4. Enter a name for your notebook. Snowflake preserves the exact casing of the notebook name as entered, including names that contain spaces. Notebook names are case-sensitive.

  5. Select a Notebook location. This is the database and schema in which to store your notebook. These cannot be changed after you create the notebook.

    Note

    The Notebook location list might not show databases that were created after you opened the Create Notebook dialog. If you can’t find your recently created database, schema, or warehouse, reload your browser window.

    Querying data in the notebook is not restricted to this location. In the notebook, you can query data in any location you have access to. To specify the location, run USE WAREHOUSE and USE SCHEMA.

  6. Select Run on warehouse as your Python environment. For details on what is included in each runtime, see Legacy Notebook runtimes.

    For details on Container Runtime, see Notebooks on Container Runtime.

  7. Optional: Select a Query warehouse to run any SQL and Snowpark queries issued by the notebook.

  8. Select a Notebook warehouse to run notebook-specific tasks. Snowflake recommends that you use SYSTEM$STREAMLIT_NOTEBOOK_WH, a Snowflake-managed warehouse that is provisioned in each account for running notebooks.

    Note

    By default, notebooks are suspended after a period of inactivity. The default idle timeout depends on the runtime:

    • Warehouse Runtime notebooks: 30 minutes (1,800 seconds) of inactivity
    • Container Runtime notebooks: 60 minutes (3,600 seconds) of inactivity

    You can set the idle timeout to a maximum of 72 hours (259,200 seconds). To update the idle timeout setting, use either the CREATE NOTEBOOK or ALTER NOTEBOOK commands to set the value of the IDLE_AUTO_SHUTDOWN_TIME_SECONDS property.

    You can change the idle timeout setting after creation from the notebook settings. For more information, see Idle time and reconnection.

  9. Select Create to create and open your notebook.

To create a new notebook from an existing file, follow these steps:

  1. Select the down arrow next to + Notebook and then select Import .ipynb file.

  2. 打开要导入的文件,例如通过 Snowflake 之外的应用程序创建的笔记本文件。

    Note

    If your notebook imports Python packages, you must add the packages to the notebook before you can run the imported notebook. See Import Python packages to use in notebooks. If the package you use in your imported notebook is not available, your code might not run. For information about adding cells, see Develop and run code in Snowflake Notebooks.

使用 SQL 创建笔记本

You can create a notebook using the CREATE NOTEBOOK command. This command lets you define the notebook’s location, main file, and version source programmatically. However, when you create a notebook using SQL, the notebook does not automatically include a live version. A live version is required in order to run the notebook using the EXECUTE NOTEBOOK command.

如果您尝试运行没有实时版本的笔记本,或者该笔记本已删除并重新创建,则可能会看到以下错误:

Live version is not found.

要解决此问题,请在执行笔记本之前向其添加实时版本,如以下示例所示:

ALTER NOTEBOOK DB_NAME.SCHEMA_NAME.NOTEBOOK_NAME ADD LIVE VERSION FROM LAST;
  • DB_NAME is the name of the database that contains the notebook
  • SCHEMA_NAME is the name of the schema that contains the notebook
  • NOTEBOOK_NAME is the name of the notebook

从 Git 存储库创建笔记本

You can sync your notebook development with a Git repository. Then you can create Snowflake Notebooks from notebooks in that Git repository.

To create a notebook from a file in Git, see Create a notebook from a file in a Git repository.

复制现有笔记本

You can duplicate existing Snowflake Notebooks. Duplicating notebooks may be useful if you want to, for example, test out some code changes without altering the original notebook version.

当您复制笔记本时,复制的笔记本将与原始笔记本具有相同的角色和仓库,并且包含在与原始笔记本相同的数据库和架构中。因此,您无法复制笔记本以将其移动到不同的数据库和架构,或更改所有权。

要复制笔记本,请完成以下步骤:

  1. Sign in to Snowsight.
  2. In the navigation menu, select Projects » Notebooks.
  3. 打开您想要复制的笔记本。
  4. Select the vertical ellipsis More actions for worksheet menu, then select Duplicate.
  5. (Optional) Enter a name for the duplicate notebook, then select Duplicate.
  6. In the confirmation dialog, select Close to return to the original notebook, or Open notebook to open the duplicate notebook.

打开现有笔记本

要打开现有笔记本,请按照以下步骤操作:

  1. Sign in to Snowsight.

  2. In the navigation menu, select Projects » Notebooks.

    Note

    Recently used notebooks also appear in Snowsight. Under Recently viewed, select Notebooks.

  3. 查看笔记本列表。

您可以查看由您当前就角色拥有的所有笔记本,或者由您当前角色继承的角色拥有的所有笔记本。每个笔记本都会显示以下信息:

  • Title: The title of the notebook
  • Viewed: The last time the notebook was viewed
  • Updated: The last time the notebook was executed
  • Environment: The runtime environment for the notebook (Container Runtime or Warehouse Runtime)
  • Location: The database and schema locations for the notebook
  • Owner: The owner of the notebook
  1. 选择并打开一个笔记本以进行编辑。

    For details about editing notebooks, see Develop and run code in Snowflake Notebooks.

When you open a notebook, you can see cached results from the last time you ran any cells in the notebook. The notebook is in the Not connected state by default, but if you select that state or run any cell, your notebook connects to your virtual warehouse.