Getting started with Flask

We can kick-start our application development with a simple example, which gives you an idea of how we program in Python with a flask framework. In order to write this program, we need to perform the following steps:

  1. Create a WSGI application instance, as every application in Flask needs one to handle requests from the client.
  2. Define a route method which associates a URL and the function which handles it.
  3. Activate the application's server.

Here is an example which follows the preceding steps to make a simple application:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello Guest!"

if __name__ == "__main__":
    app.run()

In the preceding lines of code, we have created a WSGI application instance using the Flask's Flask class, and then we defined a route which maps the path "/" and the view function home to process the request using a Flask's decorator function Flask.route(). Next, we used the app.run() which tells the server to run the code. And at that end, it will result in a web page showing up "Hello Guest!", when the code is executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.165