tomemojo commited on
Commit
17f885a
·
verified ·
1 Parent(s): 8c1d978

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -65
app.py CHANGED
@@ -12,125 +12,89 @@ stop_flag = False
12
  # Authenticate using the service account credentials
13
  def authenticate_google_sheets():
14
  print("Authenticating with Google Sheets...")
15
- # Define the scope and authenticate the client
16
  scope = [
17
- "https://www.googleapis.com/auth/spreadsheets", # Access Google Sheets
18
- "https://www.googleapis.com/auth/drive" # Access Google Drive
19
  ]
20
-
21
- # Load the service account credentials from the JSON file
22
  creds = Credentials.from_service_account_file('credentials.json', scopes=scope)
23
  client = gspread.authorize(creds)
24
  print("Authentication successful.")
25
  return client
26
 
27
- # Main function to upload the Excel data to Google Sheets, respecting delay between rows
28
  def upload_to_google_sheets(excel_file, spreadsheet_id, delay):
29
  global stop_flag
30
  stop_flag = False # Reset stop flag when process starts
 
31
 
32
- # Authenticate and open the Google Spreadsheet
33
  try:
34
  client = authenticate_google_sheets()
35
- sheet = client.open_by_key(spreadsheet_id).sheet1 # Open the first sheet
36
- print(f"Connected to Google Sheet: {spreadsheet_id}")
37
  except Exception as e:
38
- print(f"Error connecting to Google Sheets: {e}")
39
- return f"Error: {e}"
40
 
41
- # Read the Excel file
42
  try:
43
- print("Reading the Excel file...")
44
  df = pd.read_excel(excel_file.name)
45
- print("Excel file content:")
46
- print(df.head()) # Print the first few rows to verify content
47
  except Exception as e:
48
- print(f"Error reading the Excel file: {e}")
49
- return f"Error: {e}"
50
-
51
- # Replace NaN and infinite values with None to make them JSON-compliant
52
- df = df.replace([float('inf'), float('-inf')], None) # Handle infinity values
53
- df = df.where(pd.notnull(df), None) # Replace NaN values with None
54
 
55
- # Iterate through each row of the DataFrame
56
  for index, row in df.iterrows():
57
  if stop_flag:
58
- return "Process interrupted by the user."
59
-
60
  try:
61
- # Convert only the first two columns of the row to a list
62
- row_data = row.iloc[:2].tolist() # Get the first and second columns
63
- print(f"Uploading row {index}: {row_data}")
64
-
65
- # Append the first two columns to Google Sheets
66
  sheet.append_row(row_data)
67
- print(f"Row {index} uploaded successfully.")
68
-
69
  except Exception as e:
70
- # Print the problematic row and the error
71
- print(f"Error processing row {index}: {row_data}")
72
- print(f"Error message: {e}")
73
- continue # Skip the current row and continue with the next one
74
-
75
- # Wait for the specified delay before processing the next row
76
  time.sleep(delay)
 
 
 
77
 
78
- return "Excel rows have been successfully uploaded to the Google Sheet."
79
-
80
- # Function to stop the process
81
  def stop_process():
82
  global stop_flag
83
  stop_flag = True
84
- print("Stop process signal received.")
85
- return "Process will stop after the current row is uploaded."
86
 
87
- # Function to handle scheduling the upload process and respect both target time and delay per row
88
  def schedule_upload(excel_file, spreadsheet_id, delay, target_time):
89
- # Convert the input time to a Pacific Time (PST/PDT) datetime object
90
  pacific = pytz.timezone("America/Los_Angeles")
91
  current_time = datetime.now(pacific)
92
-
93
- # Parse the target time
94
  target_time = pacific.localize(datetime.strptime(target_time, '%Y-%m-%d %H:%M:%S'))
95
-
96
- # Calculate the time difference
97
  time_difference = (target_time - current_time).total_seconds()
98
 
99
  if time_difference > 0:
100
- # Wait until the target time
101
- print(f"Waiting {time_difference} seconds until the scheduled time...")
102
  time.sleep(time_difference)
103
 
104
- # Once the time is reached, call the upload_to_google_sheets function
105
  return upload_to_google_sheets(excel_file, spreadsheet_id, delay)
106
 
107
  # Gradio Interface
108
  def gradio_interface(excel_file, spreadsheet_id, delay, target_time):
109
  return schedule_upload(excel_file, spreadsheet_id, int(delay), target_time)
110
 
111
- # Use gr.Blocks to combine multiple components
112
  with gr.Blocks() as demo:
113
- excel_file_input = gr.File(label="Upload Excel File") # File upload input for Excel
114
- spreadsheet_id_input = gr.Textbox(label="Google Spreadsheet ID") # Textbox for Spreadsheet ID
115
- delay_input = gr.Number(label="Delay between rows (in seconds)", value=300) # Number input for delay (default: 300 seconds)
116
- target_time_input = gr.Textbox(label="Enter time to start (Pacific Time, format: YYYY-MM-DD HH:MM:SS)",
117
- placeholder="2024-09-25 14:30:00")
118
-
119
- # Create the interface for uploading the file
120
  result_output = gr.Textbox(label="Result")
121
  upload_button = gr.Button("Schedule Upload")
122
-
123
- # Add the stop button within the same context
124
  stop_button = gr.Button("Stop Process")
125
  stop_status_output = gr.Textbox(label="Status")
126
 
127
- # Bind the upload button to start the process
128
  upload_button.click(fn=gradio_interface,
129
  inputs=[excel_file_input, spreadsheet_id_input, delay_input, target_time_input],
130
  outputs=result_output)
131
-
132
- # Bind the stop button to stop the process
133
  stop_button.click(fn=stop_process, inputs=[], outputs=stop_status_output)
134
 
135
- # Launch the demo
136
  demo.launch()
 
12
  # Authenticate using the service account credentials
13
  def authenticate_google_sheets():
14
  print("Authenticating with Google Sheets...")
 
15
  scope = [
16
+ "https://www.googleapis.com/auth/spreadsheets",
17
+ "https://www.googleapis.com/auth/drive"
18
  ]
 
 
19
  creds = Credentials.from_service_account_file('credentials.json', scopes=scope)
20
  client = gspread.authorize(creds)
21
  print("Authentication successful.")
22
  return client
23
 
24
+ # Main function to upload Excel data to Google Sheets
25
  def upload_to_google_sheets(excel_file, spreadsheet_id, delay):
26
  global stop_flag
27
  stop_flag = False # Reset stop flag when process starts
28
+ logs = [] # To store logs for each row
29
 
 
30
  try:
31
  client = authenticate_google_sheets()
32
+ sheet = client.open_by_key(spreadsheet_id).sheet1
33
+ logs.append(f"Connected to Google Sheet: {spreadsheet_id}")
34
  except Exception as e:
35
+ logs.append(f"Error connecting to Google Sheets: {e}")
36
+ return "\n".join(logs)
37
 
 
38
  try:
 
39
  df = pd.read_excel(excel_file.name)
40
+ logs.append("Excel file read successfully.")
 
41
  except Exception as e:
42
+ logs.append(f"Error reading the Excel file: {e}")
43
+ return "\n".join(logs)
44
+
45
+ df = df.replace([float('inf'), float('-inf')], None).where(pd.notnull(df), None)
 
 
46
 
 
47
  for index, row in df.iterrows():
48
  if stop_flag:
49
+ logs.append("Process interrupted by the user.")
50
+ return "\n".join(logs)
51
  try:
52
+ row_data = row.tolist()
 
 
 
 
53
  sheet.append_row(row_data)
54
+ logs.append(f"Row {index} uploaded successfully: {row_data}")
 
55
  except Exception as e:
56
+ logs.append(f"Error processing row {index}: {row_data}, Error: {e}")
 
 
 
 
 
57
  time.sleep(delay)
58
+
59
+ logs.append("All rows processed.")
60
+ return "\n".join(logs)
61
 
62
+ # Stop the process
 
 
63
  def stop_process():
64
  global stop_flag
65
  stop_flag = True
66
+ return "Process will stop after the current row."
 
67
 
68
+ # Schedule upload with delay and target time
69
  def schedule_upload(excel_file, spreadsheet_id, delay, target_time):
 
70
  pacific = pytz.timezone("America/Los_Angeles")
71
  current_time = datetime.now(pacific)
 
 
72
  target_time = pacific.localize(datetime.strptime(target_time, '%Y-%m-%d %H:%M:%S'))
 
 
73
  time_difference = (target_time - current_time).total_seconds()
74
 
75
  if time_difference > 0:
 
 
76
  time.sleep(time_difference)
77
 
 
78
  return upload_to_google_sheets(excel_file, spreadsheet_id, delay)
79
 
80
  # Gradio Interface
81
  def gradio_interface(excel_file, spreadsheet_id, delay, target_time):
82
  return schedule_upload(excel_file, spreadsheet_id, int(delay), target_time)
83
 
84
+ # Gradio app
85
  with gr.Blocks() as demo:
86
+ excel_file_input = gr.File(label="Upload Excel File")
87
+ spreadsheet_id_input = gr.Textbox(label="Google Spreadsheet ID")
88
+ delay_input = gr.Number(label="Delay between rows (in seconds)", value=300)
89
+ target_time_input = gr.Textbox(label="Start time (Pacific Time, format: YYYY-MM-DD HH:MM:SS)", placeholder="2024-09-25 14:30:00")
 
 
 
90
  result_output = gr.Textbox(label="Result")
91
  upload_button = gr.Button("Schedule Upload")
 
 
92
  stop_button = gr.Button("Stop Process")
93
  stop_status_output = gr.Textbox(label="Status")
94
 
 
95
  upload_button.click(fn=gradio_interface,
96
  inputs=[excel_file_input, spreadsheet_id_input, delay_input, target_time_input],
97
  outputs=result_output)
 
 
98
  stop_button.click(fn=stop_process, inputs=[], outputs=stop_status_output)
99
 
 
100
  demo.launch()