Home » First Flask Application

First Flask Application

by Online Tutorials Library

First Flask application

In this section of the tutorial, we will build our first python website built using the Flask framework. In this process, open any text editor of your choice as we are using the sublime text editor in this tutorial.

Write the following lines of code and save to a file named as script.py.

Let’s run this python code on the command line and check the result.

First Flask application

Since it is a web application, therefore it is to be run to on the browser at http://localhost:5000.

First Flask application

To build the python web application, we need to import the Flask module. An object of the Flask class is considered as the WSGI application.

We need to pass the name of the current module, i.e. __name__ as the argument into the Flask constructor.

The route() function of the Flask class defines the URL mapping of the associated function. The syntax is given below.

It accepts the following parameters.

  1. rule: It represents the URL binding with the function.
  2. options: It represents the list of parameters to be associated with the rule object

As we can see here, the / URL is bound to the main function which is responsible for returning the server response. It can return a string to be printed on the browser’s window or we can use the HTML template to return the HTML file as a response from the server.

Finally, the run method of the Flask class is used to run the flask application on the local development server.

The syntax is given below.

SN Option Description
1 host The default hostname is 127.0.0.1, i.e. localhost.
2 port The port number to which the server is listening to. The default port number is 5000.
3 debug The default is false. It provides debug information if it is set to true.
4 options It contains the information to be forwarded to the server.

Next TopicFlask App Routing

You may also like