Cookies in Flask

A Cookie is a form of text file which is stored on a client’s computer, whose purpose is to remember and track data pertaining to client’s usage in order to improve the website according to the user’s experience and statistic of webpage.

The Request object contains cookie’s attribute. It is the dictionary object of all the cookie variables and their corresponding values. It also contains expiry time of itself. In Flask, cookie are set on response object.See the example given below:

Python3
from flask import Flask 

app = Flask(__name__) 
@app.route('/') 

def index(): 
    return render_template('index.html') 

HTML code ( index.html )

html
<html> 
<body> 
    
    <form action = "/setcookie" method = "POST"> 
        <p><h3>Enter userID</h3></p> 
        <p><input type = 'text' name = 'nm'/></p> 
        <p><input type = 'submit' value = 'Login'/></p> 
    </form> 
        
</body> 
</html> 

Add this code to the Python file defined above

Python3
@app.route('/setcookie', methods = ['POST', 'GET']) 
def setcookie(): 
if request.method == 'POST': 
    user = request.form['nm'] 
    resp = make_response(render_template('cookie.html')) 
    resp.set_cookie('userID', user) 
    return resp 

@app.route('/getcookie') 
def getcookie(): 
    name = request.cookies.get('userID') 
    return '<h1>welcome '+name+'</h1>'

HTML code ( cookie.html )

html
<html> 
    <body> 
        <a href="/getcookie">Click me to get Cookie</a> 
</body> 
</html> 

Run the above application and visit link on Browser http://localhost:5000/ The form is set to ‘/setcookie’ and function set contains a Cookie name userID that will be rendered to another webpage. The ‘cookie.html’ contains hyperlink to another view function getcookie(), which displays the value in browser.

Introduction to Web development using Flask

Flask is a lightweight and flexible web framework for Python. It’s designed to make getting started with web development quick and easy, while still being powerful enough to build complex web applications. Let’s understand Flask Python in more Detail

Similar Reads

What is Flask?

Flask is an API of Python that allows us to build web applications. It was developed by Armin Ronacher. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web application. Flask Python is based on the WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine....

Getting Started With Flask

Python3 is required for the installation of the Python Web Framework Flask. You can start by importing Flask from the Flask Python package on any Python IDE. For installation on any environment, you can click on the installation link given below. To test that if the installation is working, check out the code given below....

Build Flask Routes in Python

Nowadays, the web frameworks provide routing technique so that user can remember the URLs. It is useful to access the web page directly without navigating from the Home page. It is done through the following route() decorator, to bind the URL to a function....

Variables in Flask

The Variables in thePython Web Framework flask is used to build a URL dynamically by adding the variable parts to the rule parameter. This variable part is marked as. It is passed as keyword argument. See the example below...

Build a URL in Flask

Dynamic Building of the URL for a specific function is done using url_for() function. The function accepts the name of the function as first argument, and one or more keyword arguments. See this example...

Serve Static Files in Flask

A web application often requires a static file such as javascript or a CSS file to render the display of the web page in browser. Usually, the web server is configured to set them, but during development, these files are served as static folder in your package or next to the module. See the example in JavaScript given below:...

Cookies in Flask

A Cookie is a form of text file which is stored on a client’s computer, whose purpose is to remember and track data pertaining to client’s usage in order to improve the website according to the user’s experience and statistic of webpage....

Sessions in Flask

In Session, the data is stored on Server. It can be defined as a time interval in which the client logs into a server until the user logs out. The data in between them are held in a temporary folder on the Server. Each user is assigned with a specific...

File-Uploading in Flask

File Uploading in Python Web Framework Flask is very easy. It needs an HTML form with enctype attribute and URL handler, that fetches file and saves the object to the desired location. Files are temporary stored on server and then on the desired location. The HTML Syntax that handle the uploading URL is :...

Sending Form Data to the HTML File of Server

A Form in HTML is used to collect the information of required entries which are then forwarded and stored on the server. These can be requested to read or modify the form. The Python with flask provides this facility by using the URL rule. In the given example below, the ‘/’ URL renders a web page(student.html) which has a form. The data filled in it is posted to the ‘/result’ URL which triggers the result() function. The results() function collects form data present in request.form in a dictionary object and sends it for rendering to result.html....

Alert messages in Flask

It can be defined as a pop-up or a dialog box that appears on the web-page or like alert in JavaScript, which are used to inform the user. This in flask can be done by using the method flash() in Flask. It passes the message to the next template....