English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To test if Flask is installed successfully, enter the following code in the editor and save it to the file: Hello.py.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World' if __name__ == '__main__': app.run()
It is mandatory to import the Flask module in the project. An object of the Flask class is a WSGI application.
The Flask constructor takes the name of the current module (__name__) as a parameter.
The route() function of the Flask class is a decorator that tells the application which URL should call the related function.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 app.route(rule, options)
rule The parameter represents the URL bound to the function. options is the list of parameters to be forwarded to the underlying Rule object.
In the above example, '/The URL is bound to the hello_world() method. Therefore, when the homepage of the web server is opened in the browser, the output of this function will be displayed.
Finally, the run() method of the Flask class runs the application on the local development server.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 app.run(host, port, debug, options)
All parameters in the above method are optional, with the description and explanation as shown in the table below -
Number | Parameter | Description |
1 | host | The hostname of the listener. The default is127.0.0.1(localhost). Set to '0.0.0.0' to make the server accessible externally |
2 | port | Listen on port number, default is:5000 |
3 | debug | Default is: false. If set to: true, debug information is provided |
4 | options | is forwarded to the underlying Werkzeug server. |
The above hello.pyThe script is saved to the D disk (path: D:\hello.py) can be executed from the Python shell. Use the following command -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 $ python hello.py
The output message in the Python shell is as follows -
Open the above URL in the browser (localhost:5000). You will see the 'Hello World' message displayed in the browser.
The Flask application starts by calling the run() method. However, when the application is under development, it should be restarted manually for each change in the code. To avoid this inconvenience, debug support can be enabled. If the code changes, the server will automatically reload. It will also provide a useful debugger to track errors in the application (if any).
To enable debug mode, set the debug attribute of the application object to True before running or passing debugging parameters to the run() method.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 app.debug = True app.run() app.run(debug=True)