Building an webpage using python.
There are many modules or frameworks which allows to build your webpage using python like bottle, django, flask etc. But the real popular ones are Flask and Django. Django is easy to use as compared to Flask but FLask provides you the versatility to program with.
To understand what Flask is you have to understand few general terms.
- WSGI Web Server Gateway Interface (WSGI) has been adopted as a standard for Python web application development. WSGI is a specification for a universal interface between the web server and the web applications.
- Werkzeug It is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it. The Flask framework uses Werkzeug as one of its bases.
- jinja2 jinja2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.
Flask is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
We will require two package to setup your environment. virtualenv for a user to create multiple Python environments side-by-side. Thereby, it can avoid compatibility issues between the different versions of the libraries and the next will be Flask itself.
pip install virtualenv
pip install Flask
After completing the installation of the package, let’s get our hands on the code.
Save it in a file and then run the script we will be getting a output like this.
Then go to the url given there you will seeing your first webpage displaying hello world there on your local server.
Digging further into the context, the route() decorator in Flask is used to bind URL to a function. Now to extend this functionality our small web app is also equipped with another method add_url_rule() which is a function of an application object is also available to bind a URL with a function as in the above example, route() is used.
def gfg(): return ‘geeksforgeeks’ app.add_url_rule(‘/’, ‘g2g’, gfg)
You can also add variables in your webapp, well you might be thinking how it’ll help you, it’ll help you to build an url dynamically. So lets figure it out with an example.
and go to the url http://127.0.0.1:5000/hello/geeksforgeeks it’ll give you the following output.
We can also use HTTP methods in Flask lets see how to do that
HTTP protocol is the foundation of data communication in world wide web. Different methods of data retrieval from specified URL are defined in this protocol. The methods are described down 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 the GET requests. However, this preference can be altered by providing methods argument to route() decorator.
In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL.
Now let’s create a html login page.
Below is source code of the file
Now save this file html as try this python script to create the server.
After the development server starts running, open login.html in the browser, enter name in the text field and click submit button. The output would be the following.
Result will be something like this
And there’s much more to Flask than this. If you are interested in this web framework of Python you can dig into the links provided down below for further information.
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.