Deploying Python + Selenium applications without the need for dockerize it.
Lately, I have been dedicated to the development of a Python-based Application Programming Interface (API), requiring the integration of Selenium. When it was time to implement the solution on a server, I faced various limitations in efficiently running Selenium in that environment. I found that many of the proposed solutions to overcome this challenge involved dockerizing the application; however, given the peculiarities of my use case I chose to forget this option and instead sought a solution that would allow deployment in a native Python environment.
In this way, I discovered that by using render.com as a hosting platform, it is feasible to execute bash scripts on the server intended for the implementation of the application. This capability provides the opportunity to install Chrome directly on the mentioned server.
For this purpose, through this simple bash script, Chrome can be installed on the server if it is not already present.
#!/usr/bin/env bash # exit on error set -o errexit STORAGE_DIR=/opt/render/project/.render if [[ ! -d $STORAGE_DIR/chrome ]]; then echo "...Downloading Chrome" mkdir -p $STORAGE_DIR/chrome cd $STORAGE_DIR/chrome wget -P ./ https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb dpkg -x ./google-chrome-stable_current_amd64.deb $STORAGE_DIR/chrome rm ./google-chrome-stable_current_amd64.deb cd $HOME/project/src # Make sure we return to where we were else echo "...Using Chrome from cache" fi pip install -r requirements.txt # be sure to add Chromes location to the PATH as part of your Start Command # export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome"
Upon concluding the script, we include the installation of the necessary dependencies
In the final stage, it is crucial to navigate to the settings in the **“Build & Deploy”**section of render.com. Specifically, look for the option labeled “Build Command”, where the execution of the bash script that is specified using the command:
./render-build.sh
An additional relevant aspect involves adding the following command in the “Start Command” section:
export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome" && gunicorn app:app
In this instruction, the first part establishes the path where the Chrome executable is located, and we use gunicorn to run the application.
By implementing these steps, it is expected that our Python application can run Selenium seamlessly, having properly configured the render.com environment, installed Chrome, and defined the necessary dependencies in the build and start command. This set of measures provides a solid framework for the smooth operation of the application in the desired environment.
Github: https://github.com/FranciscoDiazPaccot73/selenium-python-example
Deploy: https://python-selenium-example.onrender.com/test