Flask Hello World: Setting up a python virtual environment (venv) in VS Code & installing Flask

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'd like to set up a virtual environment I'm going to use code and python I'm going to set up a python virtual environment and then within that virtual environment I would like to install flask and then run the hello world of flask so I already have uh some python things in my code so I have I'm looking here I have Visual Studio code I have the extensions open and I have uh at least three sort of python related uh extensions for that and then that was just sort of I ran a Python program it said do you want the python extensions I said yes there are more but this was sort of the ones that sort of popped up sort of automatically and I just said yes to them so that's where that's my starting point and I've made a new folder in documents flask demos and it's empty so that is that's my starting point uh in terms of the a file system and in terms of code so going to open to that folder documents and where was that flask demo there we go flask demo all right so and I will open a terminal this needs to be a little bit wider this terminal on my menu and I'm on a Windows machine okay and my terminal is right now open to flask demo because code was open there okay and I'm going to type the command python minus m v e and v e and V so I'm setting up a virtual environment that's going to be called v e n v and that's what this command does and I enter it at the terminal and I'll also sometimes say command line I've seen uh somebody I work with has a Mac and I think for him it's python3 instead of python so this is like running some code and establishing a virtual environment for python so I'd already set up again I had already set up code so then it knew it I have python on this machine and it knows uh and my code Visual Studio code knows where python is and I had some extensions with that python okay and then I ran this command and now it's set up uh closing extensions but opening my Explorer and here I am in flask demos and it's set up this folder v e n v and put some various code in there so one thing I'd like to do is move into the ve and V so I'm going to do that by uh the terminal again that I'm going to use the CD change directory command and CD space V and V and enter that so now I'm in the v e n v then I was established and I would like to activate this virtual environment so the purpose of the virtual environment is that within flask I'm installing flask and then I may install other things and on top of flask and the virtual environment allows me to control like terms of versions and what's installed to to limit it to the virtual environment so that is the purple purpose of the virtual environment you don't need a virtual environment uh for flask it's just a way to to limit the sort of like existence and knowledge of flask to the virtual environment but by no means necessary so you could um if you're having trouble with a virtual environment you could sort of skip these steps and just say well I want flask everywhere and then go to the part where where plastic is being installed okay so within scripts there is an activate and the one that's going to uh run for me is the Powershell one I'm gonna Powershell v e and B here and um so I want to run under scripts activate and for me uh that would directly work but in case you're on a machine um which doesn't have Powershell permissions uh quite set up I wanted to show that so I'm going to search for Powershell I'm going to use the ISC and I'm going to open it right click and run as administrator so I want to be the administrator of the Powershell so there that is popping up okay and then I wanted to uh write this command and just if I'm typing up here Powershell it's going to give me some like understanding of help me with the commands set execution policy so I'm getting some code help here set execution policy and I'm going to uh set that to what's called remote sign so this is going to sometimes your the Powershell system is set up that you sort of can't do Powershell things that it's sort of blocked and so I'm giving sort of some permission and I'm going to uh and sometimes like this is my own machine so I can do what I want but sometimes you're you're in you know in an environment where you have to uh you can't set the execution policy for everybody you just have to set it for yourself so I will show that so we can control the scope of this setting the execution policy to I'm going to do it to the current user so set execution policy remote sine minus scope current user and I'm going to then run that script and I'm going to say yes okay so I've already done something to this effect anyway or already had more permissions um but this may be necessary in certain environments too set the execution policy and limit the setting of the execution policy to sort of a user okay so now I'm ready to activate my virtual environment and that is to run this PS1 that's the Powershell I'm going to run this and it's in scripts so I'm not going to move into the scripts folder because I'd have to move right back out but I'm just going to start with scripts to say when I'm running is inscripts and it's activate and typically um I mean windows it's typically not case sensitive so I can just say activate but if you were in a machine like Linux or something that were case sensitive one it wouldn't be in scripts it would be in a folder called bin um and so back limit Linux uh Ubuntu things like that you're going to instead of having a folder called scripts you're going to have a folder or directory called bin and that's where activate is going to be and you're also going to then you also might have to worry about case sensitivity I'm in a Windows machine I won't have to worry about that and I'm going to run uh this Powershell version so now we see the green v e and v in front and that is my indication that I have activated my virtual environment okay so I have I made the virtual environment and then I've activated it otherwise it's just sort of a folder with with some files in it but not uh not really working as a virtual environment unless it's active Okay so now what now we're ready for the flask so I'm going to pip install flask in within the virtual environment so I have the virtual environment and activated so I'll pivot stalling flask there and so this installation that's the purpose again of the virtual environment that the installation is within the virtual environment so now I have Pip install within the virtual environment and uh what else did I want to say if you were on a Mac again I think I've sometimes seen it be Python 3 and I've sometimes seen it be like I think tip three okay so now we have the uh virtual environment and it's activated and flask is installed so I want to be at the virtual environment and I want to make a file a python file that's also going to import flask and write some flask so a new file I'm going to say I think I'll call it it'll be my hello worlds I'm going to say Hello dot python and here I am and again in let me close up script so the hello python is in the virtual environment and now we want to write uh some code I'm just going to keep it pretty simple um from flask import flask and the first flask is small letters and the second one is capital let capital F flask and then um and I've seen sometimes like for me it's not happening that the little flask the little F flask is um not under not squiggly underlined for me but I've seen it sometimes be squiggly underlined which is usually an indication that there's something not quite right it's not being completely understood and yet I've seen it work so don't don't assume then it's not going to work if you have the squiggly underline it may be um it it may be fooling you and it may work so I'm going to um just sort of establish that there's some there's going to be some app it's going to be a flask app and then what are we doing uh within the uh so flask is going to allow me to sort of have python uh work as a server-side language so it's going to sort of set up some routes some some like HTML routes and then I will be able to look and control web pages sort of behind the scenes on the server side with python and flask there are other things that do the same job like the Django um and the Django's sort of larger and does more but it's sort of everything but the kitchen sink kind of a thing um and so what I like about flask is it's good pedagogical you could do a little bit add a little bit more a little bit more sort of get a feel for what you're doing before you have boom all all the whole world of things so again I'm just making a little Hello World app I'm making a function that's going to be sort of called when you go to this route and it's not even going to be uh an honest to God uh HTML page I'm just going to put some text so I'm just going to throw some text out on the page and since it's in since it's making a web server and if I just had a simple text file then it would display sent text so that's what we're expecting to see so this is sort of simple enough this should be enough let me save it so I have a very simple hello world uh flask program here and there are different ways to run this um and but I'm going to run it from the command line and there are different if you're going to run it from a command line there are different uh ways there's a Windows command there's a Unix uh Linux Mac kind of approach I'm going to use the the Powershell version here so I'm going to uh there's now in a sort of environment variable associated with flask at least within this virtual environment and we're going to set it to hello and that is seeing this here hello.py so I could have other other python files here and this is telling me that this is the one that I'm going to when I say run flask this is the one that's going to run so I'm establishing it as the flask app so when I say run flask it's the one that will run right now it's the only one I have but uh later on we could have more python files and but this is the effectively the main this is the one that we're going to flask we'll start with so that's just establishing an environment variable uh that I can change so the hence the name variable but now I'm going to run it flask run and now seems to do nothing except you know it says down here running on uh HTTP so it's set up um a web server a virtual web server it's at uh this sort of localhost with a port of 5000 and I it's telling me control C to get out when that time comes but it's also this uh is if I highlight this URL it says for me Control Plus click if I wear Mac it might be a command click or it might be a control click I'm not good with Max to know when it's control and when it's command and clicks but it's giving you a hint here and I would give you the hint of the max so you know follow the hint um so control click and it's brought up uh for me in terms of uh the default browser at least as far as Visual Studio code is concerned for me for my visual studio code is Edge I've never changed it that's what you know they're both Microsoft so they like to work together um and and I liked it that way I like you know when I'm working I'll tend to work uh when I'm browsing myself I'll tend to browse in Chrome but I like when when I'm testing something and having a browser be called I like to be a different browser so I like it to be itch but anyway I'm rambling here is the web server page uh and it's just you know there's no uh code behind um it's really just text and later on we'll can make an actual HTML that's what I wanted to show you in this sort of a Hello World of flask so we made a virtual environment with we activated the virtual environment we installed flask within the virtual environment and we did sort of a very simple hello world within flask and ran it and now that I'm done looking at hello world uh control C2 control scene to get out of it okay that's what I wanted to show you in this video thanks for your attention
Info
Channel: Thomas Blum
Views: 1,926
Rating: undefined out of 5
Keywords: Python, Flask, VS Code, Code, venv, python -m venv venv, activate, PowerShell, PowerShellISE, Set-ExecutionPolicy, RemoteSigned, -Scope CurrentUser, pip install Flas, route, virtual server, localhost, environment variable, FLASK_APP, flask run, terminal, commandline, command line, Hello World, port, control click
Id: -RE7grqHYXg
Channel Id: undefined
Length: 17min 58sec (1078 seconds)
Published: Fri Jul 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.