Use Flask Session Reducing Server Side Resource with Python

blog details
author


Optimizing cost from server side is a forever inevitable discussion Also, increasing the loading speed is indispensable. Both of them are incredibly important in all Web3, Web App or AI projects. I assume you are a fan of Python and love making apps with its seasoning. So, this piece can fit your stomach. I would walk through briefly up-to-date scripts to display content using Python and Flask sessions.

Table of Contents: Leverage Flask Session Cookies Reducing Server side Resource Using Python


Flask session and library import

Flask’s sessions are client-side sessions. Any data that you write to the session is written to a cookie and sent to the client to store. The client will send the cookie back to the server with every request, that is how the data that you write in the session remains available in subsequent requests

Installing a Flask session needs two libraries. Here are both of those as follow:

  • from flask_session import Session
  • From flask import session<. /span>

Configure your app with Flask session

There are many different parameters developers can use to customize the session features based on the app needs. Basically there are two must-have settings:

  • app.config['SESSION_PERMANENT']= False
  • Session(app)

Create and append a list of session

Generally, the data type of session is a list. For example, one user can have different behavior records stored in the session list. Although the cookieless era has impacted the advertising sector to target audiences with personalized ads, in terms of app UX and content personalisation, it is still a key feature.

There are two main cases when setting up a session list as follows:

  • None of records and create from scratch

session['cookies'] = [newUserCookiesLake]

  • Existing records and add new on top of that

session[‘cookies’'].append(newUserCookiesLake)

Jinja 2 settings in frontend with Flask session

As well as dataset from render template, flask session can display data value based on specific settings in frontend. The difference is the dataset by using Flask session is from Client side instead of server side. That’s why it can save resources on your server and increase loading speed. Thus, we don’t need to assign any dataset in the backend by using the render template. It still can work with Jinja2.


{% If session['cookies'] %}
{% else %}

The other difference you need to keep in mind is the storage duration. Flask session as you can see the setting above is [‘SESSION_PERMANENT’]= False. That means it’s temporary and conditions matter and affect the data display. Basically it runs by browser.

Take this sample of using BuyfromLo Web content scraping API. The scraping record shown as below would go away if the user cleans up the cookies or changes another device to log into BuyfromLo account. Thus, when you build an App that requires those session dataset for a long-term running, you need to deploy further steps to sync with your database.


Flask Session Playbook


No Comment at the moment...