Сообщество переполнения стека приветствий, в настоящее время я работаю над приложением flask и пытаюсь получить файл из вспомогательной функции с помощью метода send_file в flask.
У меня есть маршрут, который выглядит так:
@app.route("/process",methods=['GET','POST'])
def do_something():
process = threading.Thread(target=function_name,args=[arg1,arg2])
process.start()
return render_template("template.html")
Функция function_name
(которая находится в другом файле) может возвращать файл, подобный этому
def function_name():
filename = 'ohhey.pdf'
return send_file(filename,as_attachment=True,cache_timeout=0)
Когда я запускаю свое приложение следующим образом Я получаю следующую ошибку
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
Поэтому я пытаюсь изменить функцию для следующего:
def function_name():
filename = 'ohhey.pdf'
with app.app_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
и получаю эту новую ошибку
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
, поэтому я попробуйте следующее:
def function_name():
filename = 'ohhey.pdf'
with app.test_request_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
После внесения этого последнего изменения мое приложение не вернет файл или ошибку. Я ценю вашу помощь.