Daanalytics

Introducing Snowflake Worksheets for Python

Recently Snowflake made working with Python from within Snowflake a little bit better. ‘Snowflake Worksheets for Python’ is in Public Preview, available for use for everybody. Time to find out how it works.

Generate Faker Data

I am planning a series of blogpost regarding Snowflake Data Governance in combination with Alation. Therefore I need some demo data. Recently I saw a blogpost of my fellow Snowflake Data Superhero; Maja Ferle about generating Sample Data using the faker Library within Snowpark. I took that blogpost as a starting point to generate some fake Customer Data using Snowflake Worksheets for Python.

Enable Anaconda Python Packages

To be able to use the Snowflake Worksheets for Python feature, you first need to review and accept the Anaconda Terms of Service in Snowsight. Therefor you need to follow the next steps:

  1. Select the ORGADMIN-role
  2. Go to Admin, Billing & Terms
  3. Enable Anaconda Python packages
  4. Acknowledge & Continue
Enable Anaconda Python Packages

Packages available in Anaconda

First you have to make sure that the required packages are available in Anaconda, by either checking the documentation or executing the following query:

If you want to select specific packages and their versions, execute the following query. In this example we are looking for; ‘faker’ and ‘pandas’.

There might be a need to include packages that are not available from Anaconda. According to the documentation, you can add a Python File from a Stage to a Worksheet.

Preparing Python Worksheet

Before you start developing in the Snowflake Worksheets for Python, the worksheet needs to be prepared.

  1. Select the Python Worksheet
  2. Select a database
  3. Select a warehouse
  4. Define how yo want to run the worksheet
    • set the ‘Handler’ (the function to be called when executing the worksheet)
    • set the ‘Return type’ (the type of result returned by the ‘Handler’)
  5. Select the required packages
    • first select the package, then import the package in the worksheet
  6. Try to run the sample code to make sure things work.
Preparing Snowflake for Python Worksheet

Developing in Snowflake Worksheets for Python

When starting a new Python Worksheet, the sheet is filled with some sample code. This code can easily be changed according to your own needs. The code I needed to generate a simple Customer tabel is on GitHub.

  1. Add the code to the Python Worksheet and run the code
  2. View the results
  3. In this case the script (re-)creates and loads a table
    • view the results by selecting from the newly created table
Developing in Snowflake Worksheets for Python

Note: If you run code via the Snowsight UI there will be added some additional execution time +/- 10 to 20 secs extra as it executes as a temporary Stored Procedure each run. If you register it as a Permanent Stored Procedure it will eliminate this extra execution time.

Deploy the code as a Stored Procedure

With a simple click of a button your Python script can be deployed as a Permanent Stored Procedure.

  1. Click ‘Deploy’
  2. Name the Stored Procedure
    • Name
    • Description
    • Overwrite existing (Y/N)
  3. Call procedure
  4. Verify results in table
Deploy the Python code as a Stored Procedure

Closing Statements

Python Worksheets for Snowflake are now in Public Preview which means it’s available to everyone. A native Python Code Editor in SnowSight including IntelliSense with auto-complete, Sowpark and third-party library support both via either Anaconda or manual upload of custom packages.

Deploying the Python scripts as Python Stored Procedures is easy and makes it possible to automatically schedule them via Snowflake Tasks.

More details in the Snowflake documentation; ‘Writing Snowpark Code in Python Worksheets’.

Till next time.

Daan Bakboord – DaAnalytics

Bekijk ook:

Snowflake Data Cloud Summit - Wrap Up

Snowflake Snowflake Data Cloud Summit — Wrap Up

Snowflake Data Cloud Summit proved that after all this years the core idea remains the same and is still strong. Technology should serve and Snowflake makes things simple. One Single Unified Platform, one product and one engine. Ease of use and Govenance. Maximum efficiency and maximum simplicity.

Bring the processing of data to the data instead of the other way around. Snowflake as a Platform where you build and share your Data, Apps and AI Products. Your data never has to leave the Platform and Snowflake takes care of this Platform.

Lees verder »
Snowflake Data Cloud Summit - Day II

Snowflake Data Cloud Summit – Day II

The second day of Snowflake Summit in the Moscone Center in San Francisco started with Platform Keynote packed with announcements and demo’s. The announcements were not necessarily completely new, but a continuation of things Snowflake was already working on. Lot’s of Developments and Previews have made it to GA status and are now Generally Available to the public. A few announcements were made as wel.

This blogpost a summarization of my notes with were possible a link to Snowflake publications or documentation.

Lees verder »
Snowflake Data Cloud Summit - Day I

Snowflake Data Cloud Summit – Day I

The first day of Snowflake Summit in the Moscone Center in San Francisco was a day with the first two announcements made during the Opening Keynote. These two announcements built upon the theme for this years Summit; “The Era of Enterprise AI”

Lees verder »