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.