猿问

如何从函数外部使用 flask 变量以便以后在 javascript 中使用?

运行 routes.py


@app.route('/articles', methods=['GET', 'POST'])

def articles():

    upload()

    return render_template('articles.html')

而这个保存图像并处理其信息的功能


def upload():

    if request.method == 'POST':

        # Save the file to static/uploads

    

        label = "some string from process above"

        probability = "another string"


        return None


    return None

如何在渲染模板时使用变量标签和概率?有些人使用接近于:


return render_template('articles.html', label=label, probability=probability)

这样做是为了使用 js 引用变量。但是,如果它是在 upload() 内部计算的,我该如何引用该变量?需要全局变量吗?


ibeautiful
浏览 148回答 1
1回答

慕慕森

您可以从函数返回这些变量。您必须解压缩从函数发送的变量upload。首先,您必须将它们从 退回upload,然后将其拆包发送到render_template。@app.route('/articles', methods=['GET', 'POST'])def articles():    label, probability = upload()    return render_template('articles.html', label=label, probability=probability)def upload():    if request.method == 'POST':        # Save the file to static/uploads            label = "some string from process above"        probability = "another string"        return (label, probability)    return (None, None)
随时随地看视频慕课网APP

相关分类

Python
我要回答