Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

MCA sem-1: Python Unit-5 Notes

Q-1: What is Flask?

Ans: Flask is a web framework that provides libraries to build lightweight web applications in
python.

• It is developed by Armin Ronacher who leads an international group of python enthusiasts


(POCCO).

• It is based on WSGI toolkit and jinja2 template engine.

• Flask is considered as a micro framework.

Q-2: How to set flask environment in windows?

• Install python 2.7 or higher version.

• Install virtual environment:

$ pip install virtualenv

• create the new virtual environment into a folder:

$ mkdir new

$ cd new

$ virtualenv venv

• To activate the corresponding environment:

$ venv\scripts\activate

• Install flask:

$ pip install flask

Python: Unit-5 Page 1


Q-3: Describe Flask HTTP methods.

• HTTP is the hypertext transfer protocol which is considered as the foundation of the data
transfer in the world wide web.
• All web frameworks including flask need to provide several HTTP methods for data
communication.
• The methods are given in the following table.
SNo. Method Description
1 GET It is the most common method which can be used to send data in the
unencrypted form to the server.
2 HEAD It is similar to the GET but used without the response body.
3 POST It is used to send the form data to the server. The server does not cache
the data transmitted using the post method.
4 PUT It is used to replace all the current representation of the target resource
with the uploaded content.
5 DELETE It is used to delete all the current representation of the target resource
specified in the URL.

Q-4: Discuss advantages and disadvantages of using Python Flask.

Advantages:

• Lightweight and Flexible:

Flask is a lightweight web framework that allows developers to have more flexibility in building
web applications.

• Easy to Learn and Get Started:

Flask has a simple and intuitive API that makes it easy for developers to get started quickly.
Flask provides clear documentation, tutorials, and a large community of users and developers,
making it easy to find support and resources online.

• Flexible Routing:

Flask provides easy-to-use routing capabilities that allow developers to define URL routes for
handling different HTTP requests. Flask follows the WSGI (Web Server Gateway Interface)
specification, which makes it compatible with a wide range of web servers.

Python: Unit-5 Page 2


• Templating Engine:

Flask comes with a built-in templating engine called Jinjaa, which provides a powerful and
flexible way to render dynamic HTML templates. Jinja2 provides features like template
inheritance, filters, loops, and conditionals, making it a powerful tool for rendering dynamic
content in web applications.

Disadvantages:

• Lack of Built-in Features:

Flask follows a minimalistic approach and does not come with all the built-in features that
other larger web frameworks may have. While this allows for flexibility and customization, it
also requires additional effort to find, integrate, and maintain these external components.

• Steeper Learning Curve for Complex Applications:

While Flask has a simple and intuitive API, it may require a steeper learning curve for more
complex web applications compared to other larger frameworks

• L ack of Opinionated Structure:

Flask does not enforce a specific structure or architecture for organizing the application’s
code, leaving it up to the developers to decide how to structure their project.

• Limited Built-in Tools for Large-scale Applications:

Flask is designed as a micro-framework and may not be the best choice for very large or
complex applications that require advanced features such as caching, load balancing, and
advanced security measures.

• Less Robust for Enterprise-level Applications:

Flask is primarily designed for small to medium-sized applications, and it may not be the best
choice for large-scale enterprise-level applications that require complex features, extensive
scalability, and high performance.

Python: Unit-5 Page 3


Q-5: With a neat diagram explain the WSGI server in Python Flask.

WSGI Servers:

• A Web Server Gateway Interface (WSGI) server implements the web server side of the
WSGI interface for running Python web applications.

Why is WSGI necessary?

• A traditional web server does not understand or have any way to run Python
applications.

• Therefore the Python community came up with WSGI as a standard interface that
modules and containers could implement. WSGI is now the accepted approach for
running Python web applications.

• WSGI is a specification that defines how a web server should communicate with a
Python application server. It specifies a simple interface for handling HTTP requests and
responses.

Q-6: Demonstrate with help of an example the use of Jinja Template.

• Templates are files that contain static data as well as placeholders for dynamic data.

• A template is rendered with specific data to produce a final document.

• Flask uses the Jinja template library to render templates. In your application, you will use
templates to render HTML which will display in the user's browser.

Python: Unit-5 Page 4


Templates example-Main Python File:

rom flask import Flask, render_template, redirect, url_for

app = Flask(__name__)

@app.route("/")

def home():

return render_template("index.html")

@app.route("/default")

def default():

return render_template("layout.html")

@app.route("/variable")

def var():

user = “DSU"

return render_template("variable_example.html", name=user)

@app.route("/if")

def ifelse():

user = “DSSSU"

return render_template("if_example.html", name=user)

@app.route("/for")

def for_loop():

list_of_courses = ['Java', 'Python', 'C++', 'MATLAB']

return render_template("for_example.html", courses=list_of_courses)

Python: Unit-5 Page 5


Q-7: Illustrate Jinja Template for for loop in Flask.

Jinja for loop syntax is similar to the if statements, the only difference is for loop requires
sequence to loop through.

Syntax of Jinja Template for Loops:

{% for variable_name in sequence%}


...
...
...
{% endfor %}
Example:
<!DOCTYPE html>
<html>

<head>

<title>For Example</title>

</head>

<body>

<h2> DSU Available Course </h2>

{% for course in courses%}

<h4> {{course}} </h4>

{% endfor %}

</body>

</html>

OUTPUT:
DSU Available Course
Java
Python
C++
Matlab
Python: Unit-5 Page 6
Q-8: Illustrate Jinja Template for if construct in Flask.

Just like declaring the variable in the Jinja2 template, if conditions have almost similar syntax.
But here we specify the beginning and end of the if block.

Syntax of Jinja Template if Statements:

{% if conditions %}
...
...
...
{% endif %}
Our app.py will stay the same. Just only one change instead of Geeksforgeeks tries giving
Geeks so that we can verify the else block.
Example:
<!DOCTYPE html>

<html>

<head>

<title>If example</title>

</head>

<body>

{% if(name == “DSU") %}

<h3> Welcome </h3>

{% else %}

<h3> Unknown name entered: {{name}} </h3>

{% endif %}

</body>

</html>
Output:
Unknown name entered: DSSSU

Python: Unit-5 Page 7

You might also like