| import gradio as gr |
| import pandas as pd |
| import time |
| import gspread |
| from google.oauth2.service_account import Credentials |
| from datetime import datetime, timedelta |
| import pytz |
|
|
| |
| stop_flag = False |
|
|
| |
| def authenticate_google_sheets(): |
| print("Authenticating with Google Sheets...") |
| scope = [ |
| "https://www.googleapis.com/auth/spreadsheets", |
| "https://www.googleapis.com/auth/drive" |
| ] |
| creds = Credentials.from_service_account_file('credentials.json', scopes=scope) |
| client = gspread.authorize(creds) |
| print("Authentication successful.") |
| return client |
|
|
| |
| def upload_to_google_sheets(excel_file, spreadsheet_id, delay): |
| global stop_flag |
| stop_flag = False |
| logs = [] |
| |
| try: |
| client = authenticate_google_sheets() |
| sheet = client.open_by_key(spreadsheet_id).sheet1 |
| logs.append(f"Connected to Google Sheet: {spreadsheet_id}") |
| except Exception as e: |
| logs.append(f"Error connecting to Google Sheets: {e}") |
| return "\n".join(logs) |
|
|
| try: |
| df = pd.read_excel(excel_file.name) |
| logs.append("Excel file read successfully.") |
| except Exception as e: |
| logs.append(f"Error reading the Excel file: {e}") |
| return "\n".join(logs) |
|
|
| |
| |
| for index, row in df.iterrows(): |
| if stop_flag: |
| logs.append("Process interrupted by the user.") |
| return "\n".join(logs) |
| try: |
| row_data = row.tolist() |
| sheet.append_row(row_data) |
| logs.append(f"Row {index} uploaded successfully: {row_data}") |
| except Exception as e: |
| logs.append(f"Error processing row {index}: {row_data}, Error: {e}") |
| time.sleep(delay) |
| |
| logs.append("All rows processed.") |
| return "\n".join(logs) |
|
|
| |
| def stop_process(): |
| global stop_flag |
| stop_flag = True |
| return "Process will stop after the current row." |
|
|
| |
| def schedule_upload(excel_file, spreadsheet_id, delay, target_time): |
| pacific = pytz.timezone("America/Los_Angeles") |
| current_time = datetime.now(pacific) |
| target_time = pacific.localize(datetime.strptime(target_time, '%Y-%m-%d %H:%M:%S')) |
| time_difference = (target_time - current_time).total_seconds() |
|
|
| if time_difference > 0: |
| time.sleep(time_difference) |
| |
| return upload_to_google_sheets(excel_file, spreadsheet_id, delay) |
|
|
| |
| def gradio_interface(excel_file, spreadsheet_id, delay, target_time): |
| return schedule_upload(excel_file, spreadsheet_id, int(delay), target_time) |
|
|
| |
| with gr.Blocks() as demo: |
| excel_file_input = gr.File(label="Upload Excel File") |
| spreadsheet_id_input = gr.Textbox(label="Google Spreadsheet ID") |
| delay_input = gr.Number(label="Delay between rows (in seconds)", value=300) |
| target_time_input = gr.Textbox(label="Start time (Pacific Time, format: YYYY-MM-DD HH:MM:SS)", placeholder="2024-09-25 14:30:00") |
| result_output = gr.Textbox(label="Result") |
| upload_button = gr.Button("Schedule Upload") |
| stop_button = gr.Button("Stop Process") |
| stop_status_output = gr.Textbox(label="Status") |
|
|
| upload_button.click(fn=gradio_interface, |
| inputs=[excel_file_input, spreadsheet_id_input, delay_input, target_time_input], |
| outputs=result_output) |
| stop_button.click(fn=stop_process, inputs=[], outputs=stop_status_output) |
|
|
| demo.launch() |
|
|