Here's the code:-
from flask import Flask
app= Flask(__name__)
#app.route('/hello')
def hello_world() :
return 'hello World'
app.add_url_rule('/','hello','hello_world')
if __name__== '__main__':
app.run(debug=True)
Please see if u can find an error, please type the correct code in the answer
Please see documentation for add_url_rule, required method name instead you will give in string format for 'hello_world', hello_world
from flask import Flask
app= Flask(__name__)
#app.route('/hello')
def hello_world():
return 'hello World'
app.add_url_rule('/','hello', hello_world)
if __name__== '__main__':
app.run(debug=True)
Related
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"
I'm attempting to deploy a simple web app, and I'm using command line waitress-serve --call command. But every time, the command immediately returns 1. Malformed application 'name_of_project_here'.
Here's my flask web app in python:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run(debug = True)
and the command I run is just
waitress-serve --call "name_of_project"
I did try looking through the documentation, and I found where the error occurs, but couldn't find an explanation of why it's occurring. What does malformed application mean?
Minimal working example:
If you put code in file main.py
from flask import Flask
def create_app():
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
return app
if __name__ == '__main__':
app = create_app()
app.run()
then you can run it as
waitress-serve --call "main:create_app"
So "name_of_project" has to be "filename:function_name" which creates Flask() instance.
It can't be any text. And if you forget : then you may see "Malformed application"
when defining your appname and the function that initialize your app with : dont put them into quotes('')
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)
I got this very simple Python code, but somehow I always get an error:
IndentationError: expected an indented block
I checked all tabs and spaces twice and I can not find the mistake.
Can someone please give me a hint?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!\n'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
May be it is due the editor or improper indentation.
Your code is perfectly running in my pc but I would suggest to run the below code
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(port=8080)
For me worked this in the docker example.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
I want to change the default 404 code, when flask doesn't finds the route, to other code. How can I do that?
As already said, it is generally not a good idea to redefine the meaning of standard status codes.
Although you can change a status code returned, here's an example:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
#app.errorhandler(404)
def page_not_found(error):
return 'This page does not exist', 777
if __name__ == '__main__':
app.run()
This will return status code 777 on any page other than /.
Here's a result:
More on the topic you can find here.