Hello, friends, in this video.
I'll explain to you how to use the flask as kill alchemy extension in our last application.
Most of the programming languages, especially the the ones that are very popular, like like Java or
even Python or dot net languages, etc., they are object oriented in nature, whereas the database
software that you use as a back end to the programs or applications written in these languages are the
data is stored in the tables and the tables are sort of flat files.
So on one hand, the programming language has class or it is object oriented and the database servers,
the data is still in the form of tables, which are nothing but flat files.
So the object lesson mapping technique.
Is used to map the object parameters of a class to the underlying RDBMS table structure.
So what do you have to do at the end of the day?
The table has the fields, which are nothing but the instance which are equivalent to the instance variables
of a class.
So therefore, to map or to correlate the instance variables of a class to the fields in a table, that
is how the object relation mapping is done.
So it is a technique of mapping the object parameters to the table structure.
In order to do that or any Aughrim API object relation mapping API provides the methods to perform the
crude operation, you operations are done on any database table.
So those database applications are operations like insert, delete, update, select, etc. These operations,
rather than doing through a raw as Google query or command insert into a are set equal to insert from
select start, etc..
All these skill statement instead of the programmer or the developer being required to execute these
Ryosuke statements, he can now use his object oriented programming knowledge to perform all these activities
using the OR MEPA.
Ezekial Alchemy is a very popular tool kit for object relational relational mapping for python related
projects or programs.
So it is the object relational mapper that presents all the mechanism for Python related database handling.
Eskil Alchemy.
Can we use, along with different database servers like it can be used with Oracle, MySQL as Kuli,
etc. flask Eskil alchemy is what it is.
An extension to the you can add it to the flask set up our environment which adds the Eskil alchemy
support to your flask application.
So this is what is an object.
Relational relation mapping and the use of flask is still alchemy will enable Aughrim into your flask
application.
So obviously, you will have to install the extension by using the.
Utility people install Gaskill Alchemy will install it.
In order to use the facilities, you are being Ezekial Alchemy class from this model.
Statemented from Laskoski, Alchemy, Import, Eskil Alchemy.
That is the mandatory step that you must do then in the application object configuration of the application
object of your application, if the name of the object is Eppy Epilog config, specify the database.
You are a ESKIL Alchemy database.
You are right is equal to which database you are going to use if you like students as to like three,
if the student is the name of the database that you are going to use, not object of this obscure alchemy
class.
Object of this Eskil Alchemy class, which you have imported, it will have all the helper functions
for performing the wartime operations.
It also provides provides what, the Eskil Alchemy class.
It also provides a model class which should be used as the parent for defining the model of our corresponding
to each table in your database.
So in order to handle or process the data in the tables, you have to define an equivalent class based
upon the model class, which refers to or which is corresponding to the table in your school database.
Each model class corresponds to the table in the database.
So, for example, if in the student's database student data is collected, there is a student stable.
What do you do?
You learn to define a class called a student, which will be based upon the baby daughter model.
That is the first create an object of alchemy class.
It requires the flask application object as the parameter that is the name of the Eskil Alchemy class
using their Demiraj model as the pattern defines the class to the students.
Is the name of the table in the.
In our database, they will find all the corresponding fields, the instance variables corresponding
to the fields in the in our table for the column name city address Pinkel.
Additionally, I have to find a primary kilson so identical to the broad column.
It is the name of the field, which is the primary.
It will be an integer.
So that string is used as the database for all these fields as you can expect.
Name city address Pincott all will strings.
So.
So this is the declaration of the table structure.
Each instance variable corresponding to the food in your table.
OK, now, if you create the object of this student's class, obviously what is going to happen is constructor
will execute the name of the constructor.
They it the constructor will provide for Parramatta's name city address in those four parameters will
be used to initialize these, for instance, variables, names, self, not names and address.
And so this will be initialize.
The object will be created.
When the object will be created, that object will be used.
For further processing, now that the model is declared, now first the table, the database is in place.
The table is there.
The model is created.
All these.
Database related activity to create or use the database mentioned in the you are right and create the
tables executed to create all function.
So this will not create all will is the table in the database and the table are not present.
They will be created.
And if they are present, they will be used, it is excellent to use a particular database kind of statement
in our Escalade or any obscure military database.
To this DBI object, there isn't such an object, bewildered session.
So this session is a lot of time to the database, all the activities like insert the record, delete
a record updated record, etc., they have to be done with the help for this session session object
submittals, what is this about and what is to be added?
What if the object of the student's class is created, the model class is created, the object will
be used as an argument to this add function.
So did not add the name of the object of the model class.
Insert the record into the map table because your model is now mapping the table in your database.
So therefore this and ultimately will add a record into the table.
Okay.
Similarly, the big decision to delete the name of the object model, what happens, the maverick label
from the maverick label, this particular object or that this particular role will be deleted from the
table, not in order to emulate the select committee, you are to set model, not query, not all in
our guest students, students in the name of the modern model, not very good at all.
Will it be equivalent to the select star from students so it will retrieve all the records from the
underlying table, in this case the underlying Kabbalist student, so it is equal to select staff from
students?
OK, if you want to apply, remember, in the in the select committee, there is a very select start
from students where models are greater than something.
So if you want to apply a filter, you can use the filter option model or filter, whichever the condition
you want.
You can apply a filter to the written record that many number of records will be treated.
Students start to filter by it.
This is the condition, not all.
So what happens from the records that are retrieved?
Only those records in which it is equal to Hyderabad will be selected.
OK.
Now, this is the form in which there is a provision to enter the deadline to for taxpayers, for example,
name, name, either having a tax free.
Then email is having a TextField City PIN code, etc., and then there is a submit button, right?
This is a simple, legible form, nothing else.
And then the submit button will take the data back to the.
Request, but from which the request was generated.
OK, so when the request is when for the first time, when the form will be open, that will be a matter
when it will be posted.
The you will be visited with the help of the post method, in which case all the data posted by this
form will be extracted by request or form to collect all that data request, not form, name, name,
city PIN code and address.
Use that data as the argument for the object of the student's class student is equal to students Euphorbia
because the object will be created.
Use that object to add the record into the underlying activity so user Demiraj should not add student.
The data will be added and then finalize or commit the transaction by saying that they should not coming.
So this is all the activity that are required to perform the insert operation equivalent insert operation.
School has an insert, so the corresponding operation in order mapper is like this, right?
And if you want to perform the select operation, then for that purpose, assure all of you function
has been defined, which will render issue all template and what it will do, it will send the parameter
as students equal to students.
Don't worry at all.
You'll see that model, dot, dot, all will retrieve all the records.
Those records will be stored in a student's parameter.
Essentially, this is a dictionary object.
So from that dictionary object on each of who perform this to render a table.
So at the end of the activity, you'll find a table being rendered on the browser with names of the
fields and the values being displayed in tablas.
So this is the entire sequence of events that, you know, is present in this interactivity, that it
will be a form.
The form will be posted to a view function.
The view function will fetch the data and perform the add or insert operation.
On the other hand, there will be equal and select operation which will find this very, very dot all
sender data to the template.
And this ginge to template code will render a table onto the surface of the browser.
So all these activities now I will show you one by one with the help of an example that will cover all
this flask, Ezekial actual alchemy features and functionality.
So with the help of this example, we shall.
You know, find out how to use the flask as Cual alchemy extension.
Along with the flask application to, you know, manipulate or use as cool as you like, database.
So first and foremost, we have to ensure that this extension flask, as alchemy is installed in the
working environment of our flask application.
So this is the virtual environment in which your flask is installed.
So now you try to install the.
Alchemy using the utility.
So I will say.
Name of the.
Extension is Flask Eskil Alchemy.
It says the government is already satisfied, that means for this extension is already present.
You can also verify this by starting a python interpreter and you try to import that, so I will start
the interpreter and right import statement.
If this this instruction is accepted, then it means that it is installed otherwise not.
There you see the.
What are the package is already present, so that is our first requirement fulfilled.
Next, we have a look at the Python code, which performs all the operations on the underlying Ezekial
database using this Ezekial Flask, Eskil Iljimae extension.
So these are our regular reports.
For this particular exercise, you want to import this Ezekial Alchemy class from this.
Lask, Eskil, Iljimae extension.
These are all the imports.
Then, of course, this is the last complication object, these two lines are the configuration of.
Your app object is appropriate config.
Ezekial Alchemy database, you are right, that means what you have to give the pot to the database
that you are going to use the name of the databases to start escalate.
Three, the protocol that you are going to use is CircuLite, Collen.
Students will ask you like three, and then you have to set the secret key of your application by saying,
I have not configured secret keys equal to this.
OK, so this is, again, the configuration of your application object.
Second step is to rather the third step is to declare an object of the actual alchemy class, which
you are imported.
It requires.
This AP as an argument, so that is cruel alchemy, class object, I call it as D.B..
Now, in this debate, there is a different model.
Now this our model is to be used as a base class for.
Declaring a.
Mappa class college students.
Which means that this inside this student's database, there will be a table called the student's.
And.
This object of this student's class will be each role in the table.
So naturally, what do you have to give the structure of that table?
Now, this DBE.
Object has a.
My terror function, collegeboard.com.
Requires, the argument says.
The data type and the size.
In the first place, the name of the.
Feel will be student student ID, it's the data that will be in danger and it is to be set as the primary
keys equal to true.
So this is the first field second release name.
They will not Collum.
It will be a string containing a string, the top 200.
Characters, the column again, it will be a string with 50 characters address up to 200 characters,
string pin up to 10 characters string.
So this is nothing but the structure of your table.
OK, now, every time when you declare an object of this student's class.
You know that whenever the object of a class is declared, the constructor goes to work and the constructor
is in it.
This constructor will be provided for arguments, each corresponding to the phrase name city address.
So using these arguments, the object will be initialized.
So later on.
There will be an object of this Lowrance class declared, which will take for argument's.
OK, so this is our modern.
So three aspects we have seen.
Import application, model, configuration and declaration of the model.
So this is our.
First step.
Of the.
Python called.
In this flask, application to your rules are defined.
One is the flash and the other is new.
I will explain this to you first and then we'll go to this less.
Slash new is map to a view function called new.
Now, this you are maybe called the application may reach this, you are new.
In two ways, one is if the user directly enters into his browser's address, bar localhost 5000 new.
In that case, the method by which this you are reached or fired will not be possible, it will be get
so therefore what will happen, this block will not be executed.
This statement will be executed and what it is doing, it is rendering a new e-mail template on the
browser.
OK, so we will have to see what the new estimate.
So this is the New Testament in which.
This is a form.
And in in the form, there are four text elements, one each for name, city address and penal code.
Isn't it and then there is a submit button.
When the user clicks after filling the form, when the user click on the submit button, it will be
posted to the same order by the meter post.
By the metal post.
So what happens when?
The form is posted there, or rather what happens when this when this new URL is reached with the post
ometer?
So let us go back.
So in this case now, when, again, this new you are fired, now the matter is supposed so, therefore
this block of statements will be executed.
Isn't it?
So if the matter is paused, but the form that has been posted containing four former elements name,
city address and beancurd, any of those fields.
Are empty.
Then obviously those that should not be added, so therefore what what what is to be done, a message
is to be flashed, an error message.
Please enter all the names.
And this flash function will generate a flash message which will be displayed on the browser when the
next to you are released accessed.
OK, but if this this form is properly filled, all the officials do have a certain value, then the
else block will be executed here.
The object of the student's class is created with four attributes or four parameters.
Extracted from the request, not from object, the or form names addressed in court.
This will be the values that will be released that will be corresponding to the estimable form data
using or taking these values.
This object is created.
Now, this object is to be added into.
The database that is right now in the session so deeply, not not at all what the student object.
If it is properly executed, commit the operations by saying they will not commit and then the message
to be flashed record, the message to be flashed as record was successfully ended.
So either this message has to be flashed or this has to be flashed.
Well, when this when your browser is redirected towards another, you are sure all.
OK, so this is the activity, which is then.
When?
The user clicks on.
The link, which takes it to this, uh.
Insertion of new record activities done.
Incidentally, what happens to the flash messages if the record if the data is not properly filled,
that means if this condition is.
Through.
When will be either name or your address, it will not be entered, then this is the message to be flashed.
As you know, the flash will be other messages will be flashed when the next.
You are in our next review function is executed.
So after this flash, what is going to happen?
This is not going to take place.
The blog.
So the control of the program will come to this.
So the in the New Testament, if the is improperly filled, the form must be shown again, but with
an error message.
So if you remember, if you will have a look at the will again.
So this is the.
Template to template code, which will take care of the messages to be fleshed, extract the messages
by using the get flashed the messages function and display the.
They called each function at a time and there will be only one.
Indicating that there is an error in the form A..
Right.
So this is the.
Explanation for this part of your new rotation, so that takes us to this as look what happens in the
health law when the data is properly filled up.
What is going to happen, this object will be created and the record will be added.
And the browser will be redirected towards the Schwall, sure all.
The you are all for sure, all function.
What is the you are in virtual function?
This is the real virtual function, it is going to render the show all that e-mail.
And it is going to take an attribute which is a result of this query, students not thrilled at all.
All the records in the student's table will form a record set object.
Which will be a value of this student sacrebleu.
OK, so.
Either by directly entering into the browser, localhost 5000 slash.
This all e-mail will be rendered after completing or after performing the insert operation.
After adding the record again, this sure all you are, which is against your or not e-mail will be
displayed.
So what does that insure?
All that has to be.
This is the extreme end quote for sure, all the e-mail.
At the top, there is a there is, again, a template called.
Which will.
Rather this much.
If there are if at all, there are any messages and there will be this message will be flashed when
this show, all that estimate is going to be rendered after the successful completion of the insert
operation.
What is that, the record of a successful year that will be dispelled by this?
Now, the rest of the team called.
Is displaying a table.
This is the table.
Out of the first row is the.
Hitting.
Name, address.
This will be appearing in the first row in the as if they are the.
Fewer names on the titles of the table and.
You know that the sure, all the testimony is going to be rendered with one parameter, which is the.
In a record, the results of the query all function.
They're not really all function.
OK, so the student is it?
List of all records.
This is a list object of all the girls take one record at a time and.
All the.
Instance, variables of the student objects, not name, city address and pin code are displayed in.
One group, one student object, that data is displayed in one room.
Such like all the rooms will be integrated and it will complete a table of all the records in the student
table.
So this is the explanation of shouldered ASTM.
So this student killer database is already present in our application folder.
I am now showing you the calculator database using deescalates Tullio.
And in this database, there is a table called restaurants, and this is the structure of the student.
Remember, we have given this lecture in our students class.
There are this is a primary key and these are the four.
You know his name, address and phone call.
Believe it or not, string is corresponding to or is being mapped to the worker sites.
This was the site that was given.
OK, and of which this student ID is the primary key that also we had given in our declaration of this
student's class.
Right, and right now, these phone records are already present in this.
Not our intention is to just to verify whatever operation that we perform on this rescue database through
our python code, whether they are, you can just verify that the operations are exactly stored and
reflected in this year's kilojoule because they just show you that if the record is error, that should
be visible here.
And if the records are listed, those are listed on the list.
Exactly.
Should match with this list.
That is the intention of showing you this close to you.
Now, let us start our application brought from the command line.
The server is running, there are some warnings with relation to the.
The configuration of this flask, Ezekial Alchemy, but then you can ignore, uh, the subtle reasoning.
So what we have to do now to go to the browser and give this you are localhost five thousand slash.
So I entered this, you are in.
Is giving the present list of records, and it is also giving me a link to Astroland, so it will take
me to localhost 5000 new.
So if I click on this new.
There is a form.
In this form.
Something like this.
And then submit.
There you see the list is now updated, the fifth record Israel recorded successfully.
This is the message that was flashed.
So if I click on this ad student again.
This will be, again, their eye without entering anything, I just submit it will prompt me, please
enter all the so all the operations are now, you know, verified with the help of this.
Let me go back to this rescue slue and see whether the newly added record is happening in that or not.
So this is, again, the studio interface students database student stable.
The fifth record added is being displayed here.
So our insert operation is successful and the list is also properly being displayed on the browser.
With the help of all this, we are done with the help of the Flask Eskil Alchemy Extension.