tomemojo commited on
Commit
08268ab
·
verified ·
1 Parent(s): 1611d1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -27
app.py CHANGED
@@ -3,12 +3,15 @@ import pandas as pd
3
  import time
4
  import gspread
5
  from google.oauth2.service_account import Credentials
 
 
6
 
7
  # Global variable to control process interruption
8
  stop_flag = False
9
 
10
  # Authenticate using the service account credentials
11
  def authenticate_google_sheets():
 
12
  # Define the scope and authenticate the client
13
  scope = [
14
  "https://www.googleapis.com/auth/spreadsheets", # Access Google Sheets
@@ -18,59 +21,116 @@ def authenticate_google_sheets():
18
  # Load the service account credentials from the JSON file
19
  creds = Credentials.from_service_account_file('credentials.json', scopes=scope)
20
  client = gspread.authorize(creds)
 
21
  return client
22
 
23
- # Function to upload each row to Google Spreadsheet with a delay
24
- def upload_to_google_sheets(csv_file, spreadsheet_id, delay):
25
  global stop_flag
26
  stop_flag = False # Reset stop flag when process starts
27
 
28
  # Authenticate and open the Google Spreadsheet
29
- client = authenticate_google_sheets()
30
- sheet = client.open_by_key(spreadsheet_id).sheet1 # Open the first sheet
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Read the CSV file
33
- df = pd.read_csv(csv_file.name)
 
34
 
35
  # Iterate through each row of the DataFrame
36
  for index, row in df.iterrows():
37
  if stop_flag:
38
  return "Process interrupted by the user."
39
 
40
- # Convert row to list and append it to Google Sheets
41
- sheet.append_row(row.values.tolist())
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Wait for the specified delay before processing the next row
44
  time.sleep(delay)
45
 
46
- return "CSV rows have been successfully uploaded to the Google Sheet."
47
 
48
  # Function to stop the process
49
  def stop_process():
50
  global stop_flag
51
  stop_flag = True
 
52
  return "Process will stop after the current row is uploaded."
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Gradio Interface
55
- def gradio_interface(csv_file, spreadsheet_id, delay):
56
- return upload_to_google_sheets(csv_file, spreadsheet_id, int(delay))
57
 
58
- # Create Gradio app with file upload, spreadsheet ID input, delay input, and stop button
59
- csv_file_input = gr.File(label="Upload CSV File") # File upload input
60
- spreadsheet_id_input = gr.Textbox(label="Google Spreadsheet ID") # Textbox for Spreadsheet ID
61
- delay_input = gr.Number(label="Delay between rows (in seconds)", value=300) # Number input for delay (default: 300 seconds)
 
 
 
62
 
63
- # Create the interface
64
- interface = gr.Interface(
65
- fn=gradio_interface,
66
- inputs=[csv_file_input, spreadsheet_id_input, delay_input],
67
- outputs=gr.Textbox(label="Result"),
68
- live=False # Disable continuous updates
69
- )
70
 
71
- # Add a stop button to interrupt the process
72
- stop_interface = gr.Interface(fn=stop_process, inputs=None, outputs=gr.Textbox(label="Status"))
 
 
 
 
 
73
 
74
- # Launch both interfaces
75
- interface.launch()
76
- stop_interface.launch()
 
3
  import time
4
  import gspread
5
  from google.oauth2.service_account import Credentials
6
+ from datetime import datetime, timedelta
7
+ import pytz
8
 
9
  # Global variable to control process interruption
10
  stop_flag = False
11
 
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
 
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()