# 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](https://yuthakarn.medium.com/how-to-not-show-credential-in-jupyter-notebook-c349f9278466) or [python-dotenv](https://pypi.org/project/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)
#...
```
Raw data
{
"_id": null,
"home_page": "https://github.com/dtPaTh/snowflakemagic",
"name": "snowflakemagic",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "Notebook, Jupyter, Snowflake",
"author": "Patrick Thurner",
"author_email": "patrick.thurner@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/90/e7/67998e3f38a0b47e32fb4f9c4832b10e8d4bcad7d42176b12f518def80fc/snowflakemagic-1.0.1.tar.gz",
"platform": null,
"description": "# Snowflake Magic \r\n\r\nAn ipython magic function to simplify usage of Snowflake SQL in your notebooks.\r\n\r\nExample\r\n```\r\nimport pandas as pd\r\nresult = %snowflake SELECT timestamp, value FROM mytable;\r\ndf = pd.DataFrame(result)\r\ndf.plot.line();\r\n```\r\n\r\n## Setup and Configuration\r\n\r\n### Install the extension\r\n\r\n```\r\npip install snowflakemagic\r\n```\r\n\r\n### Load extension\r\n```\r\n%reload_ext snowflakemagic\r\n```\r\n\r\n## Available magic functions\r\n\r\n### %snowflake_auth\r\nInline function connecting to your snowflake account. Reads connection parameters from .env file:\r\n\r\nYou can either authenticate via SSO, which opens an external browser, or using credentials. \r\n\r\nProvide your snowflake account details:\r\n\r\n```\r\nsnowflake_account=\"<YOUR-SNOWFLAKE-ACCOUNT>\"\r\n```\r\n\r\nIf you want to connect via sso, provide your sso username: \r\n\r\n```\r\nsnowflake_ssouser=\"<YOUR-SSO-USERNAME>\"\r\n```\r\n\r\nIf you want to connect via use-credentials, provide the password, otherwise SSO authentication is used.\r\n```\r\nsnowflake_user=\"<YOUR-USERNAME>\"\r\nsnowflake_password=\"<YOUR-PASSWORD>\"\r\n```\r\n\r\nFor more details on .env file see [How to NOT embedded credential in Jupyter notebook](https://yuthakarn.medium.com/how-to-not-show-credential-in-jupyter-notebook-c349f9278466) or [python-dotenv](https://pypi.org/project/python-dotenv/)\r\n\r\n\r\n### %%snowflake, %snowflake or %snowflake_script\r\n* Executes a snowflake query/script and returns the result as a json object. \r\n* Multiple queries/statements separated by ; can be exceuted, but only last result will be returned. \r\n* A query MUST end with a semi-colon (;)\r\n\r\n### Example 1 \r\nQuery in code-cell\r\n```\r\n%%snowflake my_result\r\nSELECT * \r\n FROM xyz;\r\n```\r\n\r\n.. use result in another code cell:\r\n```\r\nimport pandas as pd\r\n\r\n#put result into a dataframe\r\ndf = pd.DataFrame(my_result)\r\n\r\n#...\r\n```\r\n\r\n### Example 2 - Inline query\r\n```\r\nimport pandas as pd\r\n\r\nmy_result = %snowflake SELECT * FROM xyz;\r\ndf = pd.DataFrame(my_result)\r\n\r\n#...\r\n```\r\n\r\n### Example 3 - From script\r\nQuery using external query script files e.g. myscript.snowql\r\n```\r\nSELECT * FROM xyz;\r\n```\r\n\r\nThen in your code-cell, pass the script name \r\n```\r\nimport pandas as pd\r\n\r\nmy_result = %snowflake_script myscript.snowql\r\ndf = pd.DataFrame(my_result)\r\n\r\n#...\r\n```\r\n\r\n### Example 4 - Parameterized script\r\nQuery using external query script files e.g. myscript.snowql which can be parameterized\r\n```\r\nSELECT * FROM xyz WHERE mycolumn=@MYVALUE@@;\r\n```\r\n\r\nThen in your code-cell, pass the script name \r\n```\r\nimport pandas as pd\r\n\r\nmy_result = %snowflake_script myscript.snowql @@MYVALUE@@=test\r\ndf = pd.DataFrame(my_result)\r\n\r\n#...\r\n```\r\n\r\n### Example 5 - Chaining multiple scripts\r\nYou can also chain multiple scripts\r\n\r\nmycte.snowql\r\n```\r\nWITH\r\n my_cte AS (\r\n SELECT col_1, col_2\r\n FROM xyz\r\n )\r\n```\r\n\r\nmyscript.snowql\r\n```\r\nSELECT * FROM my_cte WHERE col_1=@MYVALUE@@;\r\n```\r\n\r\nThen in your code-cell, you can append the various script files\r\n```\r\nimport pandas as pd\r\n\r\nmy_result = %snowflake_script mycte.snowql<<myscript.snowql @@MYVALUE@@=test\r\ndf = pd.DataFrame(my_result)\r\n\r\n#...\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "IPython magic function for Snowflake Query Language (SQL)",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/dtPaTh/snowflakemagic"
},
"split_keywords": [
"notebook",
" jupyter",
" snowflake"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ae8fba6ab09031513e10195860fd9a8572444e5e410a70a4eae70d327e391169",
"md5": "fbb1bd1ead3d04b90302fab686520f79",
"sha256": "00b35e5e778461f392cbda29b67473b6ea958e256e54c6814c9a55e89d07c0c1"
},
"downloads": -1,
"filename": "snowflakemagic-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fbb1bd1ead3d04b90302fab686520f79",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4025,
"upload_time": "2024-06-28T13:37:51",
"upload_time_iso_8601": "2024-06-28T13:37:51.630489Z",
"url": "https://files.pythonhosted.org/packages/ae/8f/ba6ab09031513e10195860fd9a8572444e5e410a70a4eae70d327e391169/snowflakemagic-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "90e767998e3f38a0b47e32fb4f9c4832b10e8d4bcad7d42176b12f518def80fc",
"md5": "61a0123f29ee872d1851d39fcb4d0fd3",
"sha256": "dd79bf992dfe42ecd94da456147786906439aba94c240f0b5daea42e36a882c8"
},
"downloads": -1,
"filename": "snowflakemagic-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "61a0123f29ee872d1851d39fcb4d0fd3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3688,
"upload_time": "2024-06-28T13:37:53",
"upload_time_iso_8601": "2024-06-28T13:37:53.012215Z",
"url": "https://files.pythonhosted.org/packages/90/e7/67998e3f38a0b47e32fb4f9c4832b10e8d4bcad7d42176b12f518def80fc/snowflakemagic-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-28 13:37:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dtPaTh",
"github_project": "snowflakemagic",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "snowflakemagic"
}