Python Flask-Flask Application

How does flask app works?

A small web applications inside a Python file and run it to start the server, which will display some information on the browser. Open a file named class.py for editing. This class.py file will serve as a minimal example of how to handle an HTTP requests. Import the Flask object, and create a function that returns an HTTP response. The code lets us run a basic web application that we can serve, as if it were a website.
The following code inside class.py:

Example:
     from flask import Flask
     app = Flask(__name__)
     @app. route("/")
     def home():
     return "Hello, World!"
     if __name__ == "__main__":               
     app.run (debug=True)
This code is stored in our class.py

  • Here we are importing the Flask module and creating a Flask web server from the Flask module. Then, use it to create your flask application instance with name app. When we pass a special variable __name__ which holds the name of current python module.
  • Once you create the app instance, use it to handle incoming web request and send response to the user. @app. route is a decorator which turns the python function into Flask view function, it convert a function return value into HTTP response that displayed by an HTTP client, such as web browser. When we pass the value '/' to @app. route () to signify that the function will respond to web requests for an URL / , which is the main URL. When we run the Python script, python assigns the name “__main__” to the script when executed. If we import another script, the if statement will prevent other scripts from running. When we run class.py, it will change its name to and only then will that if statement activate. This will run the application. Having debug=True allows possible Python errors to appear on the web page. This will help us to find out the errors.
To run your web application, first tell Flask where to find the application (the class.py file in your case) with the FLASK_APP environment variable:
     export FLASK_APP=hello
Then run it in development mode with the FLASK_ENV environment variable:
     export FLASK_ENV=development
Run the application with flask run command.

The output of running application will be something like the following one:

Output
Serving Flask app "hello" (lazy loading)
Environment: development
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat
Debugger is active!
Debugger PIN: 713-894-367
The above output has several information such as,

  • The application is running locally on the URL http://127.0.0.1:5000/, where 127.0.0.1 is the IP that represents your machine’s localhost and:5000 is the port number.
  • In a flask, the debugger is running which signifies that the debug mode is on. When developing it is useful that it gives a detailed error message when things go wrong which makes troubleshooting easier.
  • Open a browser and type in the URL http://127.0.0.1:5000/, you will receive the string “Hello, World!” as a response, this confirms that your application is successfully running.
Now leave the development server running in the terminal and open another terminal window. Move into project folder where class.py is located, activate the virtual environment, set the environment variables like FLASK_ENV and FLASK_APP, and continue to the next steps. We have a small Flask web application. We have run the application and displayed information on the web browser.