Not able to run a function after another function fully executes

Viewed 66

So, I am making this application that will take input from users, save it in a database and load the last database row in order to retrieve strings that will next be searched in google in order to get their screenshots. I have separately made both functions 1.) that'll create the flask app and take the user input & 2.) That will parse through the tuple of user input whose screenshots have to be taken. However, I am failing to run the functions one after the other.

app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///automationDB.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class Automate(db.Model):
  sno = db.Column(db.Integer, primary_key=True)
  title_name = db.Column(db.String(20), nullable=False)
  second_name = db.Column(db.String(20))
  third_name = db.Column(db.String(20))
  fourth_name = db.Column(db.String(20))

def index():
  if request.method=='POST':
        
        category = request.form['category']
        alternative_1 = request.form['searchtexts1']
        alternative_2 = request.form['searchtexts2']
        alternative_3 = request.form['searchtexts3']
        
        example = Automate(title_name=category, second_name = alternative_1, third_name = alternative_2, fourth_name = alternative_3 )
        db.session.add(example)
        db.session.commit()
        
  allTodo = Automate.query.all() 
  return render_template('index.html', allTodo=allTodo)

contents = sqlite3.connect('automationDB.db')
content = contents.cursor()

This is the function that is supposed to execute first, this function will basically fetch the input from the user and save it in the database.

Next is read_and_exec() function that is supposed to read all the value of tuple last_row and google it for images and save it in the directory.

def read_and_exec():
  
    last_row = content.execute('select * from automate').fetchall()[-1]

    os.mkdir("C:\\Users\\susha\\OneDrive\\Desktop\\Automation Project\\Content Images\\"+last_row[1]+" Images\\")
   
    os.environ['PATH'] += r"C:/Users/susha/OneDrive/Desktop/Automation Project"

    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver = webdriver.Chrome(options=options)

    driver.get('https://www.google.ca/imghp?hl=en&tab=ri&authuser=0&ogbl')
    
    my_element = driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
    for x in range(1,5):  
        my_element.send_keys(last_row[x])
        my_element.send_keys(Keys.ENTER)
       

        for i in range(1,10):
          sc_content = "C:\\Users\\susha\\OneDrive\\Desktop\\Automation Project\\Content Images\\"+last_row[1]+" Images\\"+last_row[x]+" ("+ str(i)+ ").png"
          driver.find_element(By.XPATH, '//*[@id="islrg"]/div[1]/div['+str(i)+']/a[1]/div[1]/img').screenshot(sc_content)

if __name__ == "__main__":
  app.run()

So the major problem I am having is where do I put this read_and_exec() function so that my program is able to run index() function at first and then read_and_exec() function? Call back function results in one task not executing fully before another task runs.

1 Answers

Flask executes code only in response to an HTTP request. In your index method, you get some data, you do some stuff with it, and then you return a response. After that, Flask doesn't do anything until there is a new request.

You will need to call your read_and_exec() method inside index() if you want it to be executed with the request. However, this will wait until read_and_exec has finished and only return your index.html template afterwards. If the read_and_exec() method takes a long time to complete and you want to return index.html before that, you will have to look at methods for asynchronous tasks, such as celery or Flask-APscheduler.

Related