-1

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 %}
            &emsp;&emsp;&emsp;&emsp;<input type="submit">
        </form>
    </body>
</html>
PYB
  • 503
  • 6
  • 20

2 Answers2

1

if you're not retrieving the list from something more permanent (such as a database) you might want to make a third function where you declare/define your list and then call that function..

def my_list():
    make_a_list = [1, 2, 3] # make/get list however you want
    return make_a_list

@app.route('/')
def home():
    list1 = my_list()
    print('list 1 = ', list1) # do stuff with list
    return render_template('template1.html', list1=list1)

@app.route('/test')
def test():
    list2 = my_list()
    print('list 2 = ', list2) # do stuff with list
    return render_template('template2.html', list2=list2)

i'm not sure how or where you're getting your list, but this will avoid defining a global.

None
  • 572
  • 1
  • 5
  • 12
  • A TypeError is thrown, mentioning that test() is missing 1 required positional argument: '_list' I am at a complete loss here, please expand on this. – PYB Jul 04 '19 at 03:41
  • i've edited the original answer for what is hopefully a more useful solution. – None Jul 04 '19 at 07:18
  • Wow, so simple and effective. – PYB Jul 06 '19 at 21:21
  • Well, looks like I spoke too fast. I am getting my list from the fields of a form. I will my original question as I still can't achieve what I need. – PYB Jul 07 '19 at 00:24
0

You can use session. So the data will be saved during user session.

from flask import session

@app.route('/')
def home():
    session['list1'] = [1,2,3]
    return render_template('template1.html', list1=session['list1'])

@app.route('/test')
def test():
    if session.get('list1'):
        list2 = session['list1']
    else:
        list2 = []
    return render_template('template2.html', list2=list2)
ASSILI Taher
  • 1,210
  • 2
  • 9
  • 11
  • I cannot use `session` as the user will input and change the values during a session. – PYB Jul 04 '19 at 03:47