i have 2 pages in a flask project. On page 1 i enter data, which I then send to another python script which I execute.
After the script is finished I want to redirect the page to page 2, where I want to use a csv file created by the python script on page 1.
I’ve started with the following code, without a redirect:
@app.route('/page1', methods=['GET', 'POST'])
def page1():
if request.method == 'POST':
var = request.form['var']
script_path = os.path.join(os.path.dirname(_file_), 'python', 'script.py')
result = subprocess.run([sys.executable, script_path, var], capture_output=True, text=True)
return f"<pre>{result.stdout}</pre><br><pre>{result.stderr}</pre>"
return render_template('page1.html')
This works fine, so the script is executed and after the csv is created the page reloads and my debug message is displayed.
Now I tried adding the redict by replacing the last return with:
return redirect(url_for('page2'))
The redirect itselfs, works, but I get redirected instantly, so the python script is not executed and because of that the csv file is not created…
What am I doing wrong?
Thank you for your help!
dont really now what else i should try
user is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.