Deploying Flask Apps Using Python Anywhere

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone its aunty from pretty printed here in today's video I'm going to show you how to deploy a flask app to Python anywhere so if you're not familiar with Python anywhere it's a really easy way to host Python web apps so this could be flask Django pyramid or really any other framework that you can use in Python to build web apps and I've created a video like this four years ago but it's a little out of date so I'm creating this video today so you can have an updated way of doing this because things have changed just a little bit so you can use Python anywhere for free they have something called the beginner accounts and just waiting for the site to load it's a little slow right now and you can just create a bigger inner account and it's free if you want more functionality like hosting more apps or just having more CPU power or various things you can pay the cheapest plan as five dollars a month but for our purposes the free account will be sufficient so this is what the dashboard looks like once you create a new accounts you have these links up here consoles files web tasks and database we'll be working with the first four and I'm sure you can imagine what databases mean and tasks are just things that you want to run at regular intervals and it's pretty easy to figure out how to use once you go there so the first thing I need to worry about is getting my code from my local machine which is running this app here so the app isn't really that important but I want to get the code for this app onto Python anywhere so I can actually you know have my own address for it so what I'll do is I'll stop the app and I'll create a git repository so get an it and what I'll do is I'll put my virtual environment directory and I get ignore so I'll create that so dot gets ignore and inside I'll just put env slash to ignore the interview directory and it should turn white in just a moment to signify that it's no longer included with the rest of the files and we see that it's white and then what I need to do is I need to start up my virtual environment again and what I'm going to do is I'm going to create a requirements that text file because I need to replicate this virtual environment on the Python anywhere so what I can do is hit freeze and then requirements texts and we should see the libraries that I have okay so now I can do get ad also get ad period get commit - em first commit and what I want to do is I want to create a repo on github so I have my github account I'll create a new one a new repo so I'll call this flask QA app make it private and the rest of the settings are optional hit create and because I already have a repository I want to use these down here so these two commands so I'll copy the first and I'll paste it in so I'm adding the origin which means I'm connecting my repo on my machine to the actual remote repo on github and I can push it so get push - you or Jam master and it's gonna prompt me for my username and password so Anthony add prettyprint accom and then my password and it should push everything over to Python anywhere and let's refresh okay so I see my code here so now I want to bring this code over to Python anywhere so to do that I need to start up a console so I'll go to the consoles link over here and I will create a new bash console so I'll just click bash and now wait for it to load and it's ready now so what I want to do is I want to clone at the directory or the clone the repo that I have so git clone is what I want so it gets already installed when you use Python anywhere I'll click on this clone or download link and I'll just copy the link and paste it in and it's going to prompt me for the same username and password again so I'll just enter those and now I should have the directory on Python network so flask underscore QA underscore app so that's exactly what I have now once you create a virtual environment to run this app so using Python anywhere you can use MK virtual and B which is a more convenient way of working with multiple virtual environments we only need one but it's still pretty useful for creating one so to create a single virtual environment I'm going to use the command MK virtual and V then I need to give it a name so you will be the name and then I need to specify a Python version so - - Python equals quotes slash user so USR slash bin slash Python 3.8 which is the latest version of Python at the recording of this video and it's gonna go ahead and create a virtual environment for me and now it's done so now that I have the virtual environment created I want to create or install all the libraries that I have in my project so I'll CD into the directory so flask UA app if I hit LS or type LS I see requirements text so I'll use pip install - R and this will install all of the requirements inside of the requirements text okay so now that everything is installed I can leave the console and where I want to go next is the web so I'll just click on this menu over here and go to web and then I want to click add a new app and just click Next and instead of clicking flask I'm going to use manual configuration and I'm going to select Python 3.8 because that's the version of the virtual environment that I'm using and then just click Next and it should create the app for you okay so once that's done you should be able to click on this link and see something and we see hello world so if you can see this you know the app was set up correctly so I'll leave that open because I'll just refresh it when I have my stuff in there and now you to manage some of the settings so under virtual env you can just type in the name of your virtual environment you don't have to select the entire path or a type the entire path because it will figure it out for you so mine is e and B and then a source code I enter a path to your source code so same thing a flask UA app and then what we want to do is we want to click on this WSGI configuration file and this section here starting from line 19 down to 47 this generates this particular page so this is just an example to make sure everything's working so you can delete this and then you need to go down to the flask section and just uncomment everything with one hash so one hash there one there one there one there and one there right so there should be what five things you have to uncomment and not include even spaces so for path you need to specify the path of your flask app directory so in my particular case it's flask underscore QA app and here on line 80 we need to import the either the main flask app file or from the main flask app file or just from the project so in my particular case I'm importing from the project so if we go back this is what the project looks like so this is flask UA app and then flask UA is the name of the app so flask UA so I'm going to import from flask underscore QA and instead of importing app directly I can't do that in my particular case because I'm using the application factory pattern so I have this create app so what I need to do is I need to import to create app instead of app now if you're not using the create app the app factory pattern that you can import the app directly but I have to import create app and then on the next line what I can do is I can say application so they want it to be application instead of app so application equals create app and it's a function so I have to have the parentheses and I can save this and then I can go back to the web tab I'm gonna open this one up because I know I have to come back here for something so I open it up in a new tab and you can click reload pretty printed Python or calm or whatever your username is you can reload it there or you can reload it up here either method reloads your app so we're going to wait for this to reload and then I'll refresh the page and we get an internal server error so a way that we can check the errors in our app is to go down to our log files and I'll click the error one so the one in the middle and then I can look at the air and I can determine how to fix it so I know the reason why this area is happening so I'm using Python dot e + V or yeah Python DME that's the name of it so I have these dot env files in the dot flask in V files so I need to load this into the environment and it doesn't happen automatically on Python anywhere I have to do something so here's the information on how to do that and it's down here so this import OS and then all this code that follows so what I'll do is I'll put it in my code between the path part and the import from flask and what I'll do is I will say the path is going to be the same as the path that I have define above so that and then the project folder is the variable that gets assigned and I'm looking for the DMV file and by calling load dot in it's going to load everything into the environment so I'll hit save and I'll restart the server and then I'll try refreshing and we see the app is now working and I can go to the different pages of the app and see it like I said the app itself isn't too important but uh we see that it works and one last thing that you may want to do is you want to you may want to set the static directory so slash static slash and then in my particular project it's going to be home a pretty printed admin / flask under square QA app / flask keyway / static so any static files that I have can be served from the file system instead of the app itself and I can do things like force HTTP I can have password protection whatever I want in these settings I can change but the most important part is the app itself is work so just a recap really the most important two things that you have to know is if you're using the application factory pattern you need to import your create app function and then use that to create an object called application not app and if you're using Python env like I am you have to add this extra code to make it work I'll put this as a link in the description below so you can take a look at it and copy this code if you need it for your particular app so that's it for this video if you have any questions about deploying to Python anywhere feel free to leave the 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: 81,523
Rating: undefined out of 5
Keywords: prettyprinted, flask, deployment, python, python anywhere, programming, tutorial
Id: 5jbdkOlf4cY
Channel Id: undefined
Length: 12min 10sec (730 seconds)
Published: Thu Apr 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.