TangoLite
TangoLite

Reputation: 17

Print result to webpage via Flask (Python)

I hope someone can give me a hand with this. I'm trying to print the output of a calculation result onto a webpage and it's just not showing - I really don't understand how the exact same function is working perfectly for another result, but not anything else.

from flask import Flask, render_template

app = Flask(__name__)

stackList = []
latestNumber = 0


@app.route('/')
@app.route('/calc/')
@app.route('/calc/peek')
def calcPeek():
    return render_template('peek.html', stackList=stackList)

@app.route('/calc/push/<number>')
def pushNumber(number):
    stackList.append(number)
    latestNumber = number
    print(stackList)
    return render_template('push.html', number=number, latestNumber=latestNumber)

@app.route('/calc/pop')
def calcPop():
    stackList.pop()
    return render_template('pop.html', stackList=stackList)

@app.route('/calc/add')
def calcAdd():
    numOne = stackList[-1]
    numTwo = stackList[-2]
    addResult = int(numOne) + int(numTwo)
    stackList.pop()
    stackList.pop()
    stackList.append(addResult) 
    print(addResult)
    return render_template('add.html', stackList=stackList, addResult=addResult)

@app.route('/calc/subtract')
def calcSubtract():
    numOne = stackList[-1]
    numTwo = stackList[-2]
    minusResult = int(numOne) - int(numTwo)
    stackList.pop()
    stackList.pop()
    stackList.append(minusResult) 
    print(minusResult)
    return render_template('add.html', stackList=stackList, minusResult=minusResult)

And here's the add.html file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add</title>
</head>
<body>
    <p>Result: {{addResult}} </p>
</body>
</html>

The peek, push/number and add functions all work fine and give me the result I need. However, if I try the same function with divide/multiply/subtract it doesn't actually show anything on the webpage - No error or anything. The result prints to the console but doesn't generate any output for the webpage. Subtract.html below

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Subtract</title>
</head>
<body>
    <p> {{minusResult}} </p>
</body>
</html>

Is there anything I'm missing thats completely obvious here? Thanks for any help with this in advance

Upvotes: 0

Views: 441

Answers (1)

Kishore Sampath
Kishore Sampath

Reputation: 1001

Return statement on your calcSubtract function should be return render_template('subtract.html', minusResult=minusResult). There is no need to pass stacklist.

Code Snippet:

@app.route('/calc/subtract')
def calcSubtract():
    numOne = stackList[-1]
    numTwo = stackList[-2]
    minusResult = int(numOne) - int(numTwo)
    stackList.pop()
    stackList.pop()
    stackList.append(minusResult) 
    print(minusResult)
    return render_template('subtract.html', minusResult=minusResult)

Upvotes: 1

Related Questions