Oh, boy.
Wasn't that last lesson super, super fun with all that installation and set up and errors.
Hopefully things will be a lot better as we move along.
That was, I would say, the hardest part.
We're back here at the FLASK Framework.
And the next thing we want to do is we want to go to the quick start guide.
And I know I could just code along and show you exactly how it is, but I really want to show you the
process of how you work as a developer, because once you're done with this course, you're going to
be out in the world and you should know how to learn something new.
Let's say at the next job, somebody says, Hey, I want you to use Django instead of Flask.
How do you learn it?
Well, you read the documentation, so I just clicked on the quick start because I just want to build
a server really, really quick.
And you see here that the minimal application is, well, it looks like this, so let's just copy the
code from here and we'll talk about it line by line.
Don't worry.
So I'm going to close off these index files, the script files and the CSS files where in our virtual
environment, let's clear this and we can actually just create a new file.
So in our web server, let's just say new file and I'm going to say server dot pie.
So we have a new Python file and we're going to copy and paste that code.
So we're installing or we already installed flask.
So I'm going to import flask from flask.
And then we do this funny thing called app equals flask and we use the flask class.
To instantiate an app.
And we do this underscore underscore name.
Now, when something is confusing, I always like knowing what's happening, so I'm just going to run.
Underscore underscore.
Name here and actually just print it up so we know what it is.
And for now, let's just comment out the bottom part of this.
Just click Save.
I want to find out what this name is.
So let's see if we can run this file.
I'm going to say Python three.
And simply say server dot pie.
Oh, and we're in the desktop right now, so let's seed into web server.
Let's clear and run that again.
So Python three server pie.
And there you go.
So our name in this case is Maine.
And why is that?
Well, because this is the Maine file that we're running.
This should really be a surprise, right?
So we've created an instance of a Flask app.
Or set up our flask.
And this is our app.
And when we run it from our command line, it sets the name as the main.
So that's the name of our app, essentially.
All right, so we've created our app, our flask app.
And then.
We see that we're using.
What is this?
This is a decorator, right?
We learned about this.
Again, one of the cool things about frameworks is that they give you what we call a higher level of
abstraction.
That means we don't necessarily need to know what this is doing, what the code is doing underneath.
We just know that this app dot root decorator gives us some extra tools for us to build a server.
In this case, it's going to say any time we hit slash or what we call the root, I want you to define
a function called Hello World and Return Hello World.
But let's go back to the documentation.
Now, one thing I want to note here is that when you call your Python file, make sure you don't call
it Flask Pi, because this would actually conflict with Flask itself.
So name it Server Pi or hello dot pi.
Whatever you want, just in case you get that error.
Now the next part is to actually run our application, our flask application.
Now, there's one other trick here that we need to do.
We need to run the flask.
Run command.
So Flask also gives us a command line run to actually run our server.
But we also see here that we need to export.
Let's make this a little bit bigger.
We need to export.
Flask app equals to hello pi.
And as you can see here, we just need to give it that environment variable.
So flask app just like this.
And if you're on Windows or partial, it shows you what commands to run again.
I'll link to this resource so that you can copy and paste as well.
In my case, I'll just do export.
So copy and paste here and a set of Hello Dot Pie.
I'm going to name it Server Pie.
And then we can do flask run.
Now another option is to actually do Python dash and flask run.
But since we created a virtual environment, we should be able to run.
Flask run.
All right.
We get a little warning.
This is a development server.
Do not use it in production deployment.
All right, that's good.
We don't need to worry about because we're not doing production right now.
This is just something on our computers.
And you see here that it's running on HTTP 1 to 7 .0.0.1.
This is the standard.
It's what we call a local host.
This is the URL or the address of the laptop I'm working on.
And for you, that's the same thing with Port 5000.
So if I copy this.
And open this up.
What?
Look at that.
Are you kidding me?
We just created our server with Hello World.
Where is this coming from?
Well, it's coming from this, right?
The slash says, hey, any time we do the root directory.
So this is like saying here, slash here, any time the browser requests for this endpoint, it's going
to hit our server and look at that.
It's actually logging out that 1 to 7 .0.01, which is us.
We logged this entry.
The app root is going to say, Hey, hey, somebody needs some of our resources, some files, some
data.
What should we do?
Well, we have this hello world function that we're going to run and we're going to run by returning.
Hello World.
That's right.
We just created our very first server just to prove that I'm not making this up.
Let's put your own name on it, refresh and oh, I'm refreshing and it's not working.
And that is because you see here that I'm actually making these requests, but the changes haven't been
saved.
We actually have to do control or command.
See, let's clear and run the server again.
So now fy refresh.
Look at that.
Now.
That's really cool.
And I also want to show you that see here how it says Serving Flask App Server Pie.
Remember when we ran the command?
This where we used flask app equals server pie.
This is what it's saying.
We are able to do what we call an environmental variable and set flask app, which is going to get displayed
here a server by.
So I could have called our app whatever we want.
So if I go back here and just say.
Super duper.
Super duper and I run.
You see that my flask app is named Super Duper.
Now, the other thing I don't like is the fact that every time I make a change, I have to restart the
server.
And you can see here that the debug mode is off.
I can actually turn on the debug mode.
By doing something like this.
Once again we use the environment variable and if we go to debug mode in our documentation here, you
can see here that if we do flask EMF equals development.
Which again instead of the flask app will say env equals development.
And I run this again.
You see that I'm now in the debug mode on.
This is something that we use again, just flask specific to make sure that it's in development mode.
And what this does is watch this as I change this and save it.
I go back?
And refresh.
Oh, and I get an air again.
This is one of the cool things with the debugger.
So if I make this a little smaller.
I get an error saying hey could not import super duper.
Well, that's because I just changed the name from Super Duper, but it's actually trying to import
this server pie and well, we should probably name it server pie because it doesn't work.
I only changed it just to show you what it does so we can actually fix that.
Again, the debug mode, as you can see, gives us a lot of information.
So I'm going to close the server.
Fix my silly name from super duper to server dot pie.
All right, we're all good.
And then we're going to flask run.
All right, let's go back and refresh.
And there you go.
Everything is all good.
But the cool part is that now if I change this to just.
Hello, save.
Notice how the server notice the changes.
You see over here, detect it changes and automatically change things for me.
So if I refresh, look at that.
I don't have to restart the server and everything is working awesome.
We have our basic server but we still have a bit more to go because we want to get to the point where
we can actually serve important information like HTML files, CSS files and JavaScript files.
Let's explore that a little bit more in the next video.