Managing Snowflake Notebooks with Python¶
You can use Python to manage Snowflake Notebooks, which is a development interface in Snowsight that offers an interactive, cell-based programming environment for Python and SQL. For more information, see About Snowflake Notebooks.
The Snowflake Python APIs represents notebooks with two separate types:
Notebook: Exposes a notebook’s properties such as its name, version, query warehouse, and.ipynbfile.NotebookResource: Exposes methods you can use to fetch a correspondingNotebookobject, manage versions of the notebook, and execute the notebook.
Prerequisites¶
The examples in this topic assume that you’ve added code to connect with Snowflake and to create a Root object from which to use the
Snowflake Python APIs.
For example, the following code uses connection parameters defined in a configuration file to create a connection to Snowflake:
Using the resulting Session object, the code creates a Root object to use the API’s types and methods. For more information,
see Connect to Snowflake with the Snowflake Python APIs.
Creating a notebook¶
To create a notebook, first create a Notebook object, and then create a NotebookCollection object from the API Root
object. Using NotebookCollection.create, add the new notebook to Snowflake.
Code in the following example creates a Notebook object that represents a notebook named my_nb in the my_db database
and the my_schema schema:
The code creates a NotebookCollection variable notebooks and uses NotebookCollection.create to create a new
notebook in Snowflake.
You can also create a notebook from a stage with an existing .ipynb file. Code in the following example creates a notebook from the
@my_stage stage with the notebook_file.ipynb file:
Getting notebook details¶
You can get information about a notebook by calling the NotebookResource.fetch method, which returns a Notebook object.
Code in the following example gets information about a notebook named my_nb in the my_db database and the my_schema schema:
Listing notebooks¶
You can list notebooks using the NotebookCollection.iter method, which returns a PagedIter iterator of
Notebook objects.
Code in the following example lists notebooks whose name starts with my in the my_db database and the my_schema schema, and
then prints the name of each:
Performing notebook operations¶
You can perform common notebook operations—such as managing versions and executing notebooks—with a NotebookResource object.
To demonstrate some operations you can do with a notebook resource, code in the following example does the following:
Gets the
my_nbnotebook resource object.Adds a lives version to the notebook object. This is equivalent to ALTER NOTEBOOK … ADD LIVE VERSION.
Commits the live version of the notebook to a Git repository, if a Git connection is set up. Otherwise, sets the live version to
null.For more information, see ALTER NOTEBOOK.
Executes the notebook.
Note
To execute a notebook, you must add a live version to it first.
Drops the notebook.