-1

Im a student learning how to use flask and planning to integrate it with Matplotlib ( The graph maker library). I have got inputs from the user and stored them as variables. However, the variables are not callable upon in later code.

This is my code for flask:

 @app.route('/', methods = ['POST','GET'])
def upload_file():
    global title, x_axis, y_axis
    if request.method == 'POST':
        f = request.files['file']
        if True == allowed_file(f.filename):
            f.save(f.filename)
            print(f)

    if request.method == 'POST':
        if request.method == 'POST':
            reqform = request.form
            #
            title = reqform['title']
            x_axis = reqform['x_axis']
            y_axis = reqform['y_axis']
            #
            print(title, x_axis, y_axis)

    return render_template('uploaded.html') #, title = title, form = reqform, f = f.filename )

I want to call on the variables such as title,x_axis, y_axis in another @app.route to make a graph.

Im not sure if I'm being specific enough, however, any help would be appreciated. Thank you

1 Answers1

0

There are several ways to store and access data between your routers. One way you could achieve this is by using Flask Sessions:

 @app.route('/', methods = ['POST','GET'])
def upload_file():
    global title, x_axis, y_axis
    if request.method == 'POST':
        f = request.files['file']
        if True == allowed_file(f.filename):
            f.save(f.filename)
            print(f)

    if request.method == 'POST':
        if request.method == 'POST':
            reqform = request.form
            # assign in session variable

            session['title'] = reqform['title']
            session['x_axis'] = reqform['x_axis']
            session['y_axis'] = reqform['y_axis']

    return render_template('uploaded.html')

@app.route('/another_endpoint', methods = ['POST','GET'])
    def another_endpoint():
       title = session.get('title', None)
       x_axis = session.get('x_axis', None)
       y_axis = session.get('y_axis', None)
       # do something...
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81