forked from mahanteshimath/MYSNOWSIGHT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysnowsight.py
526 lines (445 loc) · 30.3 KB
/
mysnowsight.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import re
import streamlit as st
import pandas as pd
import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas
import time
from datetime import datetime
import os
import pathlib
import textwrap
from PIL import Image
import google.generativeai as genai
from concurrent.futures import ThreadPoolExecutor
st.set_page_config(
page_title="MYSNOWSIGHT",
page_icon=" ",
layout="wide",
initial_sidebar_state="expanded",
)
tab1, tab2, tab3 , tab4,tab5 = st.tabs(["PARALLEL EXECUTE", "LOAD FILE", "DOCUMENT AI", "GENERATE DDL","REPLICATE DATABASE"])
# Sidebar for Snowflake credentials
with st.sidebar:
st.markdown("[![Foo](https://cdn2.iconfinder.com/data/icons/social-media-2285/512/1_Linkedin_unofficial_colored_svg-48.png)](https://www.linkedin.com/in/mahantesh-hiremath/) Connect me.")
st.sidebar.header("Snowflake Credentials")
account = st.sidebar.text_input("Account")
role = st.sidebar.text_input("Role")
warehouse = st.sidebar.text_input("Warehouse")
database = st.sidebar.text_input("Database")
schema = st.sidebar.text_input("Schema")
user = st.sidebar.text_input("User")
password = st.sidebar.text_input("Password", type="password")
# Create a Snowflake connection function
def create_snowflake_connection(account, role, warehouse, database, schema, user, password):
try:
conn = snowflake.connector.connect(
account=account,
role=role,
warehouse=warehouse,
database=database,
schema=schema,
user=user,
password=password,
client_session_keep_alive=True
)
st.toast("Connection to Snowflake successfully!", icon='🎉')
time.sleep(.5)
st.balloons()
except Exception as e:
st.error(f"Error connecting to Snowflake: {str(e)}")
return conn
with tab1:
# Function to execute queries in parallel
def execute_queries(queries):
with ThreadPoolExecutor(max_workers=len(queries)) as executor:
results = list(executor.map(execute_single_query, queries))
return results
# Function to execute a single query
def execute_single_query(query):
connection = create_snowflake_connection(account, role, warehouse, database, schema, user, password)
cursor = connection.cursor()
start_time = time.time()
try:
cursor.execute(query)
result = cursor.fetchall()
elapsed_time = time.time() - start_time
return f"Query: {query}\nResult: {result}\nTime taken: {elapsed_time:.2f} seconds"
except connection.connector.errors.ProgrammingError as e:
return f"Query: {query}\nError: {str(e)}\nTime taken: {elapsed_time:.2f} seconds"
finally:
cursor.close()
connection.close()
# Streamlit UI
# Connect button
def main():
if st.sidebar.button("Connect"):
#st.sidebar.set_visible(False)
connection = create_snowflake_connection(account, role, warehouse, database, schema, user, password)
st.title("My Snowsight")
#link to YouTube channel
# st.markdown(" 👉 [🎥Visit my YouTube channel for more details](https://bit.ly/atozaboutdata)")
st.markdown('<p style="color: yellow;">👉🎥 Visit my YouTube channel for more details <a href="https://bit.ly/atozaboutdata">🎥click</a></p>', unsafe_allow_html=True)
# Text area to input queries
queries = st.text_area("Input queries separated by ; to execute all in parallel",height=200)
query_list = [q.strip() for q in queries.split(';') if q.strip()]
if st.button("Execute Queries"):
if query_list:
with st.spinner("Executing all queries..."):
results = execute_queries(query_list)
st.success("All queries executed!")
for res in results:
st.write(res)
if __name__ == "__main__":
main()
with tab2:
def main():
st.title('Upload File to Snowflake')
# st.markdown(" 👉 [🎥Visit my YouTube channel for more details](https://bit.ly/atozaboutdata)")
st.markdown('<p style="color: yellow;">👉🎥 Visit my YouTube channel for more details <a href="https://bit.ly/atozaboutdata">🎥click</a></p>', unsafe_allow_html=True)
file = st.file_uploader('Upload file', type=['xls', 'xlsx', 'csv', 'txt'])
if file is not None:
# Read the file
file_extension = file.name.split('.')[-1]
if file_extension.lower() in ['xls', 'xlsx', 'csv', 'txt']:
data = pd.read_excel(file) if file_extension.lower() in ['xls', 'xlsx'] else pd.read_csv(file, encoding='latin-1')
st.subheader('Preview of Uploaded Data')
st.write(data.head())
# Save data to Snowflake
conn = create_snowflake_connection(account, role, warehouse, database, schema, user, password)
if conn:
st.info('Connected to Snowflake!')
table_name = st.text_input('Enter table name in Snowflake')
if st.button('Save to Snowflake'):
try:
data_f=pd.DataFrame(data)
success, nchunks, nrows, _ = write_pandas(conn=conn,df=data_f,table_name=table_name,database=database,schema=schema,auto_create_table=True)
st.success(f'Dataloaded to snowflake table: {table_name} rows : {nrows}')
except Exception as e:
st.error(f'Error: {str(e)}')
else:
st.error('Unable to connect to Snowflake. Please check your credentials.')
if __name__ == '__main__':
main()
with tab3:
def main():
def get_gemini_response(input,image,prompt):
model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content([input,image[0],prompt])
return response.text
def input_image_setup(uploaded_file):
# Check if a file has been uploaded
if uploaded_file is not None:
# Read the file into bytes
bytes_data = uploaded_file.getvalue()
image_parts = [
{
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
"data": bytes_data
}
]
return image_parts
else:
raise FileNotFoundError("No file uploaded")
st.markdown('<p style="color: yellow;">👉🎥 Visit my YouTube channel for more details <a href="https://bit.ly/atozaboutdata">🎥click</a></p>', unsafe_allow_html=True)
st.title('Document AI: Upload invoices and ask question')
left, right= st.columns(2)
with right:
uploaded_file = st.file_uploader("upload image",type=["jpg", "jpeg", "png"])
image=""
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.", use_column_width=True)
with left:
google_api_key = st.text_input("INPUT GOOGLE_API_KEY",key="google_api_key")
genai.configure(api_key=google_api_key)
st.header("Invoice reader Application")
input=st.text_input("Ask about invoice: ",key="input")
submit=st.button("Submit",key="submit")
input_prompt = """
You are an expert in understanding invoices.
You will receive input images or PDF as invoices &
you will have to answer questions based on the input image
"""
if submit:
image_data = input_image_setup(uploaded_file)
response=get_gemini_response(input_prompt,image_data,input)
st.subheader("Answer: ")
st.write(response)
# current_timestamp = pd.Timestamp.now() # Correct usage of datetime module
date_time =datetime.fromtimestamp(time.time())
# Dataframe creation with file details, question, response, and timestamp
data_to_save = pd.DataFrame({
'FILENAME': [uploaded_file.name if uploaded_file else None],
'QUESTION': [input],
'RESPONSE': [response],
'TIMESTAMP':[date_time]
})
st.subheader('Preview of Uploaded Data')
st.write(data_to_save.head())
table_name='INVOICE_DATA'
write_pandas(conn=create_snowflake_connection(account, role, warehouse, database, schema, user, password),df=data_to_save,table_name=table_name,database=database,schema=schema,auto_create_table=True)
if __name__ == '__main__':
main()
with tab4:
def main():
st.markdown('<p style="color: yellow;">👉🎥 Visit my YouTube channel for more details <a href="https://bit.ly/atozaboutdata">🎥click</a></p>', unsafe_allow_html=True)
st.title(":balloon: :balloon: Generate DDL :balloon: :balloon:")
st.write(":balloon: :balloon: This is to Generate DDL :balloon: :balloon:")
if all([account, role, warehouse, database, schema, user, password]):
conn = create_snowflake_connection(account, role, warehouse, database, schema, user, password)
cursor = conn.cursor()
chk_box = st.selectbox("Do you want to Generate DDL for entire DB?",options=["YES","NO"] , index=1)
if conn and chk_box=='NO':
st.info('Connected to Snowflake!')
db_list = cursor.execute("SHOW DATABASES")
db_names = [db[1] for db in db_list]
db_name = st.selectbox("Select Database", db_names, key=f"selected_dbnames")
if db_name:
sch_list = cursor.execute(f"SHOW SCHEMAS IN DATABASE {db_name}")
sch_names = [sch[1] for sch in sch_list]
sch_name = st.selectbox("Select Schema", sch_names, index=0, key=f"schemaname_list")
if sch_name:
entity_types = [
"Dynamic Table", "Event Table", "File Format", "Function",
"Iceberg Table", "Masking Policy", "Password Policy", "Pipe",
"Procedure", "Row Access Policy", "Sequence", "Session Policy",
"Stream", "Table", "Tag", "Task", "View"
]
entity_type = st.selectbox("Select Object Type", entity_types)
if entity_type:
if entity_type =='Function' or entity_type =='Procedure':
ent_list = cursor.execute(f"SHOW USER {entity_type}S IN SCHEMA {db_name}.{sch_name}")
ent_names = [ent[8] for ent in ent_list]
else:
ent_list = cursor.execute(f"SHOW {re.sub('Policy','Policie',entity_type)}S IN SCHEMA {db_name}.{sch_name}")
ent_names = [ent[1] for ent in ent_list]
# ent_names.insert(0, "ALL")
selected_entities = st.multiselect(f"Select {entity_type}s", ent_names, key=f"selected_entity_list")
if selected_entities:
if 'Policy' in entity_type:
ent_type = 'Policy'
else:
ent_type = re.sub(" ", "_", entity_type)
if st.button('Generate DDL'):
ddl_statements = []
for entity_name in selected_entities:
ent_name = re.sub("(.*?) RETURN.*", "\\1", entity_name)
ddl_query = f"SELECT GET_DDL('{ent_type}', '{db_name}.{sch_name}.{ent_name}', true) AS DDL"
df = cursor.execute(ddl_query)
ddl_statements.append(df.fetchone()[0])
combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl_statements)
st.write("### Generate DDL")
language = "PYTHON" if "python" in combined_ddl.lower() else "SQL"
st.code(combined_ddl, language=language)
if conn and chk_box=='YES':
st.info('Connected to Snowflake!')
db_list = cursor.execute("SHOW DATABASES")
db_names = [db[1] for db in db_list]
db_name = st.selectbox("Select Database", db_names, key=f"selected_dbnames")
if db_name and st.button('Generate DDL'):
ddl_statements = []
ddl_query = f"SELECT GET_DDL('DATABASE', '{db_name}', true) AS DDL"
df = cursor.execute(ddl_query)
ddl_statements.append(df.fetchone()[0])
combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl_statements)
st.write("### Generate DDL")
language = "PYTHON" if "python" in combined_ddl.lower() else "SQL"
st.code(combined_ddl, language=language)
# Close Snowflake cursor and connection
cursor.close()
if __name__ == '__main__':
main()
with tab5:
def main():
st.markdown('<p style="color: yellow;">👉🎥 Visit my YouTube channel for more details <a href="https://bit.ly/atozaboutdata">🎥click</a></p>', unsafe_allow_html=True)
st.title(":balloon: :balloon: Replicate databse from one snowflake to another :balloon: :balloon:")
def test_connection(account, role, warehouse, database, schema, user, password):
try:
conn = create_snowflake_connection(account, role, warehouse, database, schema, user, password)
if conn:
st.success("Connection successful!")
return conn
else:
st.error("Connection failed. Please check your credentials.")
return None
except Exception as e:
st.error(f"Error testing connection: {str(e)}")
return None
col1, col2 = st.columns(2)
with col1:
st.header("Source Snowflake Credentials")
source_account = st.text_input("Source Account")
source_role = st.text_input("Source Role")
source_warehouse = st.text_input("Source Warehouse")
source_database = st.text_input("Source Database")
source_schema = st.text_input("Source Schema")
source_user = st.text_input("Source User")
source_password = st.text_input("Source Password", type="password")
if st.button("Test Source Connection"):
source_conn = test_connection(source_account, source_role, source_warehouse, source_database, source_schema, source_user, source_password)
if source_conn:
st.toast("Source Connection to Snowflake successfully!", icon='🎉')
time.sleep(0.5)
st.balloons()
# Generate DDL for source database
# cursor = source_conn.cursor()
# ddl = []
# ddl_q = f"SELECT GET_DDL('DATABASE', '{source_database}', true) AS DDL"
# df_q = cursor.execute(ddl_q)
# ddl.append(df_q.fetchone()[0])
# combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl)
# st.write("### Generate DDL")
# language = "PYTHON" if "python" in combined_ddl.lower() else "SQL"
# st.code(combined_ddl, language=language)
with col2:
st.header("Destination Snowflake Credentials")
dest_account = st.text_input("Destination Account")
dest_role = st.text_input("Destination Role")
dest_warehouse = st.text_input("Destination Warehouse")
dest_database = st.text_input("Destination Database")
dest_schema = st.text_input("Destination Schema")
dest_user = st.text_input("Destination User")
dest_password = st.text_input("Destination Password", type="password")
if st.button("Test Destination Connection"):
dest_conn = test_connection(dest_account, dest_role, dest_warehouse, dest_database, dest_schema, dest_user, dest_password)
if dest_conn:
st.toast("Destination Connection to Snowflake successfully!", icon='🎉')
time.sleep(0.5)
# cursor = dest_conn.cursor()
# ddl = []
# ddl_q = f"SELECT CURRENT_ACCOUNT() CURRENT_ACCOUNT"
# df_q = cursor.execute(ddl_q)
# ddl.append(df_q.fetchone()[0])
# combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl)
# st.write("### Generate DDL")
# language = "PYTHON" if "python" in combined_ddl.lower() else "SQL"
# st.code(combined_ddl, language=language)
# if test_connection(source_account, source_role, source_warehouse, source_database, source_schema, source_user, source_password):
# cursor = source_conn.cursor()
# ddl = []
# ddl_q = f"SELECT GET_DDL('DATABASE', '{source_database}', true) AS DDL"
# df_q = cursor.execute(ddl_q)
# ddl.append(df_q.fetchone()[0])
# combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl)
# if test_connection(dest_account, dest_role, dest_warehouse, dest_database, dest_schema, dest_user, dest_password):
# st.write("### Generate DDL")
# language = "PYTHON" if "python" in combined_ddl.lower() else "SQL"
# st.code(combined_ddl, language=language)
# Function to replicate data between two Snowflake accounts
def replicate_data(source_conn, dest_conn, source_database, source_schema, dest_database, dest_schema):
try:
# REPLICATE STRUCTURE OF ALL OBJECTS
source_cursor = source_conn.cursor()
ddl = []
#ddl_q = f"SELECT GET_DDL('DATABASE', '{source_database}',TRUE) AS DDL"
ddl_q = f'''
DECLARE
CUR CURSOR FOR SELECT CONCAT_WS('.',TABLE_CATALOG,TABLE_SCHEMA,'"'||TABLE_NAME||'"') AS NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE '%TEMP_VIEW_DEFS%'
AND TABLE_SCHEMA='{source_schema}' and IS_TEMPORARY='NO'
ORDER BY CREATED ASC;
BEGIN
CREATE OR REPLACE TEMPORARY TABLE TEMP_VIEW_DEFS(VIEW_NAME TEXT, DEFINITION TEXT);
FOR rec IN CUR DO
EXECUTE IMMEDIATE REPLACE('INSERT INTO TEMP_VIEW_DEFS(VIEW_NAME, DEFINITION)
SELECT ''<VIEW_NAME>'', GET_DDL(''TABLE'', ''<VIEW_NAME>'')'
,'<VIEW_NAME>'
,rec.NAME);
END FOR;
LET rs RESULTSET := (SELECT LISTAGG(DEFINITION,' ') DDL FROM TEMP_VIEW_DEFS);
RETURN TABLE(rs);
END;
'''
df_q = source_cursor.execute(ddl_q)
ddl.append(df_q.fetchone()[0])
combined_ddl = "\n\n-------------------------------------------------------------------------------------------\n\n".join(ddl)
dest_cursor=dest_conn.cursor()
combined_ddl=f"BEGIN create or replace database {source_database}; create or replace schema {source_database}.{source_schema}; USE {source_database}.{source_schema}; {combined_ddl} END;"
dest_cursor.execute(combined_ddl)
st.toast("Structure of all source Snowflake created in Destination!", icon='🎉')
time.sleep(0.5)
# source_tables = source_cursor.execute(f"SHOW TABLES IN {source_schema}")
# table_names = [table[1] for table in source_tables]
# QRY_TBL=f'''
# SELECT CONCAT_WS('.',TABLE_CATALOG,TABLE_SCHEMA,'"'||TABLE_NAME||'"') AS NAME
# FROM INFORMATION_SCHEMA.TABLES
# WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE '%TEMP_VIEW_DEFS%'
# AND TABLE_SCHEMA='{source_schema}' and IS_TEMPORARY='NO'
# ORDER BY CREATED ASC
# '''
# Retrieve tables from source database/schema
source_cursor = source_conn.cursor()
Q_TBLS=f'''
BEGIN
SHOW TABLES IN {source_database}.{source_schema};
LET A := SQLID;
LET rs RESULTSET :=(SELECT * FROM TABLE(RESULT_SCAN(:A)) WHERE "kind" ='TABLE');
RETURN TABLE(rs);
END;
'''
# source_tables = source_cursor.execute(f"SHOW TABLES IN {source_schema}")
source_tables = source_cursor.execute(Q_TBLS)
table_names = [table[1] for table in source_tables]
# Replicate each table from source to destination
total_cnt_tbls = len(table_names)
st.success(f"Total Tables to replicate: {total_cnt_tbls}")
cnt_tbls=0
for table_name in table_names:
# Construct the fully qualified table name
full_table_name = f'"{source_database}"."{source_schema}"."{table_name}"'
full_table_name=full_table_name.strip("'")
query = f"SELECT * FROM {full_table_name}"
cnt_tbls=cnt_tbls+1;
df = pd.read_sql(query, source_conn)
success, nchunks, nrows, _ = write_pandas(conn=dest_conn, df=df, table_name=table_name, database=dest_database, schema=dest_schema)
st.success(f'Source table: {table_name} replicated in Destination DB with rows : {nrows}')
st.toast("Data of all source Snowflake created in Destination!", icon='🎉')
time.sleep(0.5)
st.success(f"Data replication successful 🎉🎉🎉. Total {cnt_tbls}/{total_cnt_tbls} tables replicated.")
except Exception as e:
st.error(f"Error replicating data: {str(e)}")
# Execute replication process
if st.button("Replicate Data"):
try:
# Create connections to source and destination Snowflake accounts
source_conn = create_snowflake_connection(source_account, source_role, source_warehouse, source_database, source_schema, source_user, source_password)
dest_conn = create_snowflake_connection(dest_account, dest_role, dest_warehouse, dest_database, dest_schema, dest_user, dest_password)
if source_conn and dest_conn:
replicate_data(source_conn, dest_conn, source_database, source_schema, dest_database, dest_schema)
st.toast("All tables replicated successfully. Hurrayyaaaa !!", icon='🎉')
time.sleep(1)
st.balloons()
else:
st.error("Unable to establish connections to source and/or destination Snowflake accounts.")
except Exception as e:
st.error(f"Error: {str(e)}")
if __name__ == '__main__':
main()
# Adding a footer
footer="""<style>
a:link , a:visited{
color: blue;
background-color: transparent;
text-decoration: underline;
}
a:hover, a:active {
color: red;
background-color: transparent;
text-decoration: underline;
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: white;
color: black;
text-align: center;
}
</style>
<div class="footer">
<p>Developed with ❤️ by <a style='display: inline; text-align: center;' href="https://bit.ly/atozaboutdata" target="_blank">MAHANTESH HIREMATH</a></p>
</div>
"""
st.markdown(footer,unsafe_allow_html=True)