Package: cloudy-sql
cloudy_sqlSnowflake SupportThe cloudy_sql is an Ipython Magics library and pandas extension that currently supports:a Magics function that allows users to easily execute SQL Queries in Snowflakewriting to an existing Snowflake table from a pandas dataframecreating a new Snowflake table from a pandas dataframeHow To Use cloudy_sqlInstallationpip install cloudy-sqlConfigurationUpon installation, open a jupyter notebook and run the following cell.%load_extcloudy_sqlAfter you run the cell, a configuration file will be created in your HOME directory.
The path to the configuration file is:$HOME/.cloudy_sql/configuration_profiles.ymlFor Windows user: use $USERPROFILE instead of $HOME variableThe configuration file is a YAML file with the following formatprofiles:snowflake:user:<your snowflake username>pass:<your snowflake password>acct:<your snowflake account>role:<your snowflake role>warehouse:<your snowflake warehouse>database:<your snowflake database>schema:<your snowflake schema>The user, pass, acct, database, schema, values all should be filled in with your desired Snowflake credentials and connection details. The variables in this
file serve as default arguments when calling a cloudy_sql method. Role and warehouse can be filled in as well, but they are optional arguments when connecting to Snowflake.APIThe intent has been to keep the API as simple as possible by minimally extending the pandas and IPython Magics APIs.Optional ArgumentsThere are two methods for passing optional arguments into a method.The configuration fileDirectly pass in the arguments when calling the methodThe variables saved in the configuration file serve as default arguments for the methods to use.
However, you tell the method to use different credentials by passing in arguments directly. The method will use the passed in arguments
instead of the default arguments saved inconfiguration_profiles.yml.For example, if I had thedatabasevariable saved in theconfiguration_profiles.ymlasdatabase_1, but passed indatabase = database_2directly into the method,
the method would usedatabase_2instead ofdatabase_1.However, if I choose to not directly pass adatabaseargument in, the method will use thedatabase_1because it is the default. The passed in arguments take priority over the default variables saved inconfiguration_profiles.yml.IPython Magics%%sql_to_snowflakeIPython Cell Magic to seamlessly connect to Snowflake and run a query in Snowflake and return a pandas DataFrame as the result.%%sql_to_snowflake [<destination_var>] [--username <snowflake_username>]
                   [--password <snowflake_password>] [--account <snowflake_account>]
                   [--role <snowflake_role>] [--warehouse <snowflake_warehouse>]
<SQL query>Parameters* <destination_var> (Optional [IPython line argument]): 
    Variable to store the query results. If none is given, the magic will return
    the first 10 rows of the pandas DataFrame if applicable.

* --params <params> (Optional [IPython line argument]):
    Parameters to be used in the SQL Query. Params must be passed in as a 
    dictionary string in the format {"param_name": "param_value"} or reference a 
    dictionary string defined in a previous cell. The use of the parameter in the 
    query should be indicated with {{param_name}}.

* --username <username> (Optional [IPython line argument]):
    If provided, the called method will connect to Snowflake with this username 
    instead of the default in the configuration file.

* --password <password> (Optional [IPython line argument]):
    If provided, the called method will connect to Snowflake with this password
    instead of the default in the configuration file.

* --account <account> (Optional [IPython line argument]):
    If provided, the called method will connect to Snowflake with this account 
    instead of the default in the configuration file.

* --role <role> (Optional [IPython line argument]):
    If provided, the called method will connect to Snowflake with this role 
    instead of the default in the configuration file.

* --warehouse <warehouse> (Optional [IPython line argument]):
    If provided, the called method will use this warehouse instead of the 
    default in the configuration file.write_snowflakepd.DataFrame.cloudy_sql.write_snowflake(table:str,database:str=None,schema:str=None,overwrite:bool=False,username:str=None,password:str=None,account:str=None,role:str=None,warehouse:str=None)This method writes to a Snowflake table and informs you on success. This method works when writing to either an existing Snowflake table or a previously non-existing Snowflake table.
If the table that you provide does not exist, this method creates a new Snowflake table and writes to it. If the table already exists, the DataFrame data is
appended to the existing table by default. If you would like to replace the table with the pandas DataFrame
setoverwrite = Truewhen calling the method. You can configure yourdatabaseandschemadefault values in the configuration file. You can also pass them in directly when calling the method.
The passed in values are used instead of the defaults in the configuration file.The goal of this method is to be used in tandem with%%sql_to_snowflake. You use the magic function to run your SQL query
that returns a pandas DataFrame. Then, you can transform the DataFrame and write the DataFrame to a Snowflake using this method.ExamplesUsing%%sql_to_snowflakemagic andwrite_snowflakemethodIn [1]:  %load_ext cloudy_sql

In [2]:  %%sql_to_snowflake df 
         SELECT * from db.schema.table
         
         Query successfully ran and results were stored to the 'df' destination variable.

In [3]:  df.head()
Out[3]:  
         CUSTOMER_ID	FIRST_NAME	LAST_NAME	FIRST_ORDER_DATE	MOST_RECENT_ORDER_DATE	NUMBER_OF_ORDERS
     0   	   1	   Michael	       P.	      2018-01-01	            2018-02-10	               2
     1   	   2	     Shawn	       M.	      2018-01-11	            2018-01-11	               1
     2   	   3	  Kathleen	       P.	      2018-01-02	            2018-03-11	               3
     3   	   4	     Jimmy	       C.	            None	                  None	               0
     4   	   5	 Katherine	       R.	            None	                  None	               0
     
In [4]: df.cloudy_sql.write_snowflake(table="test_cloudy_sql")

        Successfully wrote to the test_cloudy_sql Snowflake table

In [5]: %%sql_to_snowflake
        drop table if exists db.schema.test_cloudy_sql
        
        Successfully ran SQL Query in Snowflake
        
In [6]: %close_connectionThe above example runs a SQL query with%%sql_to_snowflakeand saves the results as a pandas DataFrame by passing
in the destination variabledf. The example then shows how to easily write thatdfto a Snowflake tableIn [4]. Also, the created table is droppedIn [5].
The connection is then closed by calling the%close_connectionmagicIn [6].Using%%sql_to_snowflakemagic with the--paramsinline argumentIn [1]:  %load_ext cloudy_sql

In [2]:  parameters = {'firstname': 'Michael', 'orders': '2'}
         
In [3]:  %%sql_to_snowflake --params $parameters
         SELECT * from db.schema.table
         WHERE FIRST_NAME = {{firstname}} and NUMBER_OF_ORDERS = {{orders}}
Out[3]:  
         CUSTOMER_ID	FIRST_NAME	LAST_NAME	FIRST_ORDER_DATE	MOST_RECENT_ORDER_DATE	NUMBER_OF_ORDERS
     0   	   1	   Michael	       P.	      2018-01-01	            2018-02-10	               2The above example runs a SQL query with passed-in variables. The variables are used directly in the SQL query by placing each one inside{{  }}. A dictionary stringparametersis passed in when the
magic is called by including a--paramsinline argument and placing a$to reference the dictionary string creating in the previous cell. There is no specifieddestination variable. Therefore, the magic prints out the resulting pandas dataframe.Using%%sql_to_snowflakemagic with the--paramsinline argumentIn [1]:  %load_ext cloudy_sql
         
In [2]:  %%sql_to_snowflake --params {'firstname': 'Michael', 'orders': '2'}
         SELECT * from db.schema.table
         WHERE FIRST_NAME = {{firstname}} and NUMBER_OF_ORDERS = {{orders}}
Out[2]:  
         CUSTOMER_ID	FIRST_NAME	LAST_NAME	FIRST_ORDER_DATE	MOST_RECENT_ORDER_DATE	NUMBER_OF_ORDERS
     0   	   1	   Michael	       P.	      2018-01-01	            2018-02-10	               2This example directly passes in the dictionary string when calling the--paramsinline argument.