anumaurya114exp commited on
Commit
8a4e4ca
·
1 Parent(s): 31528fe

added result multiple rows to show and csv file download result (half part)

Browse files
Files changed (1) hide show
  1. app.py +34 -2
app.py CHANGED
@@ -9,7 +9,7 @@ import os
9
  import warnings
10
 
11
 
12
- from persistStorage import saveLog, getAllLogFilesPaths
13
  from config import *
14
  from constants import *
15
  from utils import *
@@ -19,7 +19,7 @@ from queryHelperManagerCoT import QueryHelperChainOfThought
19
 
20
 
21
  pd.set_option('display.max_columns', None)
22
- pd.set_option('display.max_rows', 10)
23
 
24
  # Filter out all warning messages
25
  warnings.filterwarnings("ignore")
@@ -114,6 +114,33 @@ eg\n select * from schema.table limit 20\n\n"""
114
  sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
115
  return sql, disclaimerOutputStripping
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  def testSQL(sql):
118
  global dbEngine, queryHelper
119
 
@@ -235,6 +262,11 @@ with gr.Blocks() as demo:
235
  text_button = gr.Button("RUN QUERY")
236
  clear = gr.ClearButton([text_input, text_output])
237
  text_button.click(testSQL, inputs=text_input, outputs=text_output)
 
 
 
 
 
238
  # screen 3 : To set creds, schema, tables and columns
239
  with gr.Tab("Setup"):
240
  gr.Markdown("""<h1><center> Run Query </center></h1>""")
 
9
  import warnings
10
 
11
 
12
+ from persistStorage import saveLog, getAllLogFilesPaths, getNewCsvFilePath, removeAllCsvFiles
13
  from config import *
14
  from constants import *
15
  from utils import *
 
19
 
20
 
21
  pd.set_option('display.max_columns', None)
22
+ pd.set_option('display.max_rows', None)
23
 
24
  # Filter out all warning messages
25
  warnings.filterwarnings("ignore")
 
114
  sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
115
  return sql, disclaimerOutputStripping
116
 
117
+ def getResultFilePath():
118
+ global dbEngine, queryHelper
119
+ sql, disclaimerOutputStripping = preProcessSQL(sql=sql)
120
+ if not isDataQuery(sql):
121
+ return "Sorry not allowed to run. As the query modifies the data."
122
+ try:
123
+ dbEngine2 = DbEngine(dbCreds)
124
+ dbEngine2.connect()
125
+ conn = dbEngine2.getConnection()
126
+ df = pd.read_sql_query(sql, con=conn)
127
+
128
+ dbEngine2.disconnect()
129
+ # return disclaimerOutputStripping + str(pd.DataFrame(df))
130
+ except Exception as e:
131
+ # errorMessage = {"function":"testSQL","error":str(e), "userInput":sql}
132
+ # saveLog(errorMessage, 'error')
133
+ dbEngine2.disconnect()
134
+ df = pd.DataFrame()
135
+ # print(f"Error occured during running the query {sql}.\n and the error is {str(e)}")
136
+
137
+ removeAllCsvFiles()
138
+ csvFilePath = getNewCsvFilePath()
139
+ df.to_csv(csvFilePath, index=False)
140
+ downloadableFilesPaths = getAllLogFilesPaths()
141
+ fileComponent = gr.File(csvFilePath)
142
+ return fileComponent
143
+
144
  def testSQL(sql):
145
  global dbEngine, queryHelper
146
 
 
262
  text_button = gr.Button("RUN QUERY")
263
  clear = gr.ClearButton([text_input, text_output])
264
  text_button.click(testSQL, inputs=text_input, outputs=text_output)
265
+
266
+ csvFileComponent = gr.File([], file_count='multiple')
267
+ downloadCsv = gr.Button("Get result as csv")
268
+ downloadCsv.click(getResultFilePath, inputs=text_input, outputs=csvFileComponent)
269
+
270
  # screen 3 : To set creds, schema, tables and columns
271
  with gr.Tab("Setup"):
272
  gr.Markdown("""<h1><center> Run Query </center></h1>""")