There are many modules or frameworks that allow you to build your web page using python like bottle, django, flask, etc. Django is easy to use compared to Flask, but FLask gives you programming versatility.
To understand what Flask is, you need to understand a few general terms.
Flask — is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and the Jinja2 templating engine. Both Pocco projects.
We need two packages to set up your environment. Virtualenv for the user to create multiple Python environments side-by-side. This way it can avoid compatibility issues between different versions of libraries, and the next one will be Flask itself.
pip install virtualenv
pip install Flask
After the package installation is complete, let's go through the code. p>
Save it to a file and Then run the script, we get output like this.
Then go to the URL given there and you will see your first web page displaying hello on your local server.
Next, delving deeper into context, the route () decorator in Flask is used to bind a URL to a function. Now, to extend this functionality, our small web application is also equipped with another add_url_rule () method, which is also a function of the application object, which is also available to bind the url using a function like in the above example, route ().
def gfg (): return 'pythonengineering' app.add_url_rule ('/', 'g2g', gfg)
You can also add variables to your web application, and you might think how it will help you, it will help you dynamically create url. So let's go with an example.
and follow the link http://127.0.0.1:5000/hello/pythonengineering, and you will get the following output .
We can also use HTTP methods in Flask, let's see how.
The HTTP protocol is the backbone of data transfer on the world wide web. This protocol defines various methods to retrieve data from a specified URL. The methods are described below.
GET: Sends data in simple or unencrypted form to the server.
HEAD: Sends data in simple or unencrypted form to the server without body.
HEAD: Sends form data to the server. Data is not cached.
PUT: Replaces target resource with the updated content.
DELETE: Deletes target resource provided as URL.
By default, the Flask route responds to GET requests. However, this preference can be changed by providing a method argument to the route () method.
To demonstrate the use of the POST method in URL routing, let's first create an HTML form and use the POST method to submit data forms in the url.
Now let's create a login page in html.
Below is the source code of the file
Now save this html file as try this script on python to create a server.