Flask run one time with URL link but twice under replit UI

Inside Replit UI, when I click on the Calculate button (with number = 1), the POST method is done and I see the result (Calculated number = 2), but soon after, a GET method is executed : I loose the result and get a empty form.
This doesn’t happen outside Replit UI (with the link in a browser)
I tried to add debug=False and/or use_reloader=False in app.run, without success.

Here is the console log
1
2
172.18.0.1 - - [30/Oct/2022 09:05:33] “POST / HTTP/1.1” 200 - (after the button click)
172.18.0.1 - - [30/Oct/2022 09:05:33] “GET / HTTP/1.1” 200 - (how can I avoid that GET ?)

https://TweetArmy.ebgd.repl.co

main.py

from flask import Flask, render_template, url_for, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
  res = 0
  if request.method == 'POST':
    number = int(request.form['number'])
    print(number)
    res = 2 * number
    print(res)
  return render_template('index.html', result=res)

if __name__ == "__main__":  
  app.run(host='0.0.0.0', port=8080)

templates/index.html

<html>
  <head>
    <title>Test</title>
    <meta charset="UTF-8" />
  </head>
  <body>
    <h1>Test</h1>
    <form action="{{ url_for('index') }}" method="POST">    
      <label  >Number:</label>  
      <input type="text" name="number" value="">  
      <input type="submit" name="next" value="Calculate">    
    </form>
   <h2>Results</h2>
   Calculated number = {{result}}    
  </body>
</html>
1 Like

Hi @ebgd thanks for your question.

Would it be related to @app.route having the methods GET and POST? What happens if you try to remove GET from the methods list?

Also it would be useful if you could post a link to your Repl so other users in the community can see the error for ourselves.

1 Like

https://replit.com/@ebgd/TweetArmy They did in fact post a link (unless they added that after your comment)

1 Like

Thanks @Cosmic . The link is to the web page, not the Repl. Much appreciated.