Reputation: 81
i have some project working with Flask and Jinja2, and the web ui just table like this image
that all Routes is triggering program backend for scraping some website, this web is working fine. but i wonder how make it more flexible.
i just thought of making auto-run that all routes with one button, and have their own status. that column will have Status and their status like (running, done, stop, not running etc.) but i cannot imagine that logic.
i already create for auto-run and work fine, and my question just how to know their status is running, done, stop or not running in the background.
any idea really appreciate. this is my own project so i'm so excite to make this work
Upvotes: 0
Views: 1315
Reputation: 1086
An easy and efficient approach will be to use AJAX to concurrently check a log file for the status of process and update the DOM element...
I would suggest you to have a seperate log file where the backend flask processes update the current status they are working on. something like initially everything is on status "SLEEP", and once the process is triggered it changes its corresponding log status to "RUNNING"... Once the process ends, it changes the log status to "DONE".
Use AJAX from the front end to parse the Log File every N seconds and update the DOM status element based on the status parsed from the log file by AJAX
PS. You can also add animation effects like a spinner on the DOM element on running process through AJAX
Upvotes: 1
Reputation: 762
The simplest way of doing this is by running a log of each stage of the scraping process. So for example:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.google.com/')
print("Loaded Google.com")
some_task = driver.find_element(By.XPATH, '//button[text()="Some text"]')
print("Got some task")
(Locating elements as per: https://selenium-python.readthedocs.io/locating-elements.html)
However, for real-time processing of task status and for more efficiency, you can use Celery.
Celery works well for web scraping tasks as it actually allows you to asynchronously offload work from your Python app to workers and task queues.
You can then retrieve proper status reports from each worker. See: https://docs.celeryq.dev/en/stable/reference/celery.states.html
Upvotes: 2