Skip to content

Feature: Add playwright support in build command - #76

Closed
shubhamofbce wants to merge 5 commits into
lavague-ai:mainfrom
shubhamofbce:add-playwright-support
Closed

shubhamofbce wants to merge 5 commits into
lavague-ai:mainfrom
shubhamofbce:add-playwright-support

Conversation

@shubhamofbce

@shubhamofbce shubhamofbce commented Mar 28, 2024 •

Copy link
Copy Markdown
Contributor

Partially closes: #1

This PR adds a new command lavague-build-playwright to build playwright code similar to lavague-build.

We are using playwright async api, and i have created a new function for it without affecting any existing methods.

Command to run build playwright code
!lavague-build-playwright --file_path hf.txt --config_path openai.py

Other things that I tried which didn't worked:

  • I also tried using playwright sync API in build such that the code looks similar to selenium and we don't need async await but it was throwing error: error: It looks like you are using Playwright Sync API inside the asyncio loop. I searched and found that sync api is not allowed in jupyter notebooks and collab as they are already in asyncio loop.

  • I also tried implementing the launch method using both sync and async playwright but no luck as It uses gradio and gradio was switching threads and plawright is not thread aware. We can create a separate issue for this and look into it later.

@shubhamofbce
shubhamofbce marked this pull request as draft March 28, 2024 18:59
@shubhamofbce
shubhamofbce marked this pull request as ready for review March 28, 2024 19:15
@shubhamofbce

Copy link
Copy Markdown
Contributor Author

@lyie28 @mbrunel Please take a look and let me know if you have any questions.

…ee proper error messages and playwright is not a required library
@shubhamofbce

Copy link
Copy Markdown
Contributor Author

This is a sample code generated using lavague-build-playwright @mbrunel

import asyncio
async def wrapper_func():
    try:
        from playwright.async_api import async_playwright
    except (ImportError, ModuleNotFoundError) as error:
        raise ImportError("Please install playwright using `pip install pytest-playwright` and then `playwright install` to install the necessary browser drivers") from error
    p = await async_playwright().__aenter__()
    browser = await p.chromium.launch()
    page = await browser.new_page()
    await page.goto('https://huggingface.co/')
    ########################################
    # Query: Click on the Docs
    # Code:
        # Based on the provided HTML snippet, we need to identify the link that contains the text "Docs"
    # We can use the text selector to find the link with the text "Docs"
    docs_link = page.locator('text="Docs"')
    # Then we can click on the link
    await docs_link.click()
    ########################################
    # Query: Click on Optimum
    # Code:
        # Based on the provided HTML snippet, the text "Optimum" is within an anchor tag.
    # We can use the text selector to identify and click on the element.
    optimum_link = page.locator('text=Optimum')
    await optimum_link.click()
    # Based on the provided HTML snippet, we can see that the link for NVIDIA is within an anchor tag.
    # We can use the text content of the anchor tag to identify and click on the link.
    # Find the anchor tag containing the text "NVIDIA"
    nvidia_link = page.locator('a').with_text('NVIDIA')
    # Click on the NVIDIA link
    await nvidia_link.click()
asyncio.run(wrapper_func())
    

@shubhamofbce
shubhamofbce marked this pull request as draft March 29, 2024 07:05
@shubhamofbce

Copy link
Copy Markdown
Contributor Author

@mbrunel Moved this to draft as i need to do some code cleanup and comments to improve code quality. You can review the core changes they will remain same.

@dhuynh95

Copy link
Copy Markdown
Collaborator

Pretty cool @shubhamofbce!
I think we can assume people will mostly be using LaVague through a CLI, the notebooks were more a low cost solution we chose to make it easy for people to try while we were cleaning code.

I think focusing on the build for now might make the most sense.

We will soon switch to a VS Code Extension. I don't know if it will create the same issues with async/sync though

@dhuynh95

Copy link
Copy Markdown
Collaborator

Also, tried using that for the GUI, seems to be working:

from playwright.async_api import async_playwright

playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless = False)
page = await browser.new_page()

await page.goto("http://whatsmyuseragent.org/")

Will have a look too this weekend

@shubhamofbce
shubhamofbce marked this pull request as ready for review March 30, 2024 13:29
@shubhamofbce

Copy link
Copy Markdown
Contributor Author

Added comments in the utils function and did some code cleanup, Now it is ready for review.

@dhuynh95

Copy link
Copy Markdown
Collaborator

It works! Pretty cool, thanks for a great job!

I have a few things to highlight that might be relevant before we merge, some you can help with, some we will have to look at as well (@lyie28 / @mbrunel ):

  • We should have a way for people to install LaVague with playwright install, some kind of pip install lavague[playwright]
  • Instead of appending each block of action and rerunning from scratch, I think it should be possible to have LaVague generate snippets and run them progressively without having to run from scratch.

Here is a gist to give some idea.

I think each block of action can be wrapped in an async function and called progressively.

What do you think @shubhamofbce ?

@shubhamofbce

Copy link
Copy Markdown
Contributor Author

@shubhamofbce Both of these are great ideas, let me look into them.

@shubhamofbce

Copy link
Copy Markdown
Contributor Author

@dhuynh95
I have added playwright as optional dependencies so users can install it using.
pip install lavague[playwright]
I also tried executing playwright install automatically post pip install but that is not possible yet according to this.
So we will have to mention in the docs to do playwright install manually if you are installing lavague for playwright. Even playwright mentions the same in their docs.
See this c17a1ef

@dhuynh95

Copy link
Copy Markdown
Collaborator

@shubhamofbce super! Will have a look

Also regarding the second point I don't think it's actually possible and your solution might be the only one working with Playwright

@shubhamofbce

Copy link
Copy Markdown
Contributor Author

You are right @dhuynh95 It is actually difficult to achieve that. we can create an issue for it and pick that up later as an optimisation.

@dhuynh95

Copy link
Copy Markdown
Collaborator

@shubhamofbce I agree let's see later. In any way, we got big stuff coming that will solve it:

  • VS Code extension that provides more flexibility that will replace Gradio
  • Beginning of support of Playwright too!

I did some experiment on my side, nothing ready yet but it will look like that:
gform-radio

Regarding the merge, @lyie28 and @mbrunel are off until Tuesday, so we will merge around that.

@mbrunel is working on a cleaning of the codebase, so we might wait until we converge on a more modular architecture before merging your PR.

I am thinking the build can be decomposed in the following steps, that can be modular and adapted:

  • Load driver/browser (Selenium, Playwright, etc.)
  • Load action engine (local, remote, etc.)
  • Load instructions (behavior file, txt, whatever, etc.)
  • Exec instructions (generate code specific to the targeted execution, e.g. Selenium)
  • Save output (Python script, Pytest, FastAPI, etc.)

What do you think? @shubhamofbce :)

@shubhamofbce

Copy link
Copy Markdown
Contributor Author

@dhuynh95, the VS code demo/experiment is looking fantastic! 😄 Just a friendly note: there's no rush to merge my code. Feel free to take your time, and let's sync up when we all agree it's the right moment.

Also, breaking it down into steps is a brilliant idea. It not only enhances readability and maintainability but also offers users greater flexibility in various use cases. Let's do this.

@dhuynh95

Copy link
Copy Markdown
Collaborator

Sure! Yeah I am so excited about this project that I put myself big objectives haha

Because of the time you put, I want to make sure we merge this so that we make sure we show with acts and not just words how we appreciate your efforts in helping build this project! :)

@lyie28
lyie28 requested a review from mbrunel April 5, 2024 14:03
@mbrunel mbrunel mentioned this pull request Apr 17, 2024
@mbrunel

mbrunel commented Apr 17, 2024 •

Copy link
Copy Markdown
Contributor

I opened #131 to reflect your changes on an up to date branch, while keeping this as a reference. I would like you to take a look and tell me your toughts.

@mbrunel

mbrunel commented Apr 18, 2024

Copy link
Copy Markdown
Contributor

I close this as #131 was merged.

@mbrunel mbrunel closed this Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Playwright support

3 participants