forked from kaarthik108/snowChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnow_connect.py
52 lines (43 loc) · 1.56 KB
/
snow_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st
from snowflake.snowpark.session import Session
from snowflake.snowpark.version import VERSION
from typing import Any, Dict
class SnowflakeConnection:
"""
This class is used to establish a connection to Snowflake.
Attributes
----------
connection_parameters : Dict[str, Any]
A dictionary containing the connection parameters for Snowflake.
session : snowflake.snowpark.Session
A Snowflake session object.
Methods
-------
get_session()
Establishes and returns the Snowflake connection session.
"""
def __init__(self):
self.connection_parameters = self._get_connection_parameters_from_env()
self.session = None
@staticmethod
def _get_connection_parameters_from_env() -> Dict[str, Any]:
connection_parameters = {
"account": st.secrets["ACCOUNT"],
"user": st.secrets["USER_NAME"],
"password": st.secrets["PASSWORD"],
"warehouse": st.secrets["WAREHOUSE"],
"database": st.secrets["DATABASE"],
"schema": st.secrets["SCHEMA"],
"role": st.secrets["ROLE"],
}
return connection_parameters
def get_session(self):
"""
Establishes and returns the Snowflake connection session.
Returns:
session: Snowflake connection session.
"""
if self.session is None:
self.session = Session.builder.configs(self.connection_parameters).create()
self.session.sql_simplifier_enabled = True
return self.session