0

I'm making a web app in the flask. I can't properly carry out my idea. In this case I want to receive some data from forms on the HTML page and send it to another script.py file. Then this script.py shall do some maths and return me and a couple of string variables. I want to receive them like a result of maths and put them separately on the HTML page ( like {{ var1 }} ).

Hhere is some code so that you roughly understand what the situation looks like now:

routes.py:

import some script as spst

...

@app.route('/calculate')
def calc_func():
    data1 = request.form['form1input']   # timepicker input like 07:30
    data2 = request.form['form2input']   # string 'hello'
    data3 = request.form['form3input']   # int 55
    fdata = data1[0:2]                   # 07
    fdata = data1[3:5]                   # 30
    fdata = data2                        # 'hello'
    fdata = data3                        # 55
    spst.mainclass.mainfunc(fdata1, fdata2, fdata3, fdata4)
    # how to catch results from function above???
    return render_template('index.html', output_text1, output_text2, output_text3)

somescript.py:

class mainclass(object):
    def mainfunc(fdata1, fdata2, fdata3, fdata4):
        localvar1 = int(fdata1)
        localvar2 = int(fdata2)
        localvar3 = str(fdata3)
        localvar4 = int(fdata4)
        # there is some maths and other actions
        output_text1 = str(result1)
        output_text2 = str(result2)
        output_text3 = str(result3)
        return output_text1, output_text2, output_text3

2 Answers 2

1

Firstly you need to set yout parameters correctly since there is nothing defined as fdata1, fdata2, fdata3 passed in your function and after that you can return a dictionary in your somescripyt.py file like this :

return {'output_var1': output_text1, 'output_var2': output_text2, 
        'output_var3': output_text3}

And then in your calc_func() in routes.py file just pass the returned result like this :

def calc_func():
    data1 = request.form['form1input']   # timepicker input like 07:30
    data2 = request.form['form2input']   # string 'hello'
    data3 = request.form['form3input']   # int 55
    fdata = data1[0:2]                   # 07
    fdata = data1[3:5]                   # 30
    fdata = data2                        # 'hello'
    fdata = data3                        # 55

    result = spst.mainclass.mainfunc(fdata1, fdata2, fdata3, fdata4)
    return render_template('index.html', result=result)

And then in your index.html you can use it in Jinja like below :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
<div>
    {% if result %}
        {% for obj in result.keys() %}
            {{ result[obj] }}
        {% endfor %}
    {% endif %}
</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You must assign the return value of your function to variables like this:

(output_text1, output_text2, output_text3) = spst.mainclass.mainfunc(fdata1, fdata2, fdata3, fdata4)
# how to catch results from function above???
return render_template('index.html', output_text1, output_text2, output_text3)

Right? Because those names in mainclass / mainfunc won't get carried out of the function call. The function returns 3 values, which you must assign to something to be able to use them.

Comments

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.