Supabase Crash Course For Python Developers

Hi everyone, I’m Patrick and welcome to the SuperBase Crash Course for Python developers. In this video we cover almost all important features that we can use with SuperBase. I leave an overview here and also I leave timestamps below the video so if you’re free to jump around if you’re interested in a particular topic. Also I want to say a big thank you to SuperBase for sponsoring this video. I was a big fan already so I was very happy that they agreed on sponsoring me and if you want to check it out as well I leave a link in the description below you can get started for free and now with all further ado let’s get started. SuperBase is an open source firebase alternative so it’s a hosted platform that gives us a post-data base authentication and authorization, auto-generated APIs, edge functions, real-time subscriptions and storage and with this we can build our apps around this. For example we can build a JavaScript front end or a mobile app or in our case a Python app and here it says build in a weekend and scale to millions and I think this is actually true so I’ve used this before several times for personal projects and really like it it’s super simple to get started and as I said it’s open source so we can find this on GitHub it’s pretty popular and we can actually self-host this if we want this so there’s an official guide how to do this but in our case we just use the hosted solution and they offer a pretty generous free tier so we get a database and file storage then we get authentication and we can have 50,000 monthly active users so I think this is more than enough to get your projects off the ground so now in this tutorial we learn how to work with superbase pie which is the Python client for superbase so let’s get started with our first project. So let’s start a new project for this of course we have to create an account so you can sign up with GitHub that’s what I did and here I already created a demo project so now we want to create a new project so let’s choose your organization and then let’s give this a name so I call this to do app to create a simple to do app then let’s also create a password so I use Patrick Glover 1234 just for the demo then we can select the region and the plan so we continue with the free plan here and let’s click on create new project so our project was created here if you scroll down we find the project URL and the API key that we need to grab in a moment we can also have a look at different client libraries and see how to work with them in our case we’re going to use the Python client in a few moments then we can also have a look at different example projects so here we find startup templates for different frameworks but now let’s continue by getting started by building out the database so now we want to create a table and here we work with a post-risk database so we can get started in two different ways either by using the SQL editor and using different SQL commands or by using the table editor so we want to click on table editor and then let’s click on create a new table then let’s give this a name to do for a simple to do list example then let’s also give it a description to do items and then here we see enable row level security RLS which is recommended and enabled by default this gives us an additional layer of security but for now we disable this to make it simpler but we get back to this and enable this later and then here we can create our different columns so we already have an ID which is a unique identifier and this is our primary key and we have a created at timestamp so let’s click on add column and let’s give this a name so the name is name for the to do name then we also have to select a data type here for example we can use integers or floats or chasen and in our case we want to use the varchar to use a string and here we don’t give this a default value so we want to fill this later when we create this so let’s click on safe and this will create the table and now the table was created so here on the left you are still inside the table editor and now we can insert rows manually so we don’t set a ID this is automatically generated then and we also leave the created at timestamp out so this will use the current time by default and for the name let’s use item one and then hit safe and now this will add the first row to our table and now instead of doing this manually we want to do this from the code of course so for this let’s learn how to use the super base pi client so let’s learn how to work with the super base pi package we can install this with pip install super base and then here we find different code snippets for example for authentication signing in and managing data and we’re going to have a look at all of those examples but first we need to set up the client and for this we need to create a client and then we also need the super base URL and the key for this so let’s go to the to our IDE and here I have a new project and the terminal here in inside my IDE so here I say pip install super base and then I also want to install python dot n to store the environment variables in a ENV file so let’s it enter and install this and then let’s create a new file that we call dot ENV and now let’s go back to the docs so we could export them and set them on our machine but I want to store them in here so let’s paste this in here and remove the export statements and now we need to get the actual URL and key so let’s go back to our project and then here on the left we can go to project settings and then let’s go to API and here you can copy the URL so let’s replace this part with the actual URL and now we also need the API key so there are two different ones the public one and also a secret one so the secret one has the ability to bypass row level security and as I said we enable this later but for now we can simply work with the public one so let’s copy and paste this in here and now we can save this and go back and implement our first code so the first thing is to set up the client so let’s copy and paste this in here and I actually want to get rid of the type hints here because I want to keep it simple so let’s remove this and this and this so we import OS and then we also say from super base import create client then we need to load the URL and the key and then we create the client but in order to access those environment variables now of course we need to say from dot n import load dot n and then we call this load dot n and let’s actually make this the very first statement in our code so that now that we can work with this and now if we save this and let’s clear this and run this then this should actually work without an error so yeah now we have our client and now we can work with this so let’s learn how to do the basic create read update and delete operations in python but before we do it in python I want to show you one cool thing in the dashboard we can click on API docs and here we find a API documentation that is automatically generated and specific to our project for example here we can select the to do stable and then here we find code snippets for all the basic operations in JavaScript and for the bash unfortunately not for python but the python client code is very similar to the JavaScript client code so for example let’s go to read rows and then let’s select bash and then we can copy this curl command and then in the top we can also select the public API key so now here it already uses this API key and as you can see it uses the URL and then it’s setting the API key as header and also a authorization token as header so let’s copy this and let’s go to our to a terminal and then paste the command in here and if we hit enter then now it’s returning the one row in our table so this is working and now we want to do this in python so for this let’s go to the python documentation and we need to go to selection of data and then we only need this one line so here we say data equals super based table and then the table name in our case is called to do’s and then we select star so we select everything and then say execute and now if you print data then it should produce the exact same results that we are seeing here so let’s save this and run the code and yeah it’s working so now we see how to select data in python and here we say select star so for example we can also say select the ID and only the name and then if we run this then it’s selecting only the ID and the name and not the created at timestamp and then here for example we can also apply filters by saying dot and then for example we want to use the equal filter where we have to say or we want where we want to say the name should be equal to let’s try item 2 and if you run this then it should return an empty list because no item in our to do list is called item 2 so let’s try item 1 and now if we run this then it should again find this data here and to see all the available filters you could either have a look at this in your code by looking at the possible auto completions or you can again go to your API docs and then also here there should be a section that is called filtering and if you switch to chavascript then here you see different possible filters for example we have equal to greater than less than we have also greater than or equal and some other possible filters so yeah play around with them if you want to have different filters so let’s continue with some more operations so next let’s learn how to insert new items and for this we want the this command so let’s copy and paste this in here so here we say super based table and then again to do and now we insert and then here we can give it the name let’s call this to do to and then the ID and the created at are automatically generated so if we fetch some data afterwards again so let’s comment this out here and let’s paste this in here and let’s get rid of the equal filter and let’s also say star here again so now let’s run this and now it should insert this new item and then it’s returning this data so now it found two different items and then also if we go back to our dashboard and then go to the table editor and then select the to do then now as you can see it has the the second item in our table and the ID and the created at timestamp was automatically generated so of course we could also for example let’s say from date time we want to import date time and also a time delta and then here let’s create a timestamp where we say created at equals date time dot UTC now and then let’s say minus time delta where we say hours equals two and then here as second field when inserting in the dictionary we say create that at and this should be as a string the created at time and now let’s call this to do three and save this and run this and now in this case it used the custom time that we set so if we refresh the page then we now have three items and as you can see this timestamp was two hours earlier than the current time so this is how we insert data then we can also update data of course so let’s comment this out and let’s go back to the documentation and for updating data we need this line so let’s copy and paste this in here and here we say super based table then again we call this to do’s and then let’s for example update the name to be the updated name and then here we could update also more fields but in our case we only update the name and then again we need a filter for example again we use the equal filter where we check ID equals to one and then let’s comment this out and only use the update command and run the script and this worked and then if you go to our table and refresh the page then here we see where the ID is one we updated the name to be updated name so this is working and the last operation we can do is of course also delete data so let’s try this so let’s comment this out and then for deleting data we say super base table and again to do’s and now here we want to delete where equal to ID is one and now let’s run this and then again let’s refresh our page with the table and then you see we no longer have the ID one in here so this is how you do the basic create read update and delete operations in Python and now it’s next thing let’s have a look at how to do authentication and authorization so let’s learn about authentication and authorization and for this let’s create a new file 02 auth.py then again we want to copy the first part to set up our client and this case we don’t need this line and now let’s first go back to the docs of super base pie and here we find the authentication example so let’s copy this part and paste it in here so in this case we do a sign-up per email first let’s go back to our dashboard and here we can also select the authentication tab and let’s also have a look at the docs so let’s go to the documentation and under auth you see that we can have many different options for example lock in with email, magic link, apple and a lot more and in this case we can select the providers and then the email is automatically enabled if we want to use different ones then we also have to enable them here and then here you could invite the users but we sign them up from Python code now so let’s say the email is I use my email here so let’s say put at python-engineer.com and then as a password let’s say Patrick Lober1234 and then this is the no longer a random email so this is my actual email and my password so now let’s run this and first let’s only sign this up so now we say python02auth.py also it’s python02-auth.py and now I should get an email so let’s wait a few moments and here I got my email if I bring this here then I can it says confirm your sign up so I can click here and confirm this and then if you go back to our dashboard and refresh this then now you should see my new email so yeah now we have our first user so now after signing up we can the next time of course we can also sign in and the code is actually the very same except that now it’s saying sign in instead of sign up so let’s copy and paste this here as well and now here it’s called email and password and now after signing in let’s also print the user and let’s clear this and now let’s run this and see what happens so this is working so now we are signed in and user I think this is actually not correct so if you hover over this we see its session so actually I think it’s better to call this session and now if you print the session then you see we get an access token so I talk about this in a moment and then we also have a user object inside so for example we can print session user so this will give us the current user so that’s why it’s better to actually call this session and one more thing you might notice is that it doesn’t give me back the terminal in here so this means the code here is still running so there is a separate thread now and this is still running so let me actually hit control C on my keyboard to interrupt this and now in order to stop this in the end you have to say super base dot off dot sign out and now if we save this and run this then you see in the end it’s giving me back the terminal again so yeah remember that this will keep the session open as long as you are signed in and let’s add one more improvement so let’s say the password is wrong and if we run this then this will actually throw an exception so an API error so let’s catch this so here let’s say from go true exceptions we want to import the API error and then let’s wrap this in a try accept block and here let’s say accept the API error and in our case let’s simply print um lock in failed and now if we run this again then this should not crash but instead it should yeah let’s say session equals none in the beginning and then we set this here so let’s again run this and then it should not crash so yeah um this is how to catch the API error so now let’s first let’s make the password correct again and lock our user in and now let’s learn what we can do with a locked in user and also what we want to do with this access token and for this we also have to talk about row level security and additional policies so if you go to the documentation and then under authorization you find this row level security where you can watch the video or read through this documentation but basically this gives you granular authorization rules together with the policies so by default everyone can read and write to your database but this is not ideal so what you want to do is to set up additional rules for example you can define that only locked in users can read and write to your database tables or for example if you have a paid product with different tiers for example free and premium then you can give different access rights to the different tiers and yeah for this this is super handy and it’s also easy to set this up so let’s go back to our API dashboard and in this case let’s go to the table editor first and in this case here it’s already giving us this hint that the row level security is not enabled so we disabled this so let’s enable this again and confirm this and now the first thing we want to try is in the terminal let’s try this curl command again and now it’s giving us an empty list because the row level security locks down the database again so now what we could do is instead let’s go to the API docs and let’s go to to do’s and then here where we want to read where is it read rows and bash instead of the public key we could use the secret key and now if we copy this and paste this and then it should work again so now this will bypass the row level security and we also find this in project settings under API here as this secret so we could copy this code and then use it as our API key but this is actually not how we want to do this instead we want to create a policy so let’s click on table editor and our to do’s table and here you see no active RLS policies right now so let’s click on this and then click on new policy and then we can either use a template or get started from scratch let’s use a template here here for example we can enable read access to everyone then we can enable insert access for authenticated users only or we can make this based on different fields for example enable update access for users based on their email or here for example we can make this based on a field for example if we have different tiers like free or premium then we can enable read access only for premium members or something like this in our example let’s enable read access for authenticated users only so let’s use this template and now here we can change the allowed operations so let’s use select and then let’s also change the policy name so we want to enable select for authenticated users only the target role is still authenticated for example here we can also use different ones and as using expression we simply say true so everyone that is authenticated can then have select access and then here we can review this again and now we create this and now this policy is in place and now authenticated users should be able to read the database again so let’s confirm this and first I want to go to the API docs and grab the curl command again for reading the database so here on their read rows let’s select the public key here and this should no longer work so let’s copy the command and let’s actually also save this away here in a txt file so that we can edit this more easily so let’s copy and paste this to the terminal and now this should give us an empty list so yeah here we have it but now we want to set the access token of a locked in user here so let’s comment out the sign out statement and now instead of printing the whole session let’s only print session dot access token and let’s run this so let’s say python and then our script and now you see it prints the access tokens so let’s copy this and we don’t get back the terminal because we are still signed in so now in our command here the API key is still our public super based key but now for the beer token this part here we want to select this and remove this and now paste in the access token and now if we use this whole command and paste this in here and to enter then now it’s working again so now again we have read access so this is how it’s working with the row level security and the policies and then the tokens so now we want to do this in python as well so here let me hit control C and stop this and then first let’s print let’s grab some data and say data before signing in so this should be an empty list and let’s comment this out and run this and then we see we also have an empty list here and now let’s also do this after the lock in so here we say data after lock in and now in an ideal case this would automatically set the access token but right now there is still a small back in the super based client so it’s not done automatically but there is already an open issue so I think they will fix this soon now instead we have to set this manually with a small work around so here after signing in we also have to set this in the authentication part so we want to say super based dot post-grest and then dot off and this will authenticate the client with a token so here we put in session dot access token so first let me comment this out again and confirm that right now it’s still an empty list after the sign in so here you see two times we get an empty list and now we set the access token in this part and now let’s run this again and now we get back the data again and this is how we can enable a role-level security and then also have a policy in place and can work with a locked in user and the access token so now that we’ve learned about authentication let’s talk about a different topic and this is the storage so let’s create a new file and first let’s have a look at the documentation so here we can click on storage and overview and then here super based storage makes it simple to store and serve large files for example here we can upload images or videos and then we can also go to our dashboard and then on the left we can click on storage and then we can create a new bucket and give this a name for example let’s call this image bucket and then we want to make this public so here again we can enable role-level security policies but to keep it simple let’s simply keep this public and then let’s click on create and now here we can manually drop files for example let me drop this Twitter profile image here and then it’s already uploaded so here we can access this and also copy the URL and then if we paste this here then here we have our image which is now served from this storage and now of course we want to do this in the code so here again let’s create our client and copy this part and now we can access this storage by saying response equals super based dot storage dot from underscore and now the bucket names so this was called image bucket and then we say dot get public URL and this was called I think pat underscore twitter dot p and g so let’s print the response and now let’s say python and then the file and here it’s working so let’s click on this link and open this and then again here we see our image so this is working and now let’s also upload images so let’s comment this out and now to upload this we want to again use the same first part super based storage from and then our bucket name and then we say dot upload and this gets a path and a file and then also file options if we want so for the path let’s use a new image so let me move this in here so we call this banana dot chaypec here we can simply use the file name and then for the file so the first is the path and the second the file but we actually want to use the same path then so let’s use this and this and then by default this will be a text data field so we want to set this as a chaypec image so we want to specify the content type and this is a dictionary so a dictionary with the key content type and then the value is image slash chaypec so this is for the header and now if we save this and then let’s again print the response I think this still might give us an error so yeah like I said we get a storage exception so new row violates row level security policy for table objects so even though we’ve made this public here we have to go to policies and then for objects and now here let’s say new policy then again let’s get started quickly and enable read access to everyone and then we use this template and then let’s enable everything so here true and true and then let’s click on review and let’s actually also change the policy name and enable all access for all users and save this and now the policy is in place so let’s run this again and now we get this status code 200 so now this is working and now if we have a look at our image bucket then we see we also have the banana image that was uploaded and then we could access this again in our code and now as last part let’s also have a look at how to use edge functions so if we have a look at the documentation and go to edge functions then edge functions are server side typescript functions that can interact with our database for example they can be used for listening to web hooks or integrating your super based project with third party libraries like stripe and we can also then invoke them from python and for this let’s first go to our dashboard again and then go to edge functions and here we see how to create them so for this we need the command line interface if we have a look at the docs and CLI then here we can install them so on a mac I use brew install and then super based tap super base and now after installing this we can then use this command so let me copy and paste this and now let’s go to the terminal and inside the project let’s create a new function by using this command and now if we have a look at the IDE then it created this new folder here with our function and yeah as I said this is TypeScript code with here a simple hello world example and now we want to deploy this so for this we want to grab the next command so super based functions deploy then the name and then the project draft so this is automatically filled out here so let’s copy and paste this as well and hit enter and here it says deploy the function hello world on our project and if we refresh this I think we should also see this here so yeah here we have this and now if you send a request to this URL then we invoke the function and in our python SDK we can also easily do this so here we have the example snippet so let’s copy and paste this in here and let’s create a new file 04.funk.py and copy this and then of course we also want to load our ENV variables and this was called super base URL and super base key then we create our client and then we can call super base functions and now we want to create an async function so we say async def and then a function name we actually no longer need the decorator this is deprecated and we also don’t need the argument here so it’s not used and here we use async.io so we also need to import this so we import async.io and then here we await funk invoke and then our function name in our case it was called hello world and then we can also send additional information in the buddy so this is an empty dictionary here and then we return this then we create a new event loop and run this and here we have our function then let’s also print the response and now if we run this script then it should invoke this function and give us the result that our function is doing so let’s run this and see if it works so yeah it worked but it’s also giving us a deprecation warning so I think we want to use new event loop now so let’s try this again so yeah now it’s working without the warning so here we get the result and here we can basically now implement whatever we want so in this case it’s simply getting the data and then it returns hello name in our case it is undefined so that’s why it’s not returning a name here but we can actually now send the name in the buddy we can use as key name and now as name let’s try Patrick and now if we run this again then you see the response is message hello Patrick so let’s try another one let’s try name chassis and now we see hello chassis and this is how we can work with functions so of course this is a toy example so now you could implement here whatever logic you want and then again deploy this function and then invoke this here and then again get the result so yeah that’s all for this crash course I hope you enjoyed this tutorial and if you need more information you can check out the docs they are pretty good actually and let me know in the comments if you want to see more super based tutorials in the future on this channel for example where I build an actual app with this and then I hope to see you in the next video bye.

AI video(s) you might be interested in …