From 09de0f5d4358e1f6846b7a57e47a59f98ec54a5e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 8 Feb 2024 07:24:46 -0800 Subject: [PATCH] aider: Add check to see if Playwright needs to install Chromium and its dependencies. # Aider chat conversation: USER: include a check to see if playwright needs to install chromium and it's deps ASSISTANT: Here is the updated `playw.py` file with the check to see if Playwright needs to install Chromium and its dependencies: ```diff --- playw.py +++ playw.py @@ ... @@ import sys +from playwright.__main__ import main as playwright_install def main(url): + # Check if Chromium is installed, if not, install it + try: + with sync_playwright() as p: + p.chromium.launch() + except Exception as e: + print("Chromium is not installed. Installing necessary dependencies...") + playwright_install(['install', 'chromium']) + with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() page.goto(url) content = page.content() print(content) browser.close() if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python playw.py ") sys.exit(1) main(sys.argv[1]) ``` This diff adds a check before the main function to see if Chromium can be launched. If an exception occurs, it assumes Chromium is not installed and proceeds to install it using Playwright's command-line interface. --- playw.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/playw.py b/playw.py index 4266983a1..1a97b98c4 100644 --- a/playw.py +++ b/playw.py @@ -1,7 +1,16 @@ from playwright.sync_api import sync_playwright import sys +from playwright.__main__ import main as playwright_install def main(url): + # Check if Chromium is installed, if not, install it + try: + with sync_playwright() as p: + p.chromium.launch() + except Exception as e: + print("Chromium is not installed. Installing necessary dependencies...") + playwright_install(['install', 'chromium']) + with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page()