首页 > 解决方案 > 从烧瓶函数获取返回值到另一个 python 文件(从非烧瓶 python 文件调用烧瓶函数)

问题描述

我必须从 python 的网页中获取输入。我为此使用烧瓶。我想返回它得到的输入。我想将该flask函数调用到另一个不使用flask的文件中,只是python文件。示例:以下代码在 inputpage.py 中

@app.route('/topic', methods=['GET', 'POST'])
def topic_input():
    res = None
    if request.method == 'POST':
        if not request.form['t1'] and not request.form['t2'] :
            res = 'please fil the form'
            # print(result)
        else:
            try:
                term = request.form['t1']
                t2 = int(request.form['t2'])
                res='Good'

            except BaseException as error:
                res = 'error: {}'.format(error)
                # print(result)
    #return render_template('topic.html', articles=res_dic, result=res)
    return(term,t2)

在另一个文件中说 test.py 我尝试调用上述函数,

from inputpage import topic 
print(topic_input())

但我收到以下错误:raise RuntimeError(_request_ctx_err_msg) RuntimeError: Working outside of request context。

这通常意味着您尝试使用需要活动 HTTP 请求的功能。有关如何避免此问题的信息,请参阅有关测试的文档。

我想知道我怎么能做这样的事情。感谢您的帮助:)

标签: pythonflask

解决方案


推荐阅读