#100DaysOfCode in Python Transcripts
Chapter: Days 76-78: Getting Started with Python Flask
Lecture: Creating your first Flask app!
Login or purchase this course to watch this video and the rest of the course contents.
0:01 Okay with the files created, app.py, data.py, and index.html let's get started. Now the first thing we need to do as with any application
0:12 is pretty much import any of the modules that we're going to use. And in this case it's Flask. Now Flask has so much to it that, you know,
0:21 it's not really that Pythonic to import everything. So we're going to tell our application what we're importing here.
0:29 And specifically we want to import Flask itself, you know, go figure and we want to install the render template.
0:37 Now this is what allows Flask, your flask app.py file to communicate and work with your index.html Jinja2 template.
0:46 All right, now we need to actually create our application, our Flask application object. Now everything that runs in your Flask app
0:57 is running pretty much underneath this app that you are specifying here. Now this name here can be whatever you want, you know,
1:05 so I'm just using app 'cause it's pretty self explanatory. Now, we're going to use this name dunder to say it's this file, it's this app
1:17 that's going to be assigned to this variable here, to this object here all right. Next up, and we're almost done actually
1:24 believe it or not, we have to specify a function, okay, just ignore that decorator for one second, just one second.
1:35 And all right, so this is our index function. Now I've named it index because, it's going to be the function that
1:44 operates the route directory of our website. The forward slash of our website, the home page of our website. However you want to phrase that.
1:53 Now the app.route decorator, what this does is it assigns this path, this route according to Flask terminology.
2:05 It assigns this to this function so when you access this page, this is the function that's going to run. Okay, so now you're starting to visualize how
2:16 everything ties together, all right. Now what we need to do is we need to return something to send to that page
2:24 and how we're going to do that, well we're going to return. Now if we wanted to make this super simple, really basic, we could do something like this.
2:35 All right, hello world, now what this is going to do is this is going to ignore that index.html file
2:44 because if you, as with anything, we haven't specified index.html anywhere in this Flask code, in this Python code.
2:53 So how does this application.py file, app.py, how does it know how to talk to that index.html file? Well right now it doesn't.
3:01 All that's going to happen when you run this is it's going to print hello world in the top left hand corner of your page and that's it.
3:10 We don't want to be that boring, well, it's going to be that boring but in a different way. So what we're going to do
3:17 is we're going to actually tell our Flask application that when you get to the route directory of your website, you're going to load index.html.
3:31 So now we, anything that this page presents the HTML is going to be in that index.html template, all right.
3:41 You can imagine what we're going to put in there. So now the one last thing that we are missing is the line of code that actually tells
3:52 our program to run. app.run(). Oh come on, got to get that right. This app is this. You can see this variable being referenced multiple times now.
4:10 Everything is linked together via this object here. All right. So app.run() if you don't have that, if you comment it out, it's not going to work.
4:20 Your website's not going to run when this file is invoked, okay. So all that's going to happen now when we run
4:28 the website is we're going to hit the route page it's going to load that HTML file, and that's it. Nothing will, you'll actually get a blank page
4:39 because we haven't specified what to run, all right. So here's our index.html file that we've created. I've opened it up in Notepad++
4:49 and all we're going to do is put some really basic HTML in there. So never fear if you've never worked with HTML. Let's get this really, really basic.
5:01 Going to open the body tag, I'm going to throw a paragraph tag in there and we're going to say, "Hello planet."
5:09 Yeah, let's, let's be slightly different, all right. And close the body tag and we're good as gold. Now let's actually try and run the app.
5:22 To run it all you actually have to do is hop into your command prompt, or whatever you're using, and just from your app.py file
5:31 just like any other Python script, Python app.py. Bang, now this line here tells you the default IP address that Flask uses for your website.
5:42 So it's launched the web server and it's going to respond on local host 127.0.0.1, port 5000 all right.
5:51 Now that we have port 5000 ready, we'll bring up this that, here's one I prepared earlier and we're going to hit enter and that's it.
6:01 How simple is that? So if you think about it, we've just got just a handful of lines of code. So you've got a couple of lines
6:09 in the HTML file, a Jinja2 template, and this and that is your Flask website. So what I'd like you to do now is have a play with this.
6:18 Just play around with it, that's your day. Follow along with the video, run your very first Flask website, and see how you go.
6:25 On the next video, we're going to deal with some actual data. Very exciting.