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