Member-only story

How to Generate Snowflake Stored Procs via Python Worksheets

Cristian Scutaru
5 min readNov 2, 2023

Python Worksheets are intended to make it easier to automatically generate stored procedures in Snowflake directly from pure Python code. When you run a Python worksheet, an on-the-fly temporary stored proc is created as a wrapper around your Python code and executed instead. When you Deploy the Python worksheet, you may open a SQL worksheet with a CREATE PROCEDURE around your Python code, and further customize it.

Read here the post for free if you do not have a Medium subscription.

Create a Python Worksheet

In Snowsight, create a new Python Worksheet (instead of a traditional SQL Worksheet), then copy and paste the following code:

import snowflake.snowpark as snowpark
from faker import Faker # add to Packages (already included in Anaconda)

# generate fake rows but with realistic test synthetic data
def main(session: snowpark.Session):
f = Faker()
output = [[f.name(), f.address(), f.city(), f.state(), f.email()]
for _ in range(1000)]
df = session.create_dataframe(output,
schema=["name", "address", "city", "state", "email"])
df.show()
return df

Snowpark is always required, and the default main handler takes the current Snowflake session as a parameter. We use this session here to create a Snowpark…

--

--

Cristian Scutaru
Cristian Scutaru

Written by Cristian Scutaru

World-class expert in Snowflake Data Cloud. Former Snowflake "Data Superhero" and SnowPro SME (Subject Matter Expert). 7x SnowPro certification exams.

Responses (1)