MEHUL SOLANKI
MEHUL SOLANKI

Reputation: 47

Not able to reach Flask webserver

This is my test code running on Visual studio 2017, file name is test:

from flask import Flask, request      #import main Flask class and request object
from test import app

app = Flask(__name__) #create the Flask app

@app.route('/query-example')
def query_example():
    return 'Todo...'

@app.route('/form-example')
def formexample():
    return 'Todo...'

@app.route('/json-example')
def jsonexample():
    return 'Todo...'

if __name__ == '__main__':
    app.run(debug=True, port=5000) #run app in debug mode on port 5000

But when I run it in Visual studio 2017 and I enter this route "http://127.0.0.1:5000/json-example" in Chrome Browser I always get this error message.

"404 Not Found" The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

I am a learner, tried following this article: https://scotch.io/bar-talk/processing-incoming-request-data-in-flask

Earlier I used to get the return message with the same code but after updating the flask to 0.12.4, its not working, I think something has changed. I am not able to debug the exact issue.

--Update--

Ok, after updating the flask version to 1.0.2, but still not able to reach the webserver from the url: http://127.0.0.1:5000/json-example. Please check out the screenshot below.

enter image description here

I get 404 error not found:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

Upvotes: 0

Views: 1867

Answers (3)

Dimitar Petkov
Dimitar Petkov

Reputation: 71

I had the same issue running VS Code on Ubuntu 19.10. The problem was returned string from route view function.

@app.route('/')
@app.route('/index')
def index():

    return 'Hello World!'

gives error 404. But when I changed to :

@app.route('/')
@app.route('/index')
def index():

    return '<h1>Hello World!</h1>'

everything was just fine.

Upvotes: 0

MEHUL SOLANKI
MEHUL SOLANKI

Reputation: 47

While I was troubleshooting, I noticed that the above same code works in raspberry Pi zero module with same python and flask version. But it is still not working in visual studio on windows 7. Probably there might be some issue with my visual studio. I will uninstall it reinstall it again. But yes my issue is solved for now. Thanks everyone for answering.

Upvotes: 0

J_H
J_H

Reputation: 20415

The docs suggest that you should verify that .env & .flaskenv files are not interfering. (Likely they are not.)

shadowing

You really need to avoid shadowing one symbol with another.

Please don't name your module test, as python ships with a system library with that name. You might use test1 instead, to avoid the needless possibility of confusion.

Please rename your global variable to app_, since your module has an app.py file.

root URL

Please add a / slash route, even though it's not strictly necessary. You will find that it aids debugging.

@app_.route('/')
def root():
    return '<h1>top level</h1>'

version

Run a current version of flask, please. It's much better to report problems with current rather than downrev code.

execution

Rather than having python directly call app_.run(...), please run flask instead. Use export or env, whichever you prefer:

$ export FLASK_APP=test1 FLASK_ENV=development
$ env    FLASK_APP=test1 FLASK_ENV=development flask run --port=5000

This enables debug mode, which should help you get to the bottom of your routing issues. Plus, reloading after edits is quite convenient. The key is that rather than running python, you run flask which in turn runs python.

Upvotes: 2

Related Questions