When I run the code:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello, world!"
if __name__ == '__main__':
app.run(debug=True)
I get the string "Hello, world!" in the chrome browser, but when I change the code:
return "Hello, world!"
to:
return "Hello"
The browser also shows the "Hello, world!" not the "Hello".
How does that happen?
Maybe the page is cached by your browser? Use Ctrl + F5 to force reload , or append ?foo=1 to the url to make your browser make a new request to your web app.
Related
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
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
from flask import Flask
app = Flask(__name__)
#app.route('/hello')
def hello_world():
return "Hello world"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8000, debug=True)
While clicking the link http://0.0.0.0:8000/, it is showing:
> The requested URL was not found on the server If you entered the URL
> manually please check your spelling and try again.
The only URL you have defined is /hello, so when you request /, there is nothing to be served, so you get the Not Found Error. You'll need to either define something for / or use http://0.0.0.0:8000/hello instead. E.g.,
#app.route('/')
def index_view():
return "hello from index"
In my local host, I'm unable to see any message. I am using jupyter notebook.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0',port=5005)
Working with Python Dash and have it working from local host, but when attempt to deploy to my python app server, I have issues.
When I keep the app as just Flask it works with this code:
from flask import Flask
import dash
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
When I try to pass the server to the Dash instance (when according to Dash docs is acceptable, I receive the error). Here is the code
from flask import Flask
import dash
server = Flask(__name__)
app = dash.Dash(__name__, server=server)
#app.route("/")
def hello():
return "Hello World!"
I receiving the error:
AttributeError: 'Dash' object has no attribute 'route'
The docs don't say what you think they do. app is the Dash instance, not the Flask one - that is available via the server variable, so you can call route on that.
#server.route("/")
def hello():
return "Hello World!"
As Daniel Roseman said, you must use server.route instead of app.route.
However, Dash registers itself to serve the path /, overwriting your route.
Other paths not used by Dash work as expected, e.g.:
#server.route('/hello-world')
def hello():
return "Hello World!"
Consider the following minimal working flask app:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "I am /"
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
This happily works. But when I try to make a GET request with the "requests" module from the hello route to the api route - I never get a response in the browser when trying to access http://127.0.0.1:5000/
from flask import Flask
import requests
app = Flask(__name__)
#app.route("/")
def hello():
r = requests.get("http://127.0.0.1:5000/api")
return "I am /" # This never happens :(
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
So my questions are: Why does this happen and how can I fix this?
You are running your WSGI app with the Flask test server, which by default uses a single thread to handle requests. So when your one request thread tries to call back into the same server, it is still busy trying to handle that one request.
You'll need to enable threading:
if __name__ == "__main__":
app.run(threaded=True)
or use a more advanced WSGI server; see Deployment Options.