FLASK
Introduct
Flask is a web application framework written in
ion
Python. Armin Ronacher, who leads an international group of
Python enthusiasts named Pocco, develops it. Flask is based on
Werkzeug WSGI toolkit and Jinja2 template engine. Both are
Pocco projects.
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.
Jinja2 is a popular templating engine for Python.
A web templating system combines a template with a certain
What is Web Framework?
Web Application
Framework or simply Web Framework
represents a collection of libraries and
modules that enables a web application
developer to write applications without having
to bother about low-level details such as
protocols, thread management etc.
Installation
Virtual environments
• Use a virtual environment to manage the dependencies for
your project, both in development and in production.
• What problem does a virtual environment solve? The more
Python projects you have, the more likely it is that you need to
work with different versions of Python libraries, or even Python
itself. Newer versions of libraries for one project can break
compatibility in another project.
• Virtual environments are independent groups of Python
libraries, one for each project. Packages installed for one
project will not affect other projects or the operating system’s
packages.
• Python comes bundled with the venv module to create virtual
Create an environment
1st Open Python Directory in CMD using Admin Mode
Create a project folder and a venv folder within:
> mkdir myproject
> cd myproject
> py -3 -m venv venv
Activate the environment
Before you work on your project, activate the
corresponding environment:
> venv/Scripts/activate
Install Flask
Within the activated environment, use the
following command to install Flask:
> pip install Flask
A Hello World Application
A minimal Flask application looks something like this:
from flask import Flask
app = Flask(__name__)
@[Link]("/")
def hello_world():
return "<p>Hello, World!</p>"
Save This File in Python Directory
So what did that code do?
• First we imported the Flask class. An instance of this class will be our WSGI
application.
• Next we create an instance of this class. The first argument is the name of
the application’s module or package. __name__ is a convenient shortcut for
this that is appropriate for most cases. This is needed so that Flask knows
where to look for resources such as templates and static files.
• We then use the route() decorator to tell Flask what URL should trigger our
function.
• The function returns the message we want to display in the user’s browser.
The default content type is HTML, so HTML in the string will be rendered by
the browser.
Save it as [Link] or something
similar. Make sure to not call your
application [Link] because this
would conflict with Flask itself.
To run the application, use the flask
command or python -m flask. You
need to tell the Flask where your
application is with the --app option.
$ flask --app hello run
* Serving Flask app 'hello'
* Running on [Link] (Press
CTRL+C to quit)
GET POST in Flask
Using HTML Page
Create a Web Page in HTML & Save it in Python Directory.
<html>
<body>
<form action =
"[Link] method =
"post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm"
/></p>
<p><input type = "submit" value =
"submit" /></p>
</form>
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
@[Link]('/success/<name>')
def success(name):
return 'welcome %s' % name
@[Link]('/login', methods=['POST', 'GET'])
def login():
if [Link] == 'POST':
user = [Link]['nm']
return redirect(url_for('success', name=user))
else:
user = [Link]('nm')
return redirect(url_for('success', name=user))
if __name__ == '__main__':
[Link](debug=True)
<!DOCTYPE html>
<html lang="en">
<head> GET POST in Flask Using HTML Page
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Sum of Two Numbers</title>
</head> templates/
<body>
<h1>Sum of Two Numbers</h1> [Link]
<form action="/sum" method="post">
<label for="num1">Enter first number:</label><br>
<input type="number" id="num1" name="num1"><br>
<label for="num2">Enter second number:</label><br>
<input type="number" id="num2" name="num2"><br><br>
<input type="submit" value="Calculate">
</form>
</body>
from flask import Flask, render_template,
request
app = Flask(__name__)
[Link]
@[Link]('/')
def index():
return render_template('[Link]')
@[Link]('/sum', methods=['POST'])
def sum_numbers():
num1 = int([Link]['num1'])
num2 = int([Link]['num2'])
result = num1 + num2
return render_template('[Link]',
result=result)
if __name__ == '__main__':
<!DOCTYPE html>
<html lang="en"> templates/
<head> [Link]
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<p>The sum of the two numbers is:
{{ result }}</p>
</body>