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)

#...

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snowflakemagic-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snowflakemagic-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file snowflakemagic-1.0.0.tar.gz.

File metadata

  • Download URL: snowflakemagic-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for snowflakemagic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e5a4b9129363fc15cd6b14458f036197d6594c778385c55d287987779b885c09
MD5 a0fb4e926e7a61c5393451125f9595bb
BLAKE2b-256 735226694d613612aef0c2fbb7ce9a4ed5695eead95fcb5d5884cfdef4ed71b9

See more details on using hashes here.

File details

Details for the file snowflakemagic-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: snowflakemagic-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for snowflakemagic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec0c6995e104290d0786126a8d1a1802bee0012f3cbf979c3cc92c63e5d4e1e7
MD5 10bf3a039a71386cd21eafaeccdcd9da
BLAKE2b-256 9632b716733c799d304e5705f89c7c1a459436b160e862b43fbd33743dd4a9bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page