|
1 |
| -# heroku-playwright-chromium |
2 |
| -A buildpack to install the Playwright Chromium executable ONLY. |
| 1 | +# heroku-playwright-python-browsers |
| 2 | +A buildpack to install Playwright browser executables for usage with Python on Heroku. |
3 | 3 |
|
| 4 | +**NOTE**: this buildpack and the following instructions was developed for usage with Chromium in mind. It is likely to work with Firefox and Webkit as well BUT hasn't been tested by the author. You are on your own when using Firefox and Webkit since I don't use those tools. Let me know if this buildpack works though and I'll update the docs. |
4 | 5 |
|
5 | 6 | # Usage
|
6 | 7 |
|
7 |
| -1. Add this buildpack to your app alongside the [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack). |
8 |
| - - The script CANNOT install the system requirements due to restrictions from Heroku. It's only able to install the Chromium exe. |
9 |
| - - Thus, we rely on [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack) to get the other packages. |
10 |
| -2. Run and deploy |
| 8 | +Follow the steps below. |
11 | 9 |
|
12 |
| -# Customization |
13 |
| -By default, this will install the necessary exe into a folder called `/browsers` under the build directory (the directory where the root script lives). |
| 10 | +## Tell Python to install `playwright` |
14 | 11 |
|
15 |
| -To change the this folder's name, override the `INSTALL_PATH` environmental variable. |
| 12 | +This buildpack relies on the CLI command `playwright install`. This requires [`playwright`](https://pypi.org/project/playwright/) to be installed. Do this by specifying a `requirements.txt` or any other methods accepted by the Heroku Python buildpacks. |
16 | 13 |
|
17 |
| -# Notes |
18 |
| -This was developed for usage with `playwright-python` in mind so support for other languages is unknown. |
| 14 | +## Buildpack ordering |
| 15 | + |
| 16 | +Order matters for this buildpack to work. Here's the run order that you should should have: |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +1. The official [Python buildpack](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-python). |
| 21 | + - Runs first so that Playwright is available as a CLI tool after the package installation is done |
| 22 | +1. This build pack. [Here's how to add this buildpack to your app](https://devcenter.heroku.com/articles/buildpacks#using-a-third-party-buildpack) by using its Git URL. |
| 23 | +2. The [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack) |
| 24 | + - **Why?** This buildpack CANNOT install system requirements (i.e. `playwright install --with-deps`) due to restrictions from Heroku. It's only able to install the browser executables. Thus, we rely on [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack) to get the needed system packages. |
| 25 | + |
| 26 | + |
| 27 | +## Customize the following config variables: |
| 28 | +1. `PLAYWRIGHT_BUILDPACK_BROWSERS` accepts a comma-separated list of the browser names `chromium,firefox,webkit`. By default, it's installing the executables for all these browsers. |
| 29 | + - E.g. To only install Chromium dependencies, set the variable to `chromium`. This will reduce the slug size in the end too. |
| 30 | + - **NOTE**: this is what the author uses, usage with the other 2 browsers are unknown. Feel free to test them out and let me know if they work and I'll update the docs. |
| 31 | + - This config variable is intended to be shared with [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack). |
| 32 | +2. `BUILDPACK_BROWSERS_INSTALL_PATH` is the path where the browsers will be installed. Its default value is `browsers` and it's a relative path starting from the [`BUILD_DIR`](https://devcenter.heroku.com/articles/buildpack-api#bin-compile) (the starting directory of the compiled slug to be used by Heroku). It's necessary to have the installed browsers within the `BUILD_DIR` to have the file included by the time Heroku starts the dyno. |
| 33 | + - E.g. To install in the directory `utils/browsers`, set the variable to `utils/browsers`. |
| 34 | + - This config variable is exported by the buildpack for your use in the next step |
| 35 | + |
| 36 | +## Modify your browser init script |
| 37 | + |
| 38 | +Since the browsers are installed in a non-default location, we have to specify the executable path when we create the browser instances. |
19 | 39 |
|
| 40 | +Here's how you should create your browser: |
20 | 41 |
|
| 42 | +``` |
| 43 | +with sync_playwright() as p: |
| 44 | + try: |
| 45 | + browser = p.chromium.launch(executable_path=env("CHROME_EXECUTABLE_PATH"), ...other arguments) |
| 46 | + # for chromium, you don't have to pass in flags like `no-sandbox` or `headless` because they are enabled by default. |
| 47 | + # Flags like `--disable-dev-shm-usage` and `--remote-debugging-port=9222` can be added if needed but I haven't needed that |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +# Questions |
| 52 | + |
| 53 | +**1. Why doesn't this buildpack install the requirements as well?** |
| 54 | + |
| 55 | +The command to install the system packages along with the browser is `playwright install --with-deps`. For some reason, this action is blocked by Heroku since it uses sudo underneath (which is not allowed as of 2024). Thus, only the browser installation can go through but that alone is not enough. We still need the system packages which is provided by `heroku-playwright-buildpack`. |
| 56 | + |
| 57 | +**2. The script doesn't work for me. What do I do?** |
| 58 | + |
| 59 | +Here are a few things to consider: |
| 60 | +- This buildpack is meant for Python Playwright and was developed with Chromium in mind. Any other browser/Playwright distro is not tested by me so I offer no guarantees there |
| 61 | +- Verify that the installation is successful. Look at the build logs, you should see progress bar for the executable installations |
| 62 | + - You can also `heroku run bash` into the dyno and search for the installation folder to verify that the exe are downloaded. |
| 63 | + |
| 64 | +# Notes |
| 65 | +This was developed for usage with `playwright` for Python |
21 | 66 |
|
0 commit comments