如何在flask中运行父函数? [英] How do I run the parent function in flask?
问题描述
我正在烧瓶中通过根请求接受一些数据,然后根据这些数据创建PDF.
I am accepting some data via post request on my root URL in flask and then create the PDF from that data.
在运行父函数之前,我无法生成PDF,该父函数随后使数据可用于pdf.
I can't generate PDF until I run the parent function which then makes the data available for pdf.
如何通过子功能运行父功能.
How do I run the parent function via child function.
@app.route('/', methods=['POST','GET'])
def process_data():
#Some code to get the POST data
x = int(user_input)
y = 5
z = x+y
return z
@app.route('/download')
def download(args=process_data):
a = z+2
return a
您可以看到,我继承了 download
函数中的 process_data
函数.如果我直接转到/download
,则会收到 undefined x variable
错误.
You can see, I've inherited the process_data
function in download
function. If I directly go to /download
I get undefined x variable
error.
我不想一次又一次地运行整个功能.我只需要一些在process_data函数中已处理过的变量即可.
I don't want to run the whole function again and again. I just need some variables that has been processed in process_data function.
我该如何解决?
推荐答案
不需要每个函数都是视图函数!
There is no need that every function is a view function!
如果我正确理解了您的用例,则用户发送数据,然后返回pdf.
If I understood your use case correctly, the user sends data, and you return a pdf.
您可以这样做(伪代码):
You can do it like this (pseudo code):
def process_data(data):
x = int(data)
y = 5
z = x+y
return z
def generate_pdf(data):
#Some code to generate a pdf
return pdf(data)
@app.route('/download', methods=['POST','GET'])
def download_pdf():
#Some code to get the POST data
processed_data = process_data(data)
pdf = generate_pdf(pdf)
return pdf
这篇关于如何在flask中运行父函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!