Home > Back-end >  How to go to another page after a function executes flask python
How to go to another page after a function executes flask python

Time:10-28

I have a function called getTableData() which runs another function get_table() and based on that get_table() output final function is called which renders a template and also routes to a different page.

So the problem is its not routing to a different url (/tabdata) from get_final() function

Flask code:

@app.route('/api/getTableData', methods=['POST'])
def getTableData():
    value = request.json['value']
    value=value[:8]
    url="https://some.com" value
    df_time=get_table(url)
    return get_final(df_time)

def get_table(url):
    driver = webdriver.Chrome(options=options) 
    driver.get(url) 
    abv = pd.read_html(driver.find_element(By.ID,"frm_hist").get_attribute('outerHTML'))[0]
    df_time = pd.DataFrame(abv)  
    return df_time

@app.route("/tabdata")  
def get_final(df_time):
    return render_template("new.html",df_time = df_time)

Code Explanation:

I am using the value from value variable then concat 2 strings to make the url and then passing the url to another function named get_table() which goes to that url and webscrapes the table and converts it into python dataframe.

So using the returned python dataframe get_final() is called to render the template in a html file and also route to the /tabdata url. Everything is working well except the page is not routing to that url

CodePudding user response:

You have to return a redirect:

from flask import redirect

@app.route("/tabdata/<df_time>")
def get_final(df_time):
    return redirect("http://www.example.com", code=200)

CodePudding user response:

Use redirect and use it with url_for in case you decide to change your routes in the future. You also need to change your view function get_final

from flask import redirect, url_for


@app.route('/api/getTableData', methods=['POST'])
def getTableData():
    value = request.json['value']
    value = value[:8]
    url = "https://some.com" value
    df_time = get_table(url)
    return redirect(url_for('get_final', df_time=df_time))

def get_table(url):
    driver = webdriver.Chrome(options=options) 
    driver.get(url) 
    abv = pd.read_html(driver.find_element(By.ID,"frm_hist").get_attribute('outerHTML'))[0]
    df_time = pd.DataFrame(abv)  
    return df_time

@app.route("/tabdata/<df_time>") # notice change here!
def get_final(df_time):
    return render_template("new.html", df_time=df_time)

CodePudding user response:

In getTableData(), change

return get_final(df_time)

to

return redirect(url_for("get_final", df_time=df_time))

In get_final(), change

@app.route("/tabdata")  
def get_final(df_time):
    return render_template("new.html",df_time = df_time)

to

@app.route("/tabdata/<df_time>")
def get_final(df_time):
    return render_template("new.html", df_time=df_time)

Although your redirected URL will look something like this; "http://localhost/tabdata/16606505". If this is not preferred, you can always redirect for GET request with query parameters (which will look like this; "http://localhost/tabdata&data=16606505") or redirect for POST request which will not show the df_time parameter in browser history.

  • Related