0

i am trying to run my app.py which is in flask with "python app.py" instead of "flask run". i have the following in the app.py. but when i run it via python, the html pages dont show up and says requested url not found. i am not sure what i am missing here. keep in mind the app runs fine when i use flask run command. the webpage shows on the browser fine. but when i use the "python app.py" the browser says the url is not found. i am not sure if i need to redirect something or not



app = Flask(__name__)

if __name__ == "__main__":
   
 app.run(host="10.147.180.15", port="80", debug=True)

@app.route('/')
def index():
    return render_template("index.html")

any help will be greatly appreciate it

3
  • Your example (mostly) works for me (app needs to be defined before the decorator for app, but I assume you have that part working). Where are you seeing the error message, in the browser or in the logs from your application? Commented Sep 2, 2020 at 20:06
  • The error is in the browser Commented Sep 2, 2020 at 20:14
  • This question would benefit from a Minimal, Reproducible Example. Commented Sep 2, 2020 at 20:33

1 Answer 1

2

You should declare app before initiating routes:

app = Flask(__name__)
@app.route('/')
def index():
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)

Edited: fixed indentation

Sign up to request clarification or add additional context in comments.

3 Comments

i apologize i have copy/paste it wrong, i have edited the post with the correct code
yes it works :) thanks so much now i can deploy my iis ;) can you please explain what is the difference if the "if statemenet be at the top or at the end of the code?
Either way, it shouldn't cause any problem, i was responding to your initial post (before the update). There, you had app = Flask(__name__) after app.run and that's more like referencing a variable before its declaration.

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.