In this article, we will create Flask application, we will also discuss what it is, how it works, and the various features it provides.
To check if Flask is properly installed, enter the code “welcome.py” in the editor as follows.
from flask import Flask app = Flask(__name__) @app.route('/') def welcome(): return 'Welcome to MRX Flask Tutorial' if __name__ == '__main__': app.run()
It is necessary to include the flask module in the project. The WSGI application for our project is represented by an instance of the Flask class.
The argument that is passed to the Flask constructor is the name of the current module, which is represented by __name__.
In the Flask class, the route() function is considered a decorator that instructs flask application on which URL should be linked to the corresponding function.
- The rule parameter establishes a connection between the URL and the function.
- The options parameter is a collection of arguments that are passed on to the base Rule object.
In the example given, the function called welcome() is connected to the homepage URL ‘/‘. So, when you open the website on a browser, the output of this function will be displayed on the homepage.
To run the website on your computer, you can use the run() method of the Flask class.
app.run(host, port, debug, options)
Flask run() function has additional arguments:
|host||The default hostname that the server listens on is 127.0.0.1, which refers to the local machine. If you want the server to be accessible from external sources, you can change the hostname to ‘0.0.0.0’.|
|port||The default value is set to 5000.|
|debug||The default value is false. When set to true, it enables the provision of debug information.|
|options||This needs to be sent to the core Werkzeug server.|
The Python code mentioned above is run from the Python shell/command line interface.
A notification is displayed in the Python shell/command line interface, indicating that…
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Type the URL (localhost:5000) in your web browser’s address bar and press enter. This will show a message saying “Welcome to MRX Flask Tutorial” on the webpage.
Flask Application Host Mode
In Flask, the run() function has a host parameter that determines the address the server should listen to. By default, Flask’s development server only listens to requests from the local machine, which is identified by the address 127.0.0.1 or “localhost“.
However, you can change the host parameter to 0.0.0.0, which allows the server to accept requests from any available network interface.
This makes the server publicly accessible and can be useful when testing the application on different devices or making it accessible to others on the same network.
To run the Flask application on all available network interfaces, you can simply set the host parameter to 0.0.0.0, like this:
if __name__ == '__main__': app.run(host='0.0.0.0')
Flask Application Port Mode
Flask’s run() function uses the port parameter to determine the server’s listening port number.
Flask’s default development server listens on port 5000, but it can be changed to any other available port number.
To set a custom port number, simply include the port parameter when calling the run() function.
Here’s an example:
if __name__ == '__main__': app.run(port=8000)
Flask Application Debug Mode
When creating a Flask application, you can use the run() method to start it. But during development, you would need to restart the server every time you make a change to the code.
To avoid this hassle, you can activate debug support. This means that the server will automatically reload itself when you make modifications to the code, and it will also come with a debugger to help you identify any errors in your application.
To enable debug mode, you can set the debug property of your Flask application to True, or pass the debug parameter to the run() method when starting the server.
app.debug = True app.run() app.run(debug = True)