如何在Flask中为其他app.routes引用变量

时间:2019-11-17 18:30:35

标签: python matplotlib flask

我是一名学生,学习如何使用flask,并计划将其与Matplotlib(图形制作器库)集成。我从用户那里得到了输入,并将它们存储为变量。但是,在以后的代码中不能调用变量。

这是我的烧瓶代码:

 @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 )

我想在另一个@ app.route中调用诸如titlex_axisy_axis之类的变量来制作图形。

我不确定自己是否足够具体,但是可以提供任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

有几种方法可以在路由器之间存储和访问数据。实现这一目标的一种方法是使用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...