Firebase Python FULL COURSE: Authentication, Storage, Realtime Database CRUD tutorials [pyrebase]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome to a brand new video so this video is going to be a in-depth and complete python firebase course so it will follow a series of firebase features starting with authentication so logging in creating new accounts different types of user management as well as storage so you'll be able to store and upload files to the internet as well as download them read from them and store them in these different locations and finally and most importantly we're going to access the firebase real-time database so the real-time database we're going to create some data within the database work with it create it read it update it and delete it so the different cred applications now the library will be using is the firebase library so it's a very popular library for the firebase api with python so it is very popular so we're going to explore every single aspect of it and then just really get to know everything that's in it so without further ado let's just get started so the first thing we're going to start with is the authentication part so we're going to start with authentication and then move on to something else so just let's just get started so first of all we must go to console.firebase.google.com so this is where all your firebase projects are stored you can access your different firebase projects these are different dummy projects you could say from different tutorials i've done over the time of my channel now for this tutorial we're going to create a new project so we're just going to add project and give it a name so the name we're going to give the project let's just say firebase course and then we're going to continue we can enable google analytics this is no issue but we will not be using them for this project now we can create the project and wait for it to load now while we wait for the project to load we can go back to pycharm so this is an empty project with pytron i just created it empty nothing inside and the first thing we're going to do is actually install the library that we're going to be using so if we go to the terminal all we have to do is fifth install firebase 4 now this is the library we're going to be using now you may have heard of simply firebase so just firebase or firebase 4 now the truth is that there is pretty much no difference let me just answer here i already have it so a requirement already satisfied however in your case you would wait a couple minutes for firebase to be downloaded so let's just talk about the difference between firebase and firebase four so let's get that out of the way so that there's no confusion so firebase four is essentially a github fork of the original firebase library now the thing about the original library was that it stopped being maintained and some errors started showing up and no one was there to actually fix them so someone forked it created firebase 4 and fixed all the errors while leaving the code the exact same so that nothing will differ in your code so if you're choosing to move from firebase to firebase 4 and you already have existing firebase code with that library there's no problem because the code syntax is the exact same so you can migrate by doing nothing other than uninstalling firebase and installing firebase for so that's just a note keep that in mind if you're interested in why or what the difference is between these two libraries all right so we just installed firebase now going back to our firebase console so our project is now ready so we continue and this should take us to our project overview or our project dashboard now this is where we have access to a variety of firebase features namely authentication a cloud firestore which is another type of database aside from real-time database we have analytics for crashing we have performance analytics we have testing we have cloud messaging so which you can use this for the push notifications that you send to your applications now for python for this application for firebase specifically so firebase only supports the authentication the database as well as the the cloud storage so this is what we'll be exploring throughout this course now so let's just actually understand what we have here so like i said project overview here we have the features so authentication database storage hosting and a sort of a bunch of other things all right now let's go back to pycharm and actually see what we have so going back to python let's create a new empty and python file so let's just name this main.pi or any other name if you want so we're gonna start off by importing firebase so you would import firebase actually and not firebase four and that's what i meant when i said the code is very easy to migrate from one instance of this library to the other because the code is the exact same so now that we've imported firebase here's one thing we have to do we have to add this application to this app so we want to add the firebase project to our application we want to initialize a new application to do so we have to go back to the console go back to our project and here we have get started by adding firebase or firebase i'm sorry to your app so you have ios you have android you even have unity and you have web so for python desktop applications you're best off using web so you would go to web you would give your app a nickname so firebase course you would register your app there's no need to set up firebase hosting so as you wait for it to register a bunch of credentials are going to show up so these and for this information right here is what you'll be using to connect to this application so you take this part and you copy it now you can continue to console there's no issue so you're back here now going back to pycharm you want to be able to connect to this application so you just firebase config and then you create a new dict and you paste this information right here so what this dig does is that it's it basically stores your configuration credentials for firebase so this is what connects you to this application and this is what enables you to be able to talk this to this firebase on the cloud and then just perform all sorts of operations with now one thing to keep in mind is that for python you must change these into strings because it will not understand them and will show you an error otherwise because it will consider them as variables so this is just some python dict syntax for you now if you're not familiar with python dicks so this is basically what they are they're a series of key value pairs where the keys and the values uh where the keys specifically i mean are strings and the values can be anything but from a string to an end to a boolean all right so this is a python dict with our firebase configuration credentials now to initialize an app so all we have to do is firebase and say it's equal to firebase dot initialize app and you would give it the firebase config variable now by giving it this variable you're telling that i want to initialize an app with these configuration credentials with this information and by doing so you're able to initialize an app to that specific instance to this project that you just created all right now here's the thing so with fire with firebase to connect to the database all you have to do is just db or whatever variable name you want and say firebase dot database or off and it's firebase.org now there's no need to be worried about this right now because i'm going to explain every single one of these features in depth in this video so this is how you basically initialize something that will talk to each and every one of these now we said we're starting with auth so if you want we can comment these out for future reference or we can probably leave them and just not use them so here we have the authentication now now that i'm connected to authentication let's just start exploring it through firebase so i can tell you exactly what it is how we're doing it and what sort of things are related to it so going back here now this is our project overview we want to go to authentication so let's go to authentication so by authentication we mean signing in signing up and all that so managing users and the way we manage users is usually when you have an application you would either do it through an email and a password so you obviously know this because you live on the internet and you know exactly how the internet works so you either use an email any password or maybe you sign in with facebook or sign in with google or whatever so now we're going to set up a sign in method so by going here and we have a different bunch of things that we can enable so we can use firebase to sign up with now this is really interesting because you can sign in with google facebook twitter github play games microsoft apple you can even sign in anonymously now for this tutorial for firebase we're going to stress on email and password sign in however in a future video perhaps i can have a more detailed authentication video on every single one of these different features so the first thing we have to do is we have to enable the email and password sign in so this way we'll be able to have users using their email and password so going back to users so this is where it sort of looks like so this is sort of like a table though you can't really see it with a bunch of columns so here you would have different users and the last time they signed in when the account was created you can add users manually so here i can create a dummy so dummy mail.com and i can give it a password of one two three four five six seven eight and then i can add a user by doing so i just manually created a user so this user was created on july 23rd it has this sort of email that identifies it providers meaning with what did i use to sign in so i use with email not with google or facebook or anything else as well as a user id now we signed in with not so we just created this user into this authentication database you could say that's why there exists nothing here concerning the sign in part now okay so we know how to create it manually how helpful is that you're not going to go ahead and create users manually for every single application that you own so you want users to be able to do so to the code itself so let's just code that let's do it so for this application for this project we're going to do everything through the command line just to keep it simple because i don't want to i don't want to impose a certain gui library on you so not a certain gui module so i want everyone to be able to access and use this project without having to download let's say qivi or pyqt5 or anything else to make a graphical user interface so we'll do it through the command line all right so here's what we have to do what we're going to do is that we're going to ask our user to sign in with email and password so here's what we're going to do so we're going to say email and we're going to ask for input and say enter your email and then password and we're going to ask for input and then we're going to enter your password all right and we can have this as part of the login so let's just comment it so we can keep everything separated and we can even separate this from everything else by saying this is the authentication part of this tutorial so we ask for the email and password so what we need to do now since we have an off variable here and this is connected to the firebase auth like we said right so all we need to do in this case is off dot sign in with email and password and you give it the email variable and you give it the password variable and you're done so this is how you do the authentication sign in with email and password now if you want to try it out here's what you can do so let's just run this and run main all right so enter your email so let's just try to sign into the email we just created manually in the database so let's say dummy at mail.com so that's what i believe we did now enter your password so one two three four five six seven eight now we enter and nothing happened so i should have put a print statement after it to say successfully logged in but we pretty much just managed to sign in now if you want to check we can go back here to the table we can reload by using this refresh button and it says that we signed in july 23rd 2020 which is today so if you check my calendar right here it's july 23rd 2020. so this is how we managed to sign in from the python code now what we can do is that we can add a print statement here to add successfully signed in now if we run it again so let's just do the same thing so dummy dummy mail.com and then one two three four five six seven eight so successfully signed in now what would happen if i just wrote an email that doesn't exist or perhaps a wrong password so let's just say fake mail at mail.com and then pretty much any password and then i enter i get an exception and this exception says email not found so this is the message otherwise if i typed in a wrong password so let's just say at mail.com but i wrote in a wrong password now i enter now the here the message is invalid password so in all these cases it's throwing an exception now the way we deal with exceptions with python is that we try and accept so let's just try and tap this here and then we're going to accept and when we accept we're going to use this exception to just print invalid user or password try again all right so now that we have this now if we run it and then i also try to sign in with a fake password so dummy.com and then i enter invalid user or password try again so this way we managed to actually catch the exceptions in case the login part is wrong so that's pretty much it for the login part now what we're going to do is we're going to do these sign up let's just move this so we're going to do the sign up part now sign up it's pretty much essentially the same so we're going to get the email and password from the user we can even ask to confirm password if we want to be creative so let's just say input confirm password so if you just want to be um and just make things interesting and then all we have to do is we have to off the sign i'm sorry create user with email and password so email comma password all right but i want to check if the password matches the confirm pass so we're just saying we're making sure the two strings are alive and then i would create a new user so let's just run it and then um let's just actually comment this part out right now okay so now that we've commented the login part so that doesn't bother us we can run it and then so we enter our email so let's just say fakemail.mail.com and then we enter a new password so uh a s b m c a k l whatever then we have to confirm this password and then nothing happened so obviously we should have also issued a print statement but then if we go back here and we reload here we go we have a new um we have a new user created with a new email and it was created on july 23rd and this considers it a sign in using july 23rd as well so now we just created this account so that's pretty much what we have the same thing could happen here is that a username could probably also exist now for the idea of password and confirm password not matching each other we already validated it otherwise we just want to validate the fact that a user already exists so let's just do that so what we're going to do is that we're going to try and then we're going to print success and then we're going to accept it by saying print username already exists or let's say email already exists so email and now if we run it so we enter our email so let's just say dummy at mail.com and pretty much like any password and then password email already exists so now we cannot sign in with this email because we have already created an account with it so we cannot create a new account so that's pretty much it for logging in and signing up so it is that's all it takes to be able to create an account with an email and a password so everything else is up to you to have these sort of tips and tricks to validate things to try and accept things and make sure that everything's working and you're not allowing the user to throw any exceptions and make your app crash so with what we did we sort of did not allow any exceptions in an app scenario you would here left the user um you would let the application issue an error message in case your user already exists or something isn't valid so that's pretty much it for authentication you now know how to access these you know how to create one of these how to sign in how to just validate things check if an account or an email already exists so you're pretty much good to go now the next thing we're going to do is that we're going to move on from authentication and actually start with storage so let's just move to storage right here okay so now let's start with the storage part now the first thing we're going to do is actually we're going to comment the auth part out and we're going to take the storage part and leave it here so we're saying storage is firebase dot source so the same thing that as we did with the authentication part we allowed the this variable called storage to actually interact with the firebase storage on the cloud now let's talk about the storage what it is how it works and then we can start coding so going back to our firebase console to our firebase project so this is storage what storage necessarily is that it's a place on the cloud for you to store your users data and by by your users data i mean user files like not just data for databases but actual genuine files like images audio video document pdfs word documents xml documents you can use it for pretty much anything so if you have an app such as instagram and you want your users to be uploading images the whole time you have to store it somewhere right now where that place is going to be so where that place is going to be is that it's basically a cloud area for your firebase project from google cloud itself where you can store these different files now to get started let's just get started we have to just have some rules right here so now we'll just say done and we create a bucket so while we wait for this bucket to create so let's talk more about the storage so by using storage you're allowing your users essentially to upload information to the storage without knowing about it so they're uploading their photos to this storage without knowing that they are because your code is doing it in the background and then to view these photos to view these files they're actually also downloading from the storage or reading from the storage and that's what we're going to do in this tutorial right here so this is our job in this tutorial so this is our storage all right so it looks pretty much similar to authentication except this part is for storing files now you can choose to upload files manually you can create folders so here i can create a folder called subfolder and then it will be a folder within my main storage folder so it's similar to a sort of google drive but for your apps if you want to think of it that way so let's go back to rules first before we can actually start working with it so for rules you have to specify who is allowed to read and write to this so let's just say true because this is just testing and want to be able to apply and upload these different things i'm sorry if true i want to be able to upload and read files without any problems without even being authenticated so this is our storage now we want to upload files to here i want to upload them through through these different um through these different folders so how we're going to do so is through the following now here's what we're going to do so let's just go back to the code and start working now here's what we have we have three different sample files right here so these files are first a dummy text file so lauren ipsum dummy text the next one is a shakespearean poem and it's a snippet from a shakespearean poem and it's stored in a text file called shakespeare and the last one is the story of google so this is how google was initiated as a company and it's also stored in a text file so we have these files and this is our python code so this is where we're working now let's just say storage this is the storage part of the course so here's what we're going to do what we're going to do is that we want to upload one of these different files to the internet to the storage in our cloud storage in firebase so how do we do it how do we do it first we ask the user so we have to say file name equal import and we say enter the name of the file you want to upload and then what you want to do as well so cloud file name is also input sorry um enter the name of the file on the cloud now the difference between these two things i'll explain it any minute and the last thing you want to do is that you just want to say storage dot child cloud file name dot put and you would say file so what is going on here let's just explain it so by asking the file name i'm asking the user to tell me which file or i'm sorry here that means but finally so i'm asking the user which file should i upload to storage so if i say i want to upload shakespeare.txt then i'm going to take the shakespeare.txt file and upload this to storage what cloud file name is is that what i want to name it in the storage so here it could be called shakespeare but in storage i could choose to call it abcd or i can give it a certain code word name which i'll later utilize for something else so this is pretty much how the dynamic of things sort of work here so keep that in mind let's just run it and see what's going on as for the storage.child part so here we're saying so storage dot child so this is the cloud file name and we're going to put this file there now if that's not pretty clear to you it's okay because when we demo it this will all be clear and i'll re-explain it again so running it now so enter the name of the file you want to upload so i'm just gonna say shakespeare.txt all right now i enter enter the name of the file in the cloud so i'm just gonna say books slash poems slash poem number one dot txt and now i'm going to enter process finish so what just happened why did i create this sort of path right here let's go back to storage check things out and see what really happened using this line of code right here so going back to storage now we want to refresh so that we can actually see what just got updated by running this last code instance that we ran so waiting for it to come back here it is so we manually created subfolder earlier however books we never created it manually yet here it is because when i wrote this here i created a books slash poems slash poem one dot txt path so this path gets automatically created even if one of these files or folders does not genuinely exist in the storage so books was created opening it poems was created as well and opening it as well we have poem.1.txt now we can choose to open it right here if we open it we have a long url url right here which stores this poem on the cloud and there the poem is any text file on the cloud in the cloud storage so so that's basically basically how you put a file to the storage now you can also choose to put a file without a subfolder so running it we can say we want to upload lauren ipsum.txt now what do i want to name it i just want to name it dummy text dot txt with no path specified so process finish meaning if we go back to the original part here we go we have dummy text.txt it's outside the two different subfolders that already exist and i can access it and there it is on the cloud with having its own unique url alright so that's pretty much how you do it now you know how to put a file that's literally it you can also choose to put an image file a pdf a video an audio file it's all essentially the same now if you want to get this url that we just posted you can actually print it out so print storage dot child and you can specify the url so cloud file name dot get url and you would have to put a non type here so running it we can try with story of google now so story of google.txt and then i want to enter the name of the file on the cloud so let's say google.txt and if we print it here we go we have this url right here which is the url of the file on the cloud now coming back here if we refresh we can see google.txt was created and we have this text file with the story of google online on the cloud so that's pretty much how you just put things you can also get url just like i just taught you too so what's next so we know how to put a file on the cloud how do we actually get it so i want to download it how do you let me download it dot child cloud file name dot download now we have to specify a file a path to where we're saving it so if i want to save it in this current directory i just specify an empty string and then i specify a name so i can say downloaded.txt all right so this is how i download so what's going on here essentially is that i'm taking this cloud file name so let's say for example books slash poems poem.txt and then downloading it into this path into this file so let's run it and demo it and see what's going on so as we can see here we don't have a demo text file uh download text files so enter name of the file you want to download so if i want to say books slash poems slash pawn1.txt and process is finished we have a new downloaded.txt file it contains the same poem as the shakespeare file so we just managed to basically download it now that's pretty much all there is to it so this is how you would download a file so let's just say download and here you can say upload so we learned how to download a file that we already uploaded in the storage so we can also say for example by changing this we can comment this out we can say here i want to download google.txt which i already have in the storage as google.txt now if i run it it's supposed to essentially directly download the google file so going back to downloaded now we have the story of sorry the story of google within this downloaded text file another thing we can do is that we can actually read from the file when we know that it's actually a document so a text file with a string so since we already managed to get the url like right here so let's just find that so storage dot get url so we can say url is equal to this and cloud file name is essentially the same thing we're asking the user to give us the cloud file name so let's just say this is reading and we just want to be able to read or print the strings directly into the command line without downloading it to an external text file so you do this the same way you do it with standard python so you are a lib dot request i must import url lib all right dot request dot url open and then we open it to url dot read and then we want to just print f because f will obviously contain a string so running it enter the name of the file that you want to download so we want to download say uh google.txt and here we go here's the story of google begins and this is where the entire story is printed to us as a string from the same text file by reading it from a url so this is the same as reading any text text online files just xml json or even txt file from the internet using the url lib so if you know standard python you're probably familiar with this so that's pretty much it for the storage part of this tutorial now you know exactly what you need about storage and everything you need to be able to work with files upload them download them and just manage them in general so here we go now we're done the next part is the database part okay so this is the last part of this video however it is a bigger part mainly because of the different aspects that come with a database mainly the crowd features so create read update and delete so the first thing we're going to do is actually create a database however we do not want to work with cloud firestore so the thing about firebase is that it provides two different types of databases the real-time database as well as the cloud firestore now there are a series of differences between these two databases and it would be too long to actually highlight these in this video now i already have an existing video mainly tackling this topic which is how to choose between cloud firestore versus real-time database in which i highlight the differences as well as the similarities of these two very powerful databases however for this video we're going to stick to the real-time database mainly because the firebase library only supports real-time database it does not support cloud firestore for cloud firestore support in the python language you'll have to go and use a different python library to access the cloud firestore which should not appear to be a problem for you because there you can use multiple firebase libraries within the same application and still manage to get the job done and trust me i know because i've done that for a project in which i use two different libraries one the normal firebase api as well as the firebase library within the same project anyways without further ado let's get started so let's just actually create the database so we scroll down to real-time database and we create the database so we want to start in test mode meaning read and write are true so anyone can be able to write and read data from this database meaning you who you have my credentials my api credentials my configuration part from the beginning of this whole video so from this very start when we actually started with authentication you can use those credentials and actually access my data and read and write to it so my project will be shut down after this video is up however i'm letting you know that's pretty much how it works when you're in test mode rather than locked mode so let's keep it in test mode for now and enable it and now we wait for the real-time database to be created so now we have a real-time database this is our database and this is pretty much what we have so what this is basically is a huge json3 so this is the root node you could say so this is a url mentioning the root node now if i want to create a node i can create one right here and say name and just say john and then if i add it now i have a child node for this root node and it follows this sort of json format in which things are key value pairs so like python dict or json which maybe you're familiar with json if you are then this is definitely a breeze for you so json is key value pairs and these key value pairs would be comma separated and etc etc so i have a video on json if you're interested however i don't think you need to be just to be able to understand what's going on in this video anyway so let's continue so i can create something else here so i can delve deeper into this hierarchical format by creating the following so users and then i can create a child node and then name this child named john and for john i can create an age and say that he's 32. now if i add this now i have like i can delete this original name john now i have this root node and a child node another child node another child node in which i have the information so that's pretty much how it works you just create these different children to other things and then the leaf nodes would be actual data actual information such as maybe an age and could have an int or a string so an h could actually be that he's old and that's a string or it could be a boolean so maybe not age maybe something else where i could say that whether he's employed or not and i could name that as true and i can save strings ins booleans dates and all sorts of different formats so there are a series of data types for more on data types i have a video however not a necessity for you to watch it anyways so let's just get started so now that we know how the data is structured we can actually go about actually working with it from the code so what i'm going to do is i'm going to delete everything except the root node and just start fresh start from scratch and see how this works with the code so for now we're just going to start with creating data for this database it's obviously empty since the root node has a null value now going back to the code now we can actually get started so the first thing we want to do is that we have to just enable this again so let's just remove the comment and comment the storage part out so now we have db is firebase.database and this is what helps us actually access this real-time database and like i said firebase does not provide this cloud firestore support so it only supports real-time db now scrolling back down we want to actually start by creating our data now the way this data works is that we want to let's say upload different data about different people so let's just say that the data would be a person's you could say age so we could say that this person's age is 40 and then there um however you should have always strings for your key value pairs in python so like i said at the start of this video if you leave this without a string then you would necessarily be attempting to call in a variable with python so then you can say address and then you can specify what this person's address would be so let's say new york and you can specify an occupation or actually let's just say employed and specify this as true and then you can give that person a name and i'm just going to name this person john smith so this is my early data that i have now to create data into the database what i need to do is i have to db.push it so db dot push data and that's pretty much all there is to it to actually create this data so let's just run it and wait for everything to happen so now that it's done i can go back and check here i go so this is a real-time database meaning that it syncs in real time there's no need to ever refresh anything so here we go this is the real-time database we have a new object which is a child node of the root node created and it has this different information in it so how does this work so when i use the push function when i use the push function it essentially creates a new child mode of the root node that i specified so i said db.push right so it essentially pushed it right into db into the root node db necessarily represents the root node so i pushed right into the root node a new child node with a randomly generated id that is somewhat dependent on the current timestamp so firebase does this for you it's a unique id and then for this id i push a bunch of new objects and a bunch of items these items are the key value pairs which actually represent a single json object with different sorts of information now i could have pushed this to a different part so i could say db dot child and i would say people or employees or whatever i want to call my other node and then if i run this and i wait for it to be done if i go back to the database i can see that people is a child node of this root node and within it i can save the different people it is good practice to have another node within your root node that contains everything so let's just delete this one and actually follow that sort of path but now that you know the difference between db dot versus db.child so db dot represents essentially the root node db.child means root node dot child people dot push data and you can have infinite dot child functions right here creating more paths so a person and then you can push the data so you can have dotchild.child.child creating these different paths for yourself and within these paths you'll be able to travel within this tree in the real-time database so going back here we can see people that's just minimizes so it's db dot child people dot child person and we pushed here meaning an id was created with the data within it so now that you understand how this works so you should probably understand how this works how the push and the paths work for this we can delete this and just focus on the previous format of following the people and pushing items inside it so now that you know how to do this you can have different combinations of the data so you can change let's say age 32 address to la and then you could say employed is false and then just name this person mark and then just run this and be pushing more and more data into uh sorry i uploaded it into child so let's just keep it inside people so it can all match anyways what i'm saying essentially is that you can have all sorts of combinations for your data you can prompt the user so you would tell the user to enter your age enter your address enter your employment enter your name and then you would push all that data formulate it into a dictionary and push it to the database to the required location you want using db.child.people.push so going back now this was obviously an accident so i'm just going to delete the person part and this is what we have so now we have two different people each one has their own unique id and then with the information within it now what would happen if i do not want this unique id i want my own id so here's what i have to do i change push to set and i have to create another child here so child dot set so what is this last child i've created this child is essentially stating that i want to be inside this path so i want to be inside root node dot people but the next node i'm going to follow is not going to be a randomly generated id it's going to be my own id which i'm going to set through this child function so i'm just going to say my own id and i'm going to set this data there so let me just run it and here we go so going back here we have my own id within people and it has this information about the smart guy so now you should understand the difference between allowing the push function to create this own id for your data versus you specifying it with the sat function so when you say set you're saying i want all this information to just be directly put under the last node i specified which is here i've named it my own id it could be pretty much anything it could be just a random set of characters and this set of characters should represent your id so by running it if you go back now you have another user and this is your user and this is the id with the children part of it which are the different information within this node so like i said now you know the difference between push and set and these are the mainly two ways for you to create these um to create your data in the firebase real-time database so now we just finished the first letter from the crud part so now we know cred we know that this is how we create our data next thing we're going to do is actually skip over the r for now do the update then do the delete and finally do the read data since it's the largest and bulkiest part so stay tuned for that so now it's time for the update part so i want to be able to update this data that i just created in my database so let's assume now i want to go inside this one so knowing the id is asdfghk so jk so this is where i want to update the name to mark from mark to um jane so how do i do that so how do i change the name from mark to jane so here's what i have to do i also have to follow a path using the db.child functions so i'm just going to say dblockchild people we said dot child and here it's going to be this id which is what we specified to be the id parent of this name mark right here and then we say we want that we want the to update and then we specify name to um jane alright so now i can run it saying that i want to update name to jane and then i'll explain it in a minute so running it as we can see jane here this one mark was here and it was updated into jane now how does this work let me just explain this real quick so like i said we create paths using the child function so we already know how to do this it's the same as the create part now obviously let me comment this out so it doesn't bother us okay so now that we have the update we just decided by having an input document right here or an input json or an input python dict and this input here this argument for this method it tells us that we want the name part the key here to be this value jane now what if i have said names what would happen in this case so let me just run it and show you what happens going back now we have names so it's a new one with the value jane so here's what happens this update function it checks whether this field or this key already exists so here we have these different keys which are address age i mean employed name and we do not have names originally it checks if it exists so when it existed in the case of name it just simply converts it to jane because here we're setting it as jane however if it does not exist as in the case with names it just creates a new field and specifies this value so it goes from being null to actually having a value so that's pretty much how update works now what's one thing that's concerning with this the one thing that's concerning in this case is the fact that this id here was known like we specified this so the coder here the coder actually knows each user's different id now what would happen if i want to access this one but i do not know exactly what this id is now how do i do that so here's what i'm going to do so what i'm going to do is that i'm going to just delete these and just assume i only have ids that i do not know so i'm just going to remove this since we have the same data so now i have these two different person nodes and now i'm going to go back to my code and then do this without the id so here's how i'm going to do it let's just say i want to put it in a variable so people and i say db.child people dot get so essentially what this function does is that it's taking all the people inside db.child people and storing them into one variable called get now to actually iterate over these to actually understand what they are we cannot simply just iterate over entirety what we need to do is we have to say four person in people or for i in people it doesn't really matter dot each so to be able to iterate you have to use the dot each function and now for this you can print person dot val and print person dot key now why i'm doing this is that i want you to understand what information we have available at hand now and how we can use it to actually get the certain person that we want to update mark iv so running it let's just print it so we have this value here so address new york ages 40 employed his true name john smith and this is their id and i also have this value here and this is their id so going back we can actually change it to this is the exact same information this is the information in the val part of this function and this is the information in the key part of this function now what i'm interested in is actually getting the key so how do i get this key i want to extract the key that is used for mark so i can use it to update the name of mark into something new so how i'm going to do it is i'm just going to specify that if person.val sub name because essentially this is a python dick so let's just go back here so this is a python dict meaning i can access these different key values by using the key in the subpart of this uh statement so by saying person.val sub name i'm saying i want to access the name here either john smith or mark and i want to say that if this name is equal to mark i want to actually update so how do i update so what i have to say is db dot child people dot child person dot key and then i specify dot update and i give it name should be jing now i'll explain the code in a minute let me just run it and actually show you how it works so going back we can see that this name is now jane so here's what we did so we went to through the different people so we extracted first the different people notes so for each person we have a val and a key the vowel being all this information within a person and the key being this id now for the information within a person i managed to extract it and from that i managed to extract the name and i compared so when the name is mark i asked it to actually update this name into jane and the way i asked it is that i created a path the way we just did it before but instead of the key being known the key is actually stored within person.key so because this person actually has the name of mark i want their key so i extract their id i perform the path and then through this path i actually perform the update so that's pretty much it that's pretty much how you do the update part and how you can actually do it so like i said it's pretty straightforward it's pretty easy that's all you need to know now you know how to update in two different cases where you know the id versus when you actually don't so that's pretty much it for update the next part we're going to do is the delete data so let me just comment this part out and get ready for deleting data with firebase okay so now comes the time for the delete part now the reason i'm doing delete and update together is that they hold a lot of similarities in the way that with the needs as well there are also two types of deletions you can perform one with a known id and one with an unknown and randomly generated id so first let's start with the known id so let's just go back and create some manual data right here and then inside people we're just going to create a person and then we're going to give this person some information so let's say age is going to be 34 and then we're going to have a name for them and that name is going to be mary and then we're going to have an address and it's going to have london and we do not necessarily need to provide an employment as well so it has it can be pretty much anything mainly because firebase is a no sql database meaning the different person nodes here the different objects within this database do not necessarily have to have the same schema or necessarily have to have the same fields or keys in a sense so we can add this and now that we have this we know the direct you could say path to access the h right here so let's say i want to remove mary's h how do i remove her age all i have to do is db dot child people and then dot child person dot child age dot remove pretty simple pretty straightforward now if i run it i wait for it to happen i go back and the age part is suddenly removed so that's all you need to do you have to in the case of a known id you just have to specify directly the id of the things that you want to delete you just go by it one by one and you specify this is the path i want and you i want you to delete the final note i have now what if i want to delete this entire person node so you can as well delete an entire node with all of its children so removing this child age here and let's just say just remove and if i run this it should delete the entire person node as well as all of its children you obviously cannot delete a node without deleting its children so let me just run it and now it's done we can go back person is gone as well as all of its nodes so now you know how to delete something with a known id how do you donate something with a generated key so to delete a node they generated key you follow pretty much the same logic that we used in the update part so we also have to just let me just actually copy this part right here and i'll tell you what we're going to change so you also have to store all the people you want in a variable and then for person in this people you have to specify let's say i want to delete the one for john smith i want to delete john smith's age so i want to delete this guy's age i want to remove his age here 40. i want to delete it how do i do so so like i said you check whether this name is john smith now what happens if it is john smith in the sense what do i do so i just db.child people dot child and i get john smith's id so person dot key and then [Music] dot child age dot remove now that i'm done i can run it and here i go so like i like i said the age for john smith is definitely deleted so that's how you actually delete for both cases when you have the known key versus when you have the unknown or generated key that's pretty much it for delete there is pretty much nothing else you can do in a sense so now we're going to move on to the read part which is the last part of this tutorial of this entire course and through this part we're going to be able to actually understand how the read works as well as perform some complex more advanced queries on the data so stay tuned for that okay so now comes the last part the read data part so to read data we actually have been sort of reading data throughout this tutorial so when we're working with update and delete we were doing the following we were saying that people is equal to db.child people dot get and through this i'm storing all these people and i'm able to access their keys and values using the key and the valve functions by iterating over each so now that i know that this is pretty much all i need to be able to retrieve data everything else is simply forming paths to be able to get this data so if i want to get this let's say if i want to get this user right here so i actually went ahead and can just copy this i went ahead and created a third person just so we can have more flexibility in terms of our queries and i can say dot child and i specify the id here dot get i can print the actual value of this now if i run it i get an order predict with this information for this specific person because i pasted the id so you know pretty much how to work with these paths now what we actually want is that we want to be able to create some conditions on this data to be able to access it and filter it accordingly so i want all the people whose age is greater than three i want all the people whose age whose employment is actually true i want all the people whose address is in la so that's how we're going to work with it now the way we're going to do it is that we have to actually work on these different fields or these different columns to do so we have to index these columns first so let's actually go back to the firebase and actually index these columns to index the columns we have to go to the rules going back to the rules here's what we have to do now the reason we have to go to rules is basically we have to define some indexes on the field names or the keys that we're going to be performing these conditions so what do i mean by this by performing these indexes by creating them we index on these names so let's just go back and let me show you the data so this is more clear to you however you don't really need to understand the entirety of things but it wouldn't hurt to truly get what's going on here so these names right here so address age employed name these represent these sorts of fields or this information about the data within people right so we already know this so by indexing on these things by indexing on age address name and employed that means i can query four conditions on these keys so i can say that i want the age to be 30 i want the name to be jane i want the address to be in la so conditions like that so conditions for query without performing these indexes is virtually impossible for us to be able to perform these queries with firebase so that's why we go to rules and we add this new field so here we add a new field that corresponds to the root node that we have so the the root node inside our original root node so people that's what we have it uh oops okay so people and then we create the value for it to be another object and this object would have dot dot index on and then within it we would list all the names of our fields or the keys that we're going to be querying on so age address name employ so now it can publish now that we've created this so we wait for it to publish rules are published we can go back to our data and we can start working through the code to be able to actually perform these queries so let's go back to the code right now and just see what we have so i'm going to keep this because this is pretty useful and except i'm just going to add some conditions right here so let's say i want to actually check for the name i want the name to be jane so here's what i have to do i have to order by child and then here i list the name of this field that i'm performing this condition on so name this could be age or address or employed and on this field i'm saying that i want to apply this condition now the condition is what follows there are a series of functions that can follow the order by child the first one is equal to so i want the name to be equal to jane and now i can say dot get all right so here i'm saying that order by child name meaning i want you to actually order my results the results within the db.child people i want you to order them by name and only get those who have the name equal to jing and that's pretty much how it works so if you want you can iterate over over them so four person in people or you can even not iterate over them but it's smarter to do this you can see them listed in a sense you can print the um you can print the person oops dot val and by doing so you're printing the values of these query results so let's run it and see the result that we get so running it we get the following so we get two different uh people with the name jane that's actually because we have two of them so we have jane this one with the address in la and age 32 and we have the other jane where the address is nla as well but age is 29 and we get these results we have them printed in this sort of dict so the person.val is a python dict or dictionary it's similar to a sort of json syntax and this is the information that we have so we can see the key value pairs are separated by commas and we can just sort of detect the keys versus the values so that's pretty much how you do it if you want you only want to print the let's say age of all the genes so you can print person.val sub h and by doing so you'd be able to get the age of everyone who is called jane and we get the age of 32 as well as 29 we can change this to address and then run it and then get la and la because both jane's live in la all right so that's pretty much all there is to it so this is how you get to access different parts of your query results so for now let's just print the query results so now that we know that we can order by child name and we can verify that it is equal to jane now what else can we do so instead of equal to there is another function that we can use i'll just good i'll go into it in a minute so let's order by age age is an end so if you want to verify age is an end here it's 32 here it's 29 here there is no age so it's pretty much not going back to the code we can say that the age should be equal to 32 right and now if we print the result we have only one result uh it's the following so jane has an age of 32 address of la employed is false now i don't have to just measure equal to so i can say start at 30 and by doing so i'm saying i want everyone whose age is greater than or equal to 30. so the age starts at 30. so here i should get one result which is the jane with the age of 32 since the other one has an age of 29 now if i change start at to say 25 and i run it again i get both jeans all right so that's pretty much how we can do this now one thing to note is that start at is greater than or equal to 25 we also have dot and at and we can put all of these in a sort of string of different functions just all following each other and that way we can combine these different combinations so the age also you could say ends at 30 so we want those whose age is between 25 and 30 in this case we get one result so this is how we sort of do it so this is how we perform this these conditions on the age on the end like here you can tell that the condition we're saying 25 as an int and not 25 as a string because that's not what we're doing and that's pretty much it now we can also verify that the boolean which is employed so let's say employed is equal to true so dot equals to true and then we get it and now we can print it and this is what we have so we get john smith who is employed true and jane was employed through so the jane with age of 29 is actually employed so that's really how you do it now there are different sorts of conditions you can have here so between equal to as well as start at and at another thing you can do is that you can limit the number of results so here i'm saying they're equal to true so here i have two results maybe i want to limit this result to one dot limit to first one uh sorry so limit to first one by this i'm saying that i want the first top one result of this uh of these employed different people so by doing so i get the first one which is john smith i can say limit to last and also get the bottom one in this case so here i get jane so you can also combine this with what we're doing with measuring the age by saying it's greater than 25 and i could choose to get only the people the top three people who have an age greater than 25 for example so this is how you combine them now one thing to keep in mind is that we're ordering by the child so when i say order by child i'm saying that i want to order by this field by this key or this key value pair so i'm saying i want to order by employed or age or name or pretty much anything else and i want this to have the following conditions and this is where you would have your series of functions for the conditions you can have equal to you can have start at and at you can have limits at the end so you can say i want to limit to the last five to the first three to the top ten this is essentially how you do it and in all cases you finish with a get function and when you do finish with a get function you can iterate over them using the each function and print out the value and you can just deal with the value with pretty much um anything so by actually accessing it like a python dict so going back here and changing name to employed and just keeping limit to last one and then running it here we have we can access the name of this person by printing person.val sub name and this is how we access that person's name so by that knowing these things by learning these things you're now pretty much ready to have any sort of query within your application and perform these operations on it by doing these queries you can filter your data extract relevant data as well as extract certain data with respect to relevant data so here we're extracting jane with respect to that the fact that she is employed and that is true so now that you know this you're pretty much done with the retrieving data part so thank you very much for watching this very long course or tutorial where we learned authentication storage and finally the firebase real-time database we learned these different cred applications and now you have all the tools necessary to start developing apps with python using firebase and using the firebase library so thank you very much for watching please leave a comment and a like if you enjoyed and i'll see you in the next video bye bye
Info
Channel: Code First with Hala
Views: 33,893
Rating: undefined out of 5
Keywords:
Id: s-Ga8c3toVY
Channel Id: undefined
Length: 64min 45sec (3885 seconds)
Published: Fri Jul 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.