Hello, friends, in this video, we shall see the use of the session object in a flask application.
Sessions are a little different from cookies where the cookies are stored in the client's computer.
The session is stored on the server itself.
So that is the main difference between the cookies and the sessions.
What is the session session?
Is the time interval between the time the user has logged into a Web application to the time the user
is logged out of the application.
So the time interval between these two instances, logging in to an application and logging out of the
application, the time duration is called into session.
So if you want a certain data to be persisted throughout this time interval, then certain session variables
are stored which can be used across the different you else that the user might be visiting during the
course of that session.
So the data, which is not needed to be held across the session is that is those variables.
Those are stored in a temporary directly in the server.
When a client logs into or visits a particular Web application server, then each server, each client
or each session is given a unique.
So that particular decision is identified by this.
Every session data, the variables are stored on the top of the cookies and the and the data is then
encrypted with the help of a certain encryption key, which is called secret key.
So therefore, what is necessary?
If you want a certain Web application to hold certain session data, then the application configuration
must have a secret key notified or stored.
What is the decision object?
Just like a cookie object.
An object is also a dictionary object which contains the key value pairs.
Key is the name of the station variable and the data is the associated value.
So it is also a dictionary object.
So first and foremost, you have to set the secret key app provided app is the name of your application
object dark secret underscore key is equal to now.
You find that a certain string is provided or given the value to this secret.
Remember that this must be a very random and such a string which is not easily recognized or which is
not easily, easily reproducible.
So such a random string is used as secret key of the application in order to set the variable on your
to do that sation, the name of the label that you want to use.
Put it into the bracket is equal to the value.
So this will set the value.
And in order to remove the third transition variable, you just use the function called pop station
or pop.
Give the name of the variable that Flast will take, the values you put into position object and they
will be serialized in the form of cookies.
Some you know, sometimes what may happen that the sessions are not persisted throughout the session,
even if the cookies are enable the sessions, the session variable does not contain or hold to the values.
So what may be the reason?
The reason may be that your Web browser may not be supporting the length of the cookie.
That is all you have to verify that the site supported by the Web browser is matching with the size
of the cookies in your application.
If not, then there will be certain exception that may be generated.
So these are all the different activities that you will have to verify with the help of an example.
So set an example which will, you know, statistician, create a variable and read back to the station
variable that I will demonstrate to you with the help of a python example running a flask application.
So let us now try to understand how.
The session is created, how the session variables are defined, even if needed, how the session variables
are removed and how those variables remains persistent throughout the entire session.
What is decision?
The time interval for which the user is logged into a particular Web application and logged out of it?
OK, so for this exercise, different resources that are needed from our last module are important first.
So this long list of import.
You have to import the flask object, the session object, the request object and the functions redirect
you are for and render a template to, these are our requirements which we have imported.
Then, as usual, cleared the last object.
ABC called Flask Doubleheaders codename.
This will create the flask application object.
The mandatory thing is to set up a secret key of this plastic object, a secret key, you can give any
string here.
The rest of the.
Script.
Has three, you are really rules defined.
One is the.
Index, the other is a slash login and slash logout.
So to begin with, when the server starts.
And you give the localhost lost 5000, slash, this index will function, will be executed.
So, as you will expect.
In the very first place, as soon as you log into the server by this flash, you are well, there will
be no username.
That will be in the session object.
What is the object?
Incidently, it is a it is a dictionary of all the variables and its associated value.
So you are you have just started the server.
So naturally, there will be no you there will be no session variable by the name username.
So this condition will be false.
So this will not take place.
So what what will happen?
It will come into this.
And the message that will be displayed are rendered on your brother's surface will be you are not logged
in.
And it will give you a hyperlink to this slash login you are in.
And we'll ask you to click here to login.
So if you click on this login, what will happen?
This.
Browser will be redirected towards this, you are logging.
This is the second rule that has been defined here.
And additionally, the road to function.
Taking two arguments, as usual, the usual and the Metters.
Get that post.
OK, so for the first time in.
The user clicks this click here to login.
And for the first time, the Log-in, you are really stressed.
Naturally, the request not matter will not be equal to post.
It will be.
It will be a great method, so therefore this.
Rendered template session, that e-mail will.
I mean, this action will take place.
So what will happen, the browser will render that the template or the Web page for legislation or e-mail,
which you will store into.
The templates folder of your Web application.
So at this juncture, we shall have a look at what is the intersession that Stephen.
So this is the session that is the core of this session, not Estherville, in which basically there
is a small form.
In which there is an input type equal to text in which you can type the.
Text, which will be stored in the attribute called name.
And then the submit button, so if you click on the submit button, the data will be posted back to
the same.
You are ill because you have not given any other.
You are as an action attribute.
OK, so this is the.
This is our.
Not far.
So the session will be opened in the browser will be textbooks, you will enter some user name submit
the data will be now again redirected towards this login with the post maternelle.
So that means now this.
Will be executed.
This condition is true, so session, not username, is equal to whatever username that you had, input
will be faced from the request, object, request or form username.
And the social variable will be variable by the name.
Username will be set up and then your brother will be redirected towards.
The you are all for index that are getting back to this leisz.
The second time when it comes back to this, then the username is already present, so therefore.
It will be fetched, and the text that will be rendered on your webpage or browser will be logged in
as whichever username you might have given in the station.
OK, so it will now tell you that you are logged in as this.
OK.
And it will again also give you another hyperlink.
If you click on the hyperlink, it will redirect to your redirect the browser to the slash logout you
are.
OK, so what is this logout, you are doing it, we just pop the username session with evil that you
had created and.
So the media will be released and it will be again redirected towards the same index.
Now the user name is not present.
So again, back to this.
You are not logged in and.
You are given a chance by clicking the hyperlink to back to logging.
So this is a slightly, uh, complex activity that we are going to perform as a demonstration of this
example.
OK, so the Python script is there.
There are three warrants for the road logging and logging, and then, of course, there is a secondary
e-mail page.
Finally, there is a app that run in the department that we are to when the when this session is not
people will be executed, the server will start.
So this is the explanation of this.
Gore on the flask application.
So let us now try to understand how.
The session is created, how the session variables are defined, even if needed, how the session variables
are removed and how those variables remains persistent throughout the entire session.
What is decision?
The time interval for which the user is logged into a particular Web application and logged out of it?
OK, so for this exercise, different resources that are needed from our flask model are important first.
So this long list of import.
You have to import the flask object, the session object, the request object and the functions redirect
you are for and render a template.
So these are our requirements which we have imported.
Then, as usual, cleared the flask object.
ABC to Flask DeVillers codename.
This will create the flask application object.
The mandatory thing is to set up a secret key of this plastic object, a secret key, you can give any
string here.
The rest of the.
Script.
Has three, you are really rules defined.
One is the.
Index, the other is lagging and the other is logout.
So to begin with, when the server starts.
And you give the localhost column five 5000, slash this index view function will be executed.
So, as you will expect.
And the very first place, as soon as you log into the server by the slash you are in, there will be
no username.
That will be in the session object.
What is this object?
Incidentally, it is a it is a dictionary of all the social variables and its associated values.
So you are you have just started the server.
So naturally, there will be no you there will be no session variable by the name username.
So this condition will be false.
So this will not take place.
So what what will happen?
It will come into this.
And the message that will be displayed are rendered on your brother's surface will be you are not logged
in.
And it will give you a hyperlink to the slash login you are in.
And we'll ask you to click here to login.
So if you click on this login, what will happen?
This.
Browser will be redirected towards this, you are logging.
This is the second rule that has been defined here.
And additionally, the road to function.
Taking two arguments, as usual, the usual and the Metters.
Get that boost.
OK, so for the first time in.
The user clicks this click here to login.
And for the first time, the in you are really stressed.
Naturally, the request not matter will not be equal to post.
It will be.
It will be a great method, so therefore this.
Rendered template session, that e-mail will.
I mean, this action will take place.
So what will happen, the browser will render that the template or the Web page or legislation or e-mail,
which you will store into.
The templates folder of your Web application.
So at this juncture, we shall have a look at what is the intersession, that estimate.
So this is the session of the court of this session that in which basically there is a small form.
In which there is a input type is equal to text in which you can type the.
Text, which will be stored in the attribute caller's name.
And then the submit button, so if you click on the submit button, the data will be posted back to
the same.
You are ill because you have not given any other.
You are as an action attribute.
OK, so this is the this is our second not form.
So is the mill will be opened in the browser, there will be textbooks, you will enter some name,
press submit, the data will be now again redirected towards this login with the post method now.
So that means now this.
Will be executed.
This condition is true, so session user username is equal to whatever username that you had, input
will be first from the request, object, request or form username.
And the social variable will be variable by the name.
Username will be set up and then your browser will be redirected towards.
The you are all for index that are getting back to this leisz.
No second time when it comes back to this, then the username is already present, so therefore.
It will be fetched, and the text that will be rendered on your webpage or browser will be logged in
as whichever username you might have given intersession.
OK, so it will now tell you that you are logged in as this.
OK.
And it will again also give you another hyperlink.
If you click on the hyperlink, it will redirect to redirect the browser to the slash logout, you are
a little.
OK, so what is this logout you are doing, it will just pop the username session with one that you
had created and.
So the social media will be released and it will be again redirected towards the same index.
Now the user name is not present.
So again, back to this.
You are not logged in and you are given a chance by clicking the hyperlink to back to login.
So this is a slightly complex activity that we are going to perform as a demonstration of this example.
OK, so the Python script is there.
There are three warrants out the road, logging and logging, and then, of course, there is a 600
e-mail page.
Finally, there is an app that ran in the development that we are to.
When the vendors next door will be executed, the server will start.
So this is the explanation of this.
More on the flask application.