Yatheshr commited on
Commit
e20c1b7
·
verified ·
1 Parent(s): 84d0ebf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -6,19 +6,14 @@ import pymssql
6
  # Function to connect to SQL Server and run stored procedure
7
  def run_procedure(server, database, username, password, start_date, end_date):
8
  try:
9
- # Convert string dates to datetime.date objects
10
  start_date = datetime.strptime(start_date, "%Y-%m-%d").date()
11
  end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
12
 
13
- # Connect to SQL Server using pymssql (no ODBC required)
14
  conn = pymssql.connect(server=server, user=username, password=password, database=database)
15
  cursor = conn.cursor()
16
-
17
- # Run the stored procedure with parameters
18
  cursor.execute("EXEC dbo.Temp_Get_EQUITY_DocumentProcurement %s, %s", (start_date, end_date))
19
  conn.commit()
20
 
21
- # Now read the output table using pandas
22
  query = """
23
  SELECT * FROM [DocumentAcquisition].dbo.Temp_Document_Procurement_Timeline
24
  WHERE Document_Procurement_Name = 'EQUITY'
@@ -27,16 +22,16 @@ def run_procedure(server, database, username, password, start_date, end_date):
27
  conn.close()
28
 
29
  if df.empty:
30
- return "No data found for the given dates.", None
31
  else:
32
  csv_path = "document_procurement_report.csv"
33
  df.to_csv(csv_path, index=False)
34
- return df, csv_path
35
 
36
  except Exception as e:
37
- return f"Error: {str(e)}", None
38
 
39
- # Gradio UI using Textbox for dates (YYYY-MM-DD)
40
  iface = gr.Interface(
41
  fn=run_procedure,
42
  inputs=[
@@ -50,6 +45,7 @@ iface = gr.Interface(
50
  outputs=[
51
  gr.Dataframe(label="Procedure Output"),
52
  gr.File(label="Download CSV Report"),
 
53
  ],
54
  title="EQUITY Document Procurement Report Generator",
55
  description="Enter your SQL Server details and date range to run the stored procedure and download the report."
 
6
  # Function to connect to SQL Server and run stored procedure
7
  def run_procedure(server, database, username, password, start_date, end_date):
8
  try:
 
9
  start_date = datetime.strptime(start_date, "%Y-%m-%d").date()
10
  end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
11
 
 
12
  conn = pymssql.connect(server=server, user=username, password=password, database=database)
13
  cursor = conn.cursor()
 
 
14
  cursor.execute("EXEC dbo.Temp_Get_EQUITY_DocumentProcurement %s, %s", (start_date, end_date))
15
  conn.commit()
16
 
 
17
  query = """
18
  SELECT * FROM [DocumentAcquisition].dbo.Temp_Document_Procurement_Timeline
19
  WHERE Document_Procurement_Name = 'EQUITY'
 
22
  conn.close()
23
 
24
  if df.empty:
25
+ return None, None, "No data found for the given dates."
26
  else:
27
  csv_path = "document_procurement_report.csv"
28
  df.to_csv(csv_path, index=False)
29
+ return df, csv_path, "Success"
30
 
31
  except Exception as e:
32
+ return None, None, f"Error: {str(e)}"
33
 
34
+ # Updated Gradio UI
35
  iface = gr.Interface(
36
  fn=run_procedure,
37
  inputs=[
 
45
  outputs=[
46
  gr.Dataframe(label="Procedure Output"),
47
  gr.File(label="Download CSV Report"),
48
+ gr.Textbox(label="Status / Error Message")
49
  ],
50
  title="EQUITY Document Procurement Report Generator",
51
  description="Enter your SQL Server details and date range to run the stored procedure and download the report."