按定期计划自动执行 Colab 笔记本

问题描述 投票:0回答:2

是否有可靠的方法按照特定的时间表执行 Google Colab 笔记本?这个重复的时间表不应该要求我打开我的笔记本电脑来执行或以任何方式触发它。

我还可以免费试用 GCP,以防有帮助。

感谢您花时间阅读本文!

google-cloud-platform scheduled-tasks google-colaboratory
2个回答
5
投票

这是 Google Colab 团队正在考虑添加的功能,但您已经可以安排笔记本在 GCP 上运行:https://cloud.google.com/blog/products/application-development/how-to-schedule -a-recurring-python-script-on-gcp


0
投票

您可以使用 Selenium 来实现这一点。这可能有点过头了,但你可以免费使用它。

import time
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

COLAB_URL = 'https://colab.research.google.com/drive/CHANGE'
DOWNLOAD_FOLDER = 'CHANGEME/Downloads/' 
OUTPUT_FILE_NAME = 'copy.txt'

def run_colab(COLAB_URL=COLAB_URL, DOWNLOAD_FOLDER=DOWNLOAD_FOLDER, OUTPUT_FILE_NAME=OUTPUT_FILE_NAME):
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument(f"--download.default_directory={DOWNLOAD_FOLDER}")
    chrome_options.add_argument("--user-data-dir=CHANGEME/.config/google-chrome/Default") # Added line

    wd = webdriver.Chrome(options=chrome_options)
    wd.get(COLAB_URL)
    print(wd.title)
    print("Page loaded")

    wait = WebDriverWait(wd, 10)
    runtime_button = wait.until(EC.presence_of_element_located((By.ID, 'runtime-menu-button')))
    runtime_button.click()
    action_chains = ActionChains(wd)
    action_chains.key_down(Keys.CONTROL).send_keys(Keys.F9).key_up(Keys.CONTROL).perform()
    print("Running colab...")
    print("Waiting for the task to complete...")

    while not os.path.isfile(os.path.join(DOWNLOAD_FOLDER, OUTPUT_FILE_NAME)):
        time.sleep(1)

    print("Task completed")

    time.sleep(5)

    print("Closing Driver...")

    wd.quit()

if __name__ == "__main__":
    run_colab()

还有 Jupyter 笔记本代码

from google.colab import files
import time
with open("copy.txt", "w") as file:
    file.write("Hello")

files.download("copy.txt")

之后您可以为Python脚本创建cronjob。

0 8 * * * /usr/bin/python3 /path_to_your_script/run_colab.py

此示例将在每天上午 8:00 运行脚本。根据需要调整时间。

© www.soinside.com 2019 - 2024. All rights reserved.