I've got a script powered by Selenium which contains a class that returns data based on what Selenium returns.
I'd like to incorporate that part of my codebase with a Flask server.
The Selenium-powered class:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Api():
def __init__(self, options=None):
self.base_url = 'https://baseurl.com'
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
self.response = None
def get(self, username):
if self.response is None:
self.response = self._fetch(username)
return self
def _fetch(self, username):
...
How I would like to incorporate that into Flask:
from flask import Flask
from api import Api
app = Flask(__name__)
api = Api()
@app.route('/<username>')
def user(username):
return api.get(username)
if __name__ == '__main__':
app.run(debug=True)
When trying to run the above code as is, I get the following:
====== WebDriver manager ======
Current google-chrome version is 100.0.4896
Get LATEST chromedriver version for 100.0.4896 google-chrome
Driver [c:\SoftwareDevelopment\Lytics\.wdm\drivers\chromedriver\win32\100.0.4896.60\chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:2263/devtools/browser/2479727f-4841-4837-839d-6118c25fdac2
* Serving Flask app 'app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
* Restarting with stat
====== WebDriver manager ======
Current google-chrome version is 100.0.4896
Get LATEST chromedriver version for 100.0.4896 google-chrome
Driver [c:\SoftwareDevelopment\Lytics\.wdm\drivers\chromedriver\win32\100.0.4896.60\chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:2279/devtools/browser/0132d55c-2418-4e8b-af62-49bd4e98dd61
The Flask server seems to have been killed at this point.
Side note
One naive solution to all this would be to spin up the webdriver for every new HTTP request directed towards Flask, which is not feasible for what I'm trying to do. I'd like to have the Selenium instance run in the background and be accessible from within Flask. Would this require some sort of inter-process communication?