Skip to main content

Snowflake Magic

An ipython magic function to simplify usage of Snowflake SQL in your notebooks.

Example

import pandas as pd
result = %snowflake SELECT timestamp, value FROM mytable;
df = pd.DataFrame(result)
df.plot.line();

Setup and Configuration

Install the extension

pip install snowflakemagic

Load extension

%reload_ext snowflakemagic

Available magic functions

%snowflake_auth

Inline function connecting to your snowflake account. Reads connection parameters from .env file:

You can either authenticate via SSO, which opens an external browser, or using credentials.

Provide your snowflake account details:

snowflake_account="<YOUR-SNOWFLAKE-ACCOUNT>"

If you want to connect via sso, provide your sso username:

snowflake_ssouser="<YOUR-SSO-USERNAME>"

If you want to connect via use-credentials, provide the password, otherwise SSO authentication is used.

snowflake_user="<YOUR-USERNAME>"
snowflake_password="<YOUR-PASSWORD>"

For more details on .env file see How to NOT embedded credential in Jupyter notebook or python-dotenv

%%snowflake, %snowflake or %snowflake_script

  • Executes a snowflake query/script and returns the result as a json object.
  • Multiple queries/statements separated by ; can be exceuted, but only last result will be returned.
  • A query MUST end with a semi-colon (;)

Example 1

Query in code-cell

%%snowflake my_result
SELECT * 
    FROM xyz;

.. use result in another code cell:

import pandas as pd

#put result into a dataframe
df = pd.DataFrame(my_result)

#...

Example 2 - Inline query

import pandas as pd

my_result = %snowflake SELECT * FROM xyz;
df = pd.DataFrame(my_result)

#...

Example 3 - From script

Query using external query script files e.g. myscript.snowql

SELECT * FROM xyz;

Then in your code-cell, pass the script name

import pandas as pd

my_result = %snowflake_script myscript.snowql
df = pd.DataFrame(my_result)

#...

Example 4 - Parameterized script

Query using external query script files e.g. myscript.snowql which can be parameterized

SELECT * FROM xyz WHERE mycolumn=@MYVALUE@@;

Then in your code-cell, pass the script name

import pandas as pd

my_result = %snowflake_script myscript.snowql @@MYVALUE@@=test
df = pd.DataFrame(my_result)

#...

Example 5 - Chaining multiple scripts

You can also chain multiple scripts

mycte.snowql

WITH
    my_cte AS (
        SELECT col_1, col_2
            FROM xyz
    )

myscript.snowql

SELECT * FROM my_cte WHERE col_1=@MYVALUE@@;

Then in your code-cell, you can append the various script files

import pandas as pd

my_result = %snowflake_script mycte.snowql<<myscript.snowql @@MYVALUE@@=test
df = pd.DataFrame(my_result)

#...

Release files for snowflakemagic 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snowflakemagic 1.0.1
File Size Uploaded
snowflakemagic-1.0.1.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for snowflakemagic 1.0.1
File Interpreter ABI Platform
snowflakemagic-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / snowflakemagic-1.0.1.tar.gz

Download URL snowflakemagic-1.0.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
dd79bf992dfe42ecd94da456147786906439aba94c240f0b5daea42e36a882c8
BLAKE2b-256 checksum
How to use checksums
90e767998e3f38a0b47e32fb4f9c4832b10e8d4bcad7d42176b12f518def80fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / snowflakemagic-1.0.1-py3-none-any.whl

Download URL snowflakemagic-1.0.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00b35e5e778461f392cbda29b67473b6ea958e256e54c6814c9a55e89d07c0c1
BLAKE2b-256 checksum
How to use checksums
ae8fba6ab09031513e10195860fd9a8572444e5e410a70a4eae70d327e391169
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page