2

Running routes.py

@app.route('/articles', methods=['GET', 'POST'])
def articles():
    upload()
    return render_template('articles.html')

And this function that saves an image and processes its information

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

How do I make use of the variables label and probability in rendering the template? Some people use something close to:

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

This would be done to make a reference to variables using js. But how do I make a reference to that variable if it is computed inside of upload()? Any need for global variables?

3
  • You can return those variables from the function. Commented Jul 7, 2020 at 21:41
  • if i return them like return label, probability will I be able to use them directly somewhere else or i should instead define label, probability = upload() ? Commented Jul 7, 2020 at 21:43
  • You have to unpack the variable you are sending from function, so first, you have to return them from upload and then unpack it to send it to render_template. Commented Jul 7, 2020 at 21:47

1 Answer 1

2

You can return those variables from the function.

You have to unpack the variable you are sending from upload function.

First, you have to return them from upload and then unpack it to send it to 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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, now html/javascript id for each will be the names specified in render_template() right?
I am not sure about what is id here?
For example, it will be easy to use <h3 id="label"></h3> and have the text in label be displayed?
Yes, you can certainly do that by using something like this {{ label }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.