Flask — (Creating the first simple application)

Python Methods and Functions

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.

  1. WSGI The Web Server Gateway Interface (WSGI) has been adopted as the standard for developing Python web applications. WSGI — it is a specification for a generic interface between web server and web applications.
  2. Werkzeug This is a WSGI toolkit that implements requests, response objects, and other utility functions. This allows you to build a web framework on top of it. Flask uses Werkzeug as one of its foundations.
  3. jinja2 jinja2 — popular template engine for Python. The web template system combines a template with a specific data source to display dynamic web pages.

Flask :

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.

  1. virtualenv
     pip install virtualenv 
  2. flask
     pip install Flask 

After the package installation is complete, let's go through the code.

# Importing the flask module into the project is required
# The Flask class object is our WSGI application.

from flask import Flask

# The flask constructor takes the name
# current module (__name__) as argument.

app = Flask (__ name__)

# The route () function of the Flask class is a decorator
# which tells the application which URL to call
# related function.

@ app . route ( '/' )

# & # 39; / & # 39; The URL is associated with the hello_world () function.

def hello_world ():

return 'Hello World'

# main driver function

if __ name__ = = '__main__' :


# run () method of the Flask class starts the application

  # on the local development server.

app.run ()

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.

from flask import Flask

app = Flask (__ name__)


@ app . route ( ' / hello / & lt; name & gt; ' )

def hello_name (name):

return 'Hello% s!' % name


if __ name__ = = ' __main__' :

app.run ()

and follow the link, 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.

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

& lt ; html & gt; 

& lt; body & gt; 

& lt; form action = " http:// localhost: 5000 / login " method = " post " & gt; 

& lt; p & gt; Enter Name: & lt; / p & gt; 

& lt; p & gt; & lt; input type = "text" name = "nm" / & gt; & lt; / p & gt; 

& lt; p & gt; & lt; input type = "submit" value = "submit" / & gt; & lt; / p & gt; 

& lt; / form & gt; 

& lt; / body & gt; 

& lt; / html & gt; 

Now save this html file as try this script on python to create a server.

After the development server starts, open login.html in a browser, enter a name in the text box and click the submit button. Exit will be as follows.

The result will be something like this

/ figure>

And there is much more to it than Flask. If you are interested in this Python web framework, you can follow the links below for more information.

Links Links:
1) http://flask.pocoo.org/
2) http://flask.pocoo.org/docs/0.12/
3) TutorialsPoint — Infusion

This article courtesy of Subhajit Saha . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.

from flask import Flask, redirect, url_for, request

app = Flask (__ name__)


@ app . route ( '/ success / & lt; name & gt;' )

def success (name):

return 'welcome% s' % name < / code>


@ app .route ( ' / login' , methods = [ 'POST' , 'GET' ])

def login ():

if request .method = = 'POST' :

user = request.form [ 'nm' ]

return redirect (url_for ( 'success' , name = user))

  else :

user = request.args.get ( 'nm' )

return redirect (url_for ( 'success' , name = user))


if __ name__ = = ' __main__' :

app.run (debug = True )