Installing Playwright on Streamlit Cloud

I’ve had no problem using Playwright on Streamlit locally. But when I try to deploy the app using Github (and installed all dependencies via requirements.txt), it doesn’t seem to work because Playwright requires the additional “playwright install” command via the terminal to install browser binaries.

I get the following error:

2022-09-22 06:10:47.874 Uncaught app exception

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 556, in _run_script

    exec(code, module.__dict__)

  File "/app/feedings/main_pw.py", line 14, in <module>

    browser = p.chromium.launch()

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/sync_api/_generated.py", line 11678, in launch

    self._sync(

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/_impl/_sync_base.py", line 104, in _sync

    return task.result()

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/_impl/_browser_type.py", line 93, in launch

    Browser, from_channel(await self._channel.send("launch", params))

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 43, in send

    return await self._connection.wrap_api_call(

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 387, in wrap_api_call

    return await cb()

  File "/home/appuser/venv/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 78, in inner_send

    result = next(iter(done)).result()

playwright._impl._api_types.Error: Executable doesn't exist at /home/appuser/.cache/ms-playwright/chromium-1024/chrome-linux/chrome

╔════════════════════════════════════════════════════════════╗

║ Looks like Playwright was just installed or updated.       ║

║ Please run the following command to download new browsers: ║

║                                                            ║

║     playwright install                                     ║

║                                                            ║

║ <3 Playwright Team                                         ║

╚════════════════════════════════════════════════════════════╝

Is there a simple solution for this?

I got Playwright to work but not sure if this is the best solution. To overcome the above issue, I included

import os
os.system("playwright install")

at the top of my code, and added a packages.txt file to my Github containing:

libnss3
libnspr4
libatk1.0-0
libatk-bridge2.0-0
libcups2
libdrm2
libxkbcommon0
libxcomposite1
libxdamage1
libxfixes3
libxrandr2
libgbm1
libpango-1.0-0
libcairo2
libasound2
libatspi2.0-0
libwayland-client0
2 Likes

How to call your playwright function?

I tried this, but not work:

if platform.system() == 'Windows':
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()
bar=loop.run_until_complete(foo())

Could you please share a example?
Thank you!

I run playwright in streamlit cloud, there are errors like this:

playwright._impl._api_types.Error: 
╔══════════════════════════════════════════════════════╗
║ Host system is missing dependencies to run browsers. ║
║ Please install them with the following command:      ║
║                                                      ║
║     sudo playwright install-deps                     ║
║                                                      ║
║ Alternatively, use apt:                              ║
║     sudo apt-get install libnss3\                    ║
║         libnspr4\                                    ║
║         libatk1.0-0\                                 ║
║         libatk-bridge2.0-0\                          ║
║         libcups2\                                    ║
║         libdrm2\                                     ║
║         libatspi2.0-0\                               ║
║         libxcomposite1\                              ║
║         libxdamage1\                                 ║
║         libxfixes3\                                  ║
║         libxrandr2\                                  ║
║         libgbm1\                                     ║
║         libxkbcommon0\                               ║
║         libpango-1.0-0\                              ║
║         libcairo2\                                   ║
║         libasound2                                   ║
║                                                      ║
║ <3 Playwright Team                                   

How could I install the deps using sudo ? is there any solution? Thanks.