Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Create the Flask app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to finally start writing some Python code. We've been coding over in Twilio studio, and now it's time to write our Python code.
0:09 And this is really, really exciting going to be one of the funnest parts of this course for sure. It's a good chance to remind you about the GitHub,
0:15 repository over in chapter three. I've exported the JSON definition of what we've done so far for the workflow over there and sort that JSON file here.
0:27 Be sure to go and get the GitHub repository fall along now in this particular chapter Chapter 4, I'm going to walk you through.
0:38 Setting up the project in PyCharm is really, really close to what you would do in Visual Studio Code as well and get everything
0:45 ready to go. And then afterwards, Chapter 5,6 and 7, I'm just going to say I opened it the same way Okay, so this is the one time well,
0:53 sort of really step by step. I go through it, for our project. We're going to need to use Flask no surprise for our flask app.
1:02 And whenever we have external dependencies like that, it's a really good idea to have a virtual environment.
1:07 This is an isolated environment that you can install stuff, too, and only the things for this application and it's dedicated versions will be installed
1:14 Gonna go over here, have a little cool app called Go to Shell that will open up the working folder there.
1:23 And what I want to do is I want to create a virtual environments will say Python3 -m venv, That's the commander of the module,
1:30 and then venv is going to be the folder name. Now with this folder, we have to activate it. So we'll say .venv/bin/activate, now on macOS and Linux.
1:42 This is what you do on Windows. You would just say venv/scripts/activates or activate.bat.
1:49 Why those are different? I have no idea you're in the windows. Just be aware. You got to do that as well.
1:53 And you may also have to type potentially could type Python3 Python. Just make sure you're using the right version of Python there,
2:00 and then we need to have a file where we define our dependencies. Don't need to create a file. Just use touch or we could use in our editor.
2:07 I'll just go and do it here for the first time and then do the rest in the editor requirements.txt We're also going to have a file called app.py.
2:17 So Windows you can just open this up in pycharm and create those two files I don't think touches in Windows these days, anyway. So now over here,
2:27 we've got our application file. We've got our requirements and a virtual environment. Let's go ahead and open this in pycharm on macOS.
2:34 You can drag and drop this onto the dock icon and will open on Windows and Linux. You have to simply say file open directory and browse to it.
2:44 You see, it's a founder virtual environment. It's colored it and then ignored way. So that's good. And it's got are two files here.
2:52 It also automatically discovered our virtual environment, this one that we had created. If it doesn't do that, you can just go over here.
2:58 Sometimes it doesn't say add interpreter and browse to it, but let's go and set our requirements first. We'll say flask and says, Oh,
3:07 look, you're going to need to install flask. So in pycharm, I can just click this button and frequently I will.
3:13 But this one time we'll go down to the terminal notice the virtual environment should be active and we'll say pip install -r requirements.txt
3:20 How about we try it with two l's There we go Now, We always want to recreate a virtual environment. It's almost always out of date.
3:32 The pip command is out of date, which is unfortunate, but we just run that little thing to make sure we don't get warnings again. Right now.
3:40 We should be good to go. Take a moment for PyCharm to realize that was installed. And now it is. If you worked with flask before,
3:47 it's really easy to create a minimal flask app. So we'll just say import flask. The way it works is we create this thing called an app,
3:54 and we'll say flask.flask(__name__), pass in the name of the file Here, we're going to define a function.
4:00 I'll call it Index that's just going to listen to / and it will return Hello world. Now, this is not enough to make it an in point that
4:10 Flask will host or the Web browsers can talk to. We have to tell flask, Hey, this is part of somewhere URL's we say app.route ('/').
4:18 That means just you go to the server or domain name. That's what we want. All this is great, but it's not enough to actually run the program.
4:26 So we're going to use the Dunder named dunder Main convention, which is only do this command if you're actually trying to run the file,
4:33 not trying to just import it and pycharm happens to know that this is very common. So every type main tab it will write the code to say,
4:41 Am I being run directly or am I just being used in other ways? So if that's the case, we'll say Run and of it goes.
4:47 Final comment here. Notice at the bottom. There's like these little comments. If I format, the code will do one thing by going cut a line out.
4:57 It'll show you what command that was and so on. So you'll see me using hot key's and not just the menus throughout this course,
5:04 and those over here will show you what the command is that I used what the hot key was if it was a hot key, or even if you click a menu item.
5:13 If I click this even though I click the menu item, I still get to see the hot key pop up there. So if you're wondering what's going on,
5:22 be sure to keep an eye on the little green section down there at the bottom Last thing to do is to run it,
5:27 see if it works. Now there's a cool button right here that looks like she's going to click it and run it and even shows me if I had control+r
5:32 it'll run it. But there's nothing set to run, so we're gonna choose that we're gonna run this app. Then it runs down the bottom.
5:39 It looks like it's up and going, and subsequently I'll have the controls to run this app. Click on it and Hello world not super impressive,
5:48 but it is kind of impressive, how simple this app is that we've got here.
5:53 Get everything set up. So just 13 lines of code with all the proper spacing in there and we have our hello world flask app, up and running.


Talk Python's Mastodon Michael Kennedy's Mastodon