Quick Flask Deployment: From Localhost to a Domain!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up my name is avi and welcome back to the codex in this video we're going to learn about deployment of our flask application online on the server right now a lot of the projects that we've built on the codex that you might have seen are all local projects on our platform so the projects that we have on the platform for example things like the machine learning model or speech recognition with python and flask these flask applications are all running on your local host on a specific port and for example with this case like a simple weather dashboard submitting a zip code and getting the results works locally on my machine but if i wanted to give this to my friend or tell my family about this i don't have a link to show that so in today's video i'm going to be showing you how you can literally deploy your flask application in one click and the service that i'm going to be using today is going to be a platform called zit dot co you can head over to the link z dot co slash r slash the codex in order to access the platform and deploy your project with literally just one click this startup was founded by a friend johnny and it's been quite amazing to see their platform grow and expand i currently run production level platforms on their platform as well and it's honestly blown my mind at how simple it is to now deploy flask and django sites on z so first thing first what i need you to do is if you're following along with your own local project the first thing that i'd love for you to do is make sure you have your code on a github repository so right now i simply have my pycharm files again my app.py config.ini file templates and static all stored on a github repository in this case my flask weather dashboard uh if you never use git before again all you have to do is make an account initialize a new repository and then if you go through the steps for example if i just call this test flask it'll go ahead and ask you to follow the list of steps on your console in order to add your repository to github this step is crucial one it's always good practice for version control but two z has a direct connection with github and deploying your projects from github directly on zit so make sure you have your project on your respective github account and i'm going to go back over to flask weather dashboard this is my code the next step you actually need here is going to be a requirements requirements.txt file so the whole idea is in one click we choose which github project we want to deploy z will handle everything else for us and what's really cool on their platform and the list of features they mentioned is that they manage not just generating the ssl certificate but also load balancing they have a continuous integration deployment pipeline and their pricing is very straightforward and they offer free instances for you to use so why zeet over something like aws hosting on an ec2 instance or digitalocean and the whole reason is one quick deployment recently for a production level application i was using an ec2 instance where i was managing the front end on one port the back end which is a flask gap on another port i had to generate my own ssl certificates run my own nginx configuration and develop my own web service gate interface with uwsgi it was a complete pain and then i discovered z and my life kind of changed so let's go ahead and deploy this flask website again this uh codex project which is our flask weather dashboard onto z the first step here guys is we need to go ahead and create requirements of txt of all the configurations we need in order to do so i'm going to go ahead and create a new virtual environment i'm going to call this vonf and if i go ahead and refresh my um page i see von there i'm going to activate my virtual environment so here i'm going to say source von bin activate and then inside of this i'm going to go ahead and essentially install what we need so it's request config parser and flask whatever flask app you're trying to deploy make sure you have a requirement as a txt file with those specific requirements so in this case i'm going to go ahead and say pip 3 install requests um pip3 install config parser config config parser and then pip3 install flask and once i have my three requirements i'm going to go ahead and save all my requirements into a requirements.txt file i can do that by saying pip3 freeze and then this double alligator sign requirements.txt so super straightforward and now if i refresh this folder i'm going to go ahead and see requirements.txt i can open it and there we go the last thing i need is actually going to be um i forgot this last package it's going to be called gunacore and um unicorn is essentially a web server uh gateway interface that allows us to essentially put a layer above flask and so for our project you know it's recommended you never deploy a flash site in production you have kind of like this web service gateway interface above it and what this web service gateway interface allows you to do is it kind of takes care of communicating um between your like your engine x so your your web gateway and your actual flask application by creating multiple instances it also manages load balancing in this case of making sure that if one of your servers goes down it'll kind of load balance into a different server make sure to load back that server up again it's a very powerful gateway of communication between multiple instances of your flask application and then the network requests coming in and so it's recommended that you actually deploy your flask app on a web service um gateway interface like unicorn or uw-sgi i'm going to go ahead and use unicorn for this project so the last thing i'm going to go ahead and install is over here back in pycharm i'm going to say pip3 install gunacore there we go and so now if i go ahead and freeze one more time in my requirements of txt i will now see unicorn is now there which is exactly what we need the next step is back in your app.py make sure that you're running this on the host that is equal to 0.0.0.0 and our port here is going to be 5000. this is just uh general configurations for a back-end service in this case um running on zero zero zero zero port five thousand go ahead and save this and finally add in the changes so i'm going to add in my changes for app.py and add in my changes for requirements.txt i'm going to commit these by saying um project ready for deployment then i'm going to push awesome so now i have my project with the correct host the port and all my requirements set up and now if i go back over to z i can deploy this flask project in one click so head over to z dot co slash r slash the codex and then go ahead and sign up for an account and once you've done that go ahead and click on this button new project so it's going to ask you okay you have one free basic project available which is exactly what we need and we're going to go ahead and say github so we want to connect to our github and get the repository that we just made that's going to be this flask weather dashboard if i refresh my requirements is there my app.py is there fantastic and so over here i have the curryman flask weather dashboard i'm going to continue and that is literally it if you hit deploy now you can deploy this flask repository onto zit's server now it will ask you a couple of follow-up questions just to get the proper configurations and i definitely recommend you do this so here's going to ask you what kind of project do you have again i'm running python 3.8 so i'm going to go ahead and save that it's going to say build command so what do you need to install since we made the requirements a txt file we're going to install pip install dash r requirements.txt and then last thing for the run command normally i'd say you know python main.py or python app.py would work but again we want to go ahead and use a wsgi server and the wsgi server again helps us to communicate between the web server and the web application so it helps us to distribute the load make sure that multiple processes of our web app are running all of that and so our run command is going to be a little bit different we're going to say here unicorn uh and then after that we're going to go ahead and say here um dash w 4 so that's going to go ahead and create four worker instances that will be essentially creating um you know four workers to run commands on and then we're gonna say we're binding this to 0.0.0.0 so again like the domain or the the host of our localhost is going to be running and then colon the port so our port was 5000 again these are the configurations we set in pie chart host 0.0.0.0 port 5000 and then last but not least we want to go ahead and just say here run our app and the file name is app.py so it's going to be app colon app so um if i go back to my dashboard um looks like those changes not get saved so let me actually go back over here and save these so again it was unicorn w for workers dash b 0.0.0.0 and then colon 5000 as our port so we're binding to the 0.0.0.0 full colon 5000 port and then we're saying that deploy on the app file so run app.py so you go ahead and save this and then go to deployments in your z dashboard and refresh it's going to go ahead and say a brand new build is pending it was updated 10 seconds ago and as soon as this is finished running it will be available at this domain curryman foster weather dashboard dot z dot app so again this website is loading right now but just to give you a quick overview of how simple zit is as you saw in one click you can take the flask project and literally deploy it for the world to see so imagine you know you're creating a personal project whether it's your own website um your own backend service or api you can deploy this with z in just a matter of seconds and then i think what's really cool about z is that it offers a wide variety of different features you can track logs of your project and also metric usage so cpu and memory and z offers different tiered pricing based on the amount of memory and cpu you're using so that it fits your needs um i have been through the stress and hassle of deploying an aws ec2 instance and trust me when i say z is a game changer when it comes to deploying any one of these python web applications or honestly any form of web application online on a server so i'm going to go ahead and let this deployment finish as you can see it's building right now we can track the progress again through this pipeline over here of commit build and deploy and there's a continuous integration loop so every single time i push the master this new deployment will generate and my latest changes will be live on the site so right now we can see that this site is deploying it is now deployed if i refresh this page let's take a look and see what happens it might take a couple seconds here but again we can check the logs and make sure that everything is working and i'll go ahead and go to the point in the video where this loads so the last thing i forgot to do guys is i wanted to set the port on which our server exists and here in this case our port is 5000 so go ahead and set your port 5000 protocol tcp auto https hit save this again if you go to deployments will generate a new build for us to deploy so i'm going to refresh the page it is now deploying a brand new build with this updated setting and now i'm going to go ahead and speed up to the point where the site is now live all right so as you can see over here our build got deployed and now if i go ahead and refresh the curryman flash that wear the dashboard at the z location we have our localhost application now available for the world to see and guess what z offers automatic ssl certificates so now i have https right there and there if i wanted a custom domain on this application i literally just have to specify my custom domain here and add in one cname record in cloudflare or in godaddy wherever your domain is hosted so let's go ahead and test this out let's say i enter in my zip code hit submit and there we go weather in san jose is the temperature clouds this is what it feels like fantastic that is one click deployment with z dot co uh and flask and you can take this process and apply it to any application a node app an express app a django app and i plan to create further videos explaining what more you can do with z deploying a django app in the future but again remember share this with your friends share this with families z is i believe the future of deployment and i hope this tutorial was helpful for you in deploying your flask site on zeet and getting it up and running on your very own custom domain in the future thanks so much for listening and i hope to see you in a future video on the codex platform
Info
Channel: TheCodex
Views: 68,842
Rating: undefined out of 5
Keywords:
Id: 1FdrJPt77GU
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.