How to Deploy a Flask App and Postgres Database to Render

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in today's video I want to show you how to deploy a flask app and create a database on this service called render so render is a service that you can use to host web apps create databases hose redis uh host Docker containers and many other things that you would want to do while hosting an app so I want to cover two of the most important things that you'll do with flask which will be hosting a flask app itself and then hosting a database to go along with it so render has a free plan it's a little bit Limited in the terms of how many things you can have at once so you can really only have one app running at a time but if you want to have more than one it's pretty cheap to get started with the uh the cheapest plan here and something that you should know is there's a process of deploying the app that is going to be a little bit slower for free plans but if you were to upgrade to one of the paid plans so even the cheapest one then it will run at a much faster pace so just keep that in mind you'll see what I'm talking about later in the video and I know that deployment can be difficult for a lot of people there are a lot of places where you can get tripped up and I'll try to cover those in this video but if after watching this video you feel like you still need help with this feel free to reach out to me directly I have a program where I can work with you one-on-one that's called my coaching program so you can go to prettyprinted.com coaching or you can go to the link in the description below and click on it and then you'll learn about my coaching service and then you can fill out a form if you want to actually talk to me one-on-one so let's get into this video first this is the the dashboard that you'll see when you sign into your account after creating an account and you see here there are many different things that you can get started with so static site so if you had HTML and JavaScript you can deploy it here a web service which is what I'm going to do in this video which is the flask part then you have other things and then you have postgres right here that's what I'm going to do in this video as well also let me show you the code that I have set up for this so I have a very simple flask app it doesn't really do anything so here's the dunder init file and then here's the route stop high so basically the idea is I have some users in the database I have one route that will show other users in the database and I have another route that will add users to the database so it doesn't really do much but the point here is just to demonstrate that there is a database and I'm going to transfer it over to postgres in a moment right now I'm using sqlite but I want to change that to postgres so let's get started with that first before we even deploy the app because I can work with postgres even on my local machine I can connect to the external postgres server and then run everything through there so let's go back to the dashboard and then I'll go over to postgresql and hit new postgresql and I'll just give it a name so I'll call this pretty printed render example and then I'll just name the database the same thing it could be a randomly generated name if you want I'll let the user be randomly generated for the region I'll use organ uh 15 as the version and then I don't need datadog here I have the free one but of course if you want to pay for another one you definitely can I can't select them because I don't have a credit card on file so I'll just hit create database here and it seems it doesn't like my Dash so let me just go ahead and remove that and then create database I don't mind if the actual name of the database Is Random so I'll just wait for it to create we see here it's grading and I'll just wait a moment for that to finish okay so we see it's available now so everything should be working I can go up to the upper right hand corner here and we see there are two types of connections so we have an internal connection and an external connection so the internal connection is when the r app is hosted on render so we would use this URL and then when we're connecting from outside of render we want to use this one so let me go ahead and copy this one and I'll place it in my code just as a comment here so you can see it so it has my username here it has a password it has the location of the database which is this URL here and it also has the name of my database so it turns out it actually named it pretty printed render example it just use underscores instead of dashes okay so I just open my terminal and I want to get this working the first thing I want to do is I want to create an environment variable to hold on to this URL string here because when I deploy it to render it's going to be easier to work with an actual environment variable instead of just hard coding it in my code so I'll create that I'm on Linux so I'll do export and I'll just call this database underscore URL equals and don't even need the space equals the string that I copied and there is one thing that I need to change so let me do that it can't be postgres at the beginning here so we see it's postgres colon slash slash this needs to be postgresql so put a s q l at the end and then I can export that so now I can import Os from Python and there are multiple ways of setting up environment variables but I'm just using this approach for this video there's uh no particular advantage to this one there are other methods that are just as valid so here I'm doing OS Environ gets and then the database URL okay so I'll just leave that there just so I can see it and now what I want to try is I want to open up my shell so flask shell and we see it can't find my application all you need to do is export flask app equals my app and then I should be able to do flask shell and now I get this error so because my database URL has that postgresql at the beginning it is now going to be looking for an installed postgresql library and to do that I can do pip install psycho pg2-binary so on some systems you don't need the dash binary but on mine I do so I'll go ahead and install that and now if I run flask run it works and if I do flask shell to get back where I was going this works as well so with this version of flask all I have to do is do db.create all and assuming I get no errors then we know the database was created properly and we can make sure let me open up a database tool I'm opening D beaver and what I'll do in D Beaver is connect to the database that I just created on render that way we can see the table that was created which is just this user table and then once I add data into the database you'll see it there so it's opening and it's complaining about old connections but that's fine so let me go ahead and open a new connection and here I just need to put in the information from the URL so this is the username the part before the colon so username goes here and then I have the password which is after the colon and before the at sign so I'll put that there the name of the database is at the very end after the slash so that goes in database here and then finally The Host is here so after the at sign and before the Slash and then I can test connection it should say connected and it will be similar regardless of what database tool you use so I'll go ahead and open this and I'll go to databases and then the name of my database schema it's under the public schema and then we have tables and then we have the user table right so we see we have ID and username here so let me just try to add some data so right now I'll go to data and there should be nothing in here because I just created it so let me go back over to my app and exit out of the shell and instead start it and to add something I just go to my route slash add and then a name so I'll just add myself so localhost 5000 slash at slash Anthony and it redirects me back to the home page and it gives me a list of all the users so I see Anthony here if I open up the tool again and just refresh this here we see Anthony is here and just to show you that it's still working if I do add and then let's say pretty printed as a user go back to the database refresh we see it added pretty printed here so the database is working I'm able to connect to this database that I just created on render and do everything on my local machine so that's great now the next thing I want to do is I want to actually deploy my app to render so let's go back and what this takes is first creating a repo so what I'll do is I'll initialize this so get a knit as a git repo and then I'll create a git ignore so I'll just put in this directory uh dot git ignore because I want to ignore the virtual environment directory and I want to ignore the directory with the sqlite database here and then finally I can ignore any directory that starts with pi cache okay just like that so I'll save that and now those three directories turn gray because they're no longer part of the repo next I want to create a requirements.txt file so what I can do is I can say pip freeze and then requirements.txt and the reason why I want to do this is because render is going to detect that I have a python app and then once it detects I have a python amp it's going to try to install requirements.txt and then that's how everything is going to get going so let me go ahead and just add everything here and commits and once I do that I now need to go over to GitHub and create a new repo so we'll call this render demo and I'll make this private and I'll just create Repository and at the bottom here since I already have a repository I can just copy this git remote ad origin go back to the code put it here and then I can do git push origin master go back refresh and now I see my code is here so now I want to get this onto render so let's go back to the render dashboard I'll click this and I'll click new up here and I want web service and what it's going to ask me to do is connect a repository so render demos the one I just created so I'll connect that one and then I need to give it a name so we'll call this pretty printed flask app demo the branch is going to be master of course if you have different branches you can change this the root directory you can change this if the root directory is different from like the base directory here for me it's right here and here we see the environment it defaults as python3 just make sure it's python3 because you don't have one of these other apps and then we have the bill command which is PIP install Dash R requirements and then the start so we see here it's asking us for start command and this command is going to get our app started so what they recommend using is G unicorn so let me go back over to my app and I can install that with Pip so pip install G unicorn and G unicorn is just an app server so it's something that you can use to start your app it's kind of like doing Flash run but G unicorn is designed to be running for long periods of time whereas flat screen is designed just for testing purposes right so since I've installed something new I can do pip freeze and then requirements about text again and with this method of using G unicorn I can't use flask Run Like I Do locally so what I want to do is I want to create a file called run.pi so run.pi and all this is going to do is import my create app function so from my app import create app and I just want to say app equals create app and the reason why I'm doing this is because G unicorn is going to look for this app depending on how I write the start command and it will start using this and I will get everything going so since I've made all these changes let me just go ahead and commit everything and I'll push to GitHub again so git push origin master and now I can go back to render and I can say G unicorn and then the name of the file so run colon app so it's going to look for a file called run and then inside of that run file it's going to look for an object called app once again I'm picking the free one and then there are advanced settings but I'm just going to skip over those and create web service here and then it's going to start the process of trying to take my code build it and then run it and if there are any issues we'll see them here so like I mentioned before if you're on the free plan this may take a while this is going to be slow but if you are paying this will be a lot faster so I'll just wait for it to finish okay so it has finally failed we see that it says error either SQL Alchemy database URI or SQL Alchemy binds must be set and that's because I'm using the environment variable and I haven't set it up yet so what I want to do is I want to go over to the dashboard again and I'll go to my database and this is where I'm going to use the internal connection so I'll copy the URL here and then I'll go back to my app so back to the dashboard and then click on app and then on the left hand side here we see environment and I want to add an environment variable the key is going to be database URL just like I created it locally and I'll just paste everything in here remember I have to change this from postgres to postgresql so I'll put the Q in the L on the end and then once I do that I can just save and then I can go to events here and now that I've saved the environment it's going to start the deploy process again so I'll just sit here and wait until it finishes okay so now it says deploy live and what I can do is I can click on that and we'll see the log here and we see that it's listening on Port 10 000. here I have a link to the app so I'll just click on it it will open up in a new tab and we see Anthony and pretty printed here and I can do the same thing I can do add and then let's say another one let's call this user python I'll just add python to the database and we'll add one more flask and if I go back and bring up my database viewer I can refresh and I see Python and flask are there so we see my app is running it has been deployed I can give this URL to someone and they can use my app and by the way if you have a domain and you want to use a domain name with this there is some setting here right here custom domains you just have to follow the instructions here I don't have a domain available to demonstrate this but the instructions are pretty straightforward on getting custom domains set up so if that's what you want for your app you can definitely do that so that's all the stuff that I want to cover in this video basically deploying a flask app with postgres to render if you have any questions about anything I've done here feel free to leave a comment down below if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe thank you for watching and I will talk to you next time
Info
Channel: Pretty Printed
Views: 43,960
Rating: undefined out of 5
Keywords: flask, render, postgresql, deployment
Id: IBfj_0Zf2Mo
Channel Id: undefined
Length: 16min 38sec (998 seconds)
Published: Fri Jan 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.