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