Flask Tutorial Web Development with Python 2 - Basic site setup
Key Takeaways
This video series covers the setup of a basic website using Flask, a Python web development framework, including configuring SSH, setting up a MySQL database, and creating a virtual environment. The tutorial demonstrates how to create a Flask app, configure it with a virtual host, and access it using an IP address.
Full Transcript
hello everybody and welcome to the second practical flash tutorial video in this video we're going to be just continuing continuing along building our website so now that we've done sudo apt-get upgrade luckily I'm pretty quick here and I'm still connected but you'll find that after a pretty short period of time of inactivity you were going to lose connection it's going to disconnect you and it's kind of like a tie basically is a timeout and so it's a security measure where if you're inactive for X amount of time it just disconnects and that's a good idea but it can get annoying sometimes now because I'm filming a tutorial on it it's conceivable that I talked for so many seconds and then all of a sudden that were disconnected it's very annoying sometimes or you're not sure how to do something so you go to Google you look it up and you come back and you're disconnected it's a good idea on your for sure on your development server not necessarily a big deal on a you know I mean it's a good idea for sure on your production server I'm not really sure it's a that necessary on a development server again though is a security kind of thing in place so change at your own risk but that's we're going to talk about now and what better time to discuss how developers develop in reality than this this is the number one developer tool I'm about to show you it's not really a secret but kind of and that is this tool right here sorry this is what developers use honestly so my curiosity let's say is I'm not sure how to stop this dang timeout so let's say um let's say SSH timeout Linux and look at that first option I've already clicked it but that's it SSH timeout Linux how to disable it so we'll click on that link and we're like okay what do we do we're seeing a bunch of this nastiness basically we're looking for some sort of command line stuff and in the file so generally you've got a configuration file looks like we just found it so we're going to look for SSH config but you're going to learn that there's a couple of different kind of meanings of things depending on the operating system so first let's get let's go to that directory so that directory was let's do CD etc' SSH let's just go there first and then so CD for change directory and then you give the full path directory that you want to go to and let's do LS and this lists out the stuff in here so we see we do have an SSH config and we also have an SH HD config and someone can correct me I believe the ds4 daemon but I might be wrong anyway uh so now to open it we're going to use sudo for Super User do and then Nano and then I'll will first open up SSH config since that's what the internet says so we come in here and we're kind of looking around and it doesn't look like there's really much here in this file and if we kind of look here as far as they're setting suggestions let's say alive or keep alive is the one I usually use to find so to find something within a file in nano control W and then let's say we search for keep alive and it says keep alive not found it's just not here so this might not be the file we want let's check out sshd config instead so sudo nano if you want to like basically rewrite the previous line you hit the up arrow instead of SSH config will do sshd config now again ctrl W keep alive and little behold there it is so what was the stuff that it was suggesting again we'll come back to the lovely internet here basically this line here so we've got some stuff here that maybe we don't understand but we can clearly see this 9999 that's probs what what we're thinking is going to be you know this really long-winded you know over 9,000 number so we'll copy that and we see at least one of these TCP keepalive is already here so we'll come here and a way you can delete a whole line in Nano is ctrl K boom done now after you've copied something to paste you just right click and bam there it is and then just kind of watch out because you see this little comment here it shouldn't make any any problem because it is a comment but if it wasn't a comment it would cause some trouble but anyway we'll hit enter done now we want to save and exit so ctrl X will exit it's asking if we want to save it Y for yes this is the file name as long as you want to save it the same file name enter done so now we have in theory a very long time out but of course we need to actually since this was we modified the SSH service so now we need to restart that service so service SSH restart done and that's it so now we have this kind of unlimited login and a brief introduction to the number one developer tool moving along we're ready to kind of set up this website now so coming back over here this is another developer tool and honestly when I develop stuff I'll and almost every developer I know they tend to go back to their previous work and like reference their previous work to move forward so generally you hear about people say they've got a cookbook right that's their own private stash of code that they've used to do all kinds of stuff my cookbook just so happens to be online and in tutorial form a lot of times on Python programming net but we're going to use one of them right now so we're going to come over to web dev and we're interested in the second one right here so that's all this code that we're about to run now but just in case there wasn't one there let's go back instead of SSH timeout let's say a deploy flask Linux okay and we have a couple of options here we've got the flask mega tutorial from Miguel here but then there's also this how to deploy a flask application on Ubuntu VPS well that sounds a lot like us let's click on that bad boy and see what they have to offer so they've got some stuff here and then wouldn't you know it here's a bunch of command line options for setting up exactly what we want to do and so again perfect and it's basically a bunch of copy and paste you just copy and paste all these little commands here and change this R you'll probably wind up with servers or the shot we're gonna use here it's basically the same thing I have changed a few things we're going to at least start off with a few different installation hands a couple of tweaks here and there but for the most part it's almost identical to this one so again if you weren't sure how to do it just google it honestly the best resource you'll ever find so anyway we're going to come here and we're going to begin so we're just going to start running all these commands so first we've got this sudo apt-get install Apache and in fact let me do this just so you guys can see exactly what I'm doing I hate to like go back and forth and I hate to just not show so that someone gets like lost but you could probably run through these all on your own anyway but anyway let's just do this windows ok so first we're going to get this and I'll explain kind of what these are we've got apache for running servers my sequel's for the database we want both my sequel client and the server I did want to continue yes and that's going to go through and then we're going to get this other thing here well wait I guess for this oh we're going to set up the my sequel root user not to be confused with the root user of your server so you're if you're using the root user of your server that's different than the root user of the my sequel server ok you can make the password the same if you want probably not the best idea though so keep that in mind so anyway I'm going to set this password I'm gonna set it again done ok don't forget that password by the way so that's my sequel that's what the database that we're going to be using there's also SQLite I don't really feel I it's great for kind of local versions or a really light versions of a database but I don't really think it makes a good production database so what's the point in using it in development I was taking a lot longer ice with it this whole thing like upgrade took forever and this took forever anyway that's done now we're going to go ahead and copy the next command so copy right-click and uh go and what was this is a Lib Apache mod at WSGI this so we can use WSGI it's honestly I don't understand WSGI very much we'll talk about this WSGI in a little bit uh okay we didn't need to do that that was already done um but mata WSGI is basically the way that we're going to be able to actually use flask as a developmental framework and so it's really helpful to us but it can't there's a couple of bugs that come with not really bugs but I don't know I'm not really sure what else to call them but they're pretty confusing and no one's ever been able to give me a good answer on mod WSGI so anyway we'll talk about about those when we get there though but yeah it gets kind of confusing anyway we've already enabled so now we want to CD into 4ww which was created when we installed right up here for Apache and stuff so if you were going to create say a PHP website that would have been the same line that you ran and you would store all your files there anyway uh let's see D and devour www now and just for the record we were in et Cie SSH so let me go back there just in case I'm sorry if you guys are you know pro terminal users but for those of you who are not if you start with a forward slash write that go it basically treats it like the beginning like the root of the directory there's a lot of uses of the word root so it can be confusing but what if we were in ETCs SH and we so when we did VAR w w we actually change directory into the very beginning so let's say you're on Windows and the very very beginning is C colon slash right and then you've got a bunch of directories so that would be as if we went all the way back to the root of C colon slash but then what if you're in ETCs SH and we instead said CD bar without a forward slash ww-well it would look for a directory called var within a TC SSH and it will probably say it doesn't exist so keep that in mind those slashes are very important so anyway going back to the bar www what do we do well first we're going to make dira this flask app that makes a directory called flask app easy enough now we're going to CD into that flask app directory now we're going to make dur another flask app let me scroll down a little bit here and again we're going to CD into flask app so changing directory now we're going to make two directories in here we're going to make first static and then we're also going to make templates and now we're going to sudo nano init PI and that basically is going to open in it but PI it doesn't exist so it's going to create it temporarily at least so we create it here we are and now we're going to put some starting code into in it dot PI so let's just copy this code copy right-click and the code what it's doing from flask import flasks or importing flask we're specifying the name of our flask basically our application for flask and assigning it to this app bar then we're saying the app root this is how this is basically the URL to get to this page that we're going to define right here so this is the route to it so basically the home page which we've called the home page and it returns hi there how you doing so this is the code that's required to get us there app root is required to be just like a proof you can call this function anything you want as long as syntactically and grammatically correct all right I guess I'm tactically is the only word doesn't have to be grammatically correct we call it home I don't know anything you wanted anyway in the return this normally can return things like templates so HTML templates with Jinja templating and some crazy stuff which we'll get to but right now she's just going to return a string of hi there how you doing then finally if name equals main we run the application so hopefully we'll go to the the address and it'll run in the basic series we actually made the address an actual domain I showed you guys how to do that in this one I'm not really going to focus too much on on the domain if you really want to know check that one out it's kind of a longer process so we're going to actually just utilize the IP address of our server that will get you there you guys will be able to actually look at this server so long as it's up before I nuke it I'm not going to always keep it up because I don't think it's a good idea to have two identical websites up anyways so anyway but anyone can access it including you you'll be able to access your own website via the IP address and the address bar anyway that's that control X again why to save enter because that's the same name done we've created our init dot pi now we already ran the update and upgrades so we don't need to do that and now we're going to use sudo apt-get to install pythons pip module yes and that runs and then after that we're going to install virtual environment and this is kind of like a way to have Python in literally a virtual environment and that's where we're actually install flask and run all this stuff from flask it's kind of a safer ish way to do things and just a smarter way to keep you know your your webserver separate from your your real server so anyways pip install virtual environment done now we're going to create a virtual environment that we're calling them and that's it now we can activate that virtual environment if we want and then to deactivate you just type deactivate but first we want to pip install flask in our virtual environment well that's okay it's done and then now we can do sudo python init dot pi so do that real quick and as long as you don't get any errors it's running but as we can see it's actually only running locally so whenever you see one 27001 that's a local IP address and basically it's just your home so that's no good for local you know web server but if you were say your say you're following along on a Raspberry Pi you could pull it or any Linux based operating system or I guess any operating system that you've gotten this far on you can visit one 2700 0.1 : 5,000 and there will be your website but we're going to continue moving on so we can actually view this anywhere so ctrl C to break that and then we're going to type deactivate and now you can see that we're not no longer in the virtual environment because we don't have this vem in front of our route there now what we're going to do is we want to set up the flask configuration file so that's what this line does so copy paste here we are and again we're just going to copy and paste all of this code here no sense in writing it yourself I don't know anybody who would right-click besides the original person who wrote this I suppose virtual host port 84 HTTP and this is the typical stuff server name now for server name that's where you're going to put the IP address to your actual application so let me pull this up here real quick I forget what the IP was but I can get it really quick you can also use ifconfig that will figure that will give you your IP my IP is the following so for server name if you had a domain you would put your domain name right there so on Python program I met for example I have Python programming dotnet also if you have say for example you want to be accessed by a Python program net and say wwwp I thought program you'll just copy paste and add WW here so I'll show you that in a second but anyway so python program net that's not what we want here for this development server my IP is 104 236 44 192 put your IP there then if you want you can add an email here it's like an email for like if you have like a 500 error you'll see that email otherwise that's it unless you change the directory names where you stuff to the flask app you don't need to change any of this stuff here but if you did change those names otherwise this isn't going to work and again so like if you did want to do wwl just show it really quick copy come down paste and then literally add the W double you either I'm not really sure if it'll work with an IP but I assume it would we'll just leave it there just I'm curious I suppose so control X yes to save and we're done there now what we're going to go ahead and do is sudo a two so Apache to enable the site that looks like it didn't do anything let me just right-click copy that one more time done it's enabled we'll run that reload that it's asking us to reloads it now we need to set up the WSGI file so we're going to CD VAR w flask app so the initial flask app sudo nano this flask app WSGI and now we'll copy all of this copy paste interesting this isn't can I swear in the past this looks like a you know treats it like it's a string anyway delete what's there for now your secret key and add your secret key so just for now put like a little jumbly of letters here okay just something that's that control X yes to save and I suppose before we go this is basically this is the WSGI again w s-- g is is so you can actually have flask as your back-end WSGI is heck confusing to me uh-huh so if you understand WSGI congratulations na me so anyway save yes it's like driving a car right you a lot of stuff you can use it and you don't need to know how an internal combustion engine works to get the utility out of your car you can do some cool stuff with WSGI and of course not knowing too much about it one of the problems that arises is what i'll show you guys later as far as every time we make an edit we have to restart apache you can make a script that reads every time you make an edit and restarts apache for you but that is the same thing as just doing it yourself I'd rather have something I don't know built into the WSGI that works I had someone suggested to me I'm right over my head so anyway moving on so here now we'll restart Apache that restarts the web server you'll be doing a lot of this for the reasons like I just said and then now let's do I've configured ready forgot the IP already where's it here so our internet address is this so now you should be able to your web browser and literally type that in so 104 236 44 192 enter and you should see hi there how you doing again you by the time you guys are watching this tutorial that won't be what's here on this IP I might not even have this server anymore eventually I'm going to nuke it and get rid of it I'm just using it for the tutorials but if you were around right when I was making this you too could visit this domain or this IP address and you would see my website also just curious so we can do the www I think the problem is because of all the periods but if you had a regular domain that would work anyway uh that's it for this series or this video the series is not over I promise ya here you guys go that's how to make a website done don't forget to like anyway that's not it that's it for this video if you guys have any questions or comments please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
In this second part to our practical Flask with Python web development video series, we cover the setting up of a very basic website.
sample code: http://pythonprogramming.net
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Workflow Automation
View skill →
🎓
Tutor Explanation
DeepCamp AI