Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am accepting some data via post request on my root URL in flask and then create the PDF from that data.

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

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.

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.

How do I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
170 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is no need that every function is a view function!

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...