I use Flask to route URLs to functions, which return templates. In one of the function I create a list that I would like to use in another function.
I have tried to declare variables in the global scope, and declaring them as global in the function when I modify it, but since I cannot return the list specifically, the changes are not applied in the global scope. I have read this answer but since the function is returning templates instead of modified list, I don't know how to proceed. I know global variables are not ideal, but I am hitting a wall.
Some simplified code:
list1 = []
@app.route('/')
def home():
global list1
list1 = [1,2,3]
return render_template('template1.html', list1=list1)
@app.route('/test')
def test():
list2 = copy(list1)
return render_template('template2.html', list2=list2)
I would want list2 to be [1,2,3]. It currently is [], referencing the unchanged list defined in the global scope before the home() function.
--- EDIT ---
For extra context, I will provide some updated code and more info.
I am trying to make test1.html output [1,2,3].
I tried using the g object, didn't work.
test.py
@app.route('/')
def home():
return render_template('test_form.html')
@app.route('/test_results', methods=['POST', 'GET'])
def form_results():
g.allPlayers = []
for i in range(1,4):
g.allPlayers.append(request.form['player'+str(i)])
return render_template('test_results.html',allPlayers=g.allPlayers)
@app.route('/test1')
def test1():
newList = g.allPlayers
return render_template('test1', newList=newList)
test1.html
<html>
<header>
<title>Test</title>
</script>
</header>
<body>
{{newList}}
<br>
<a href="/second">Second<a>
</body>
</html>
test_form.html
<html>
<header>
<title>Template 1</title>
</script>
</header>
<body>
<form method="POST" action="test_results">
<p></p>
{% for i in range(1,4) %}
<input type="text" name={{"player" ~ i}}><br>
{% endfor %}
    <input type="submit">
</form>
</body>
</html>