All right. Let's get started developing websites using Flask.
Now Flask is one of the most popular web development frameworks,
but what does it mean to be a framework? Well,
we've seen libraries before like Beautiful Soup. Tools that we can use and tap
into whenever we need a particular piece of functionality. Now,
a framework is a little bit like a library in the sense that it's a package of
code that you didn't write, but it also got some differences.
So here I've got a graphic from the differencebetween.net and one of the
biggest differences is the fact that a library is something which you call upon
to do something specific.
Whereas a framework is something which you have to abide by their rules,
you have to use their architecture.
And when it comes to triggering some sort of functionality,
it's the framework that calls upon your code.
So what does that mean? Well,
remember when we used the requests library and we would say something like requests
.get and then we would tell it which webpage to go and fetch.
Well,
this is us tapping into that library and then giving it a command to do
something. Now, when we're working with a framework,
it doesn't really work like that. We don't really say, hey framework,
do this thing for us. Instead,
we make sure that our code is defined in a way that the framework wants it to
be. And then when the time is right,
the framework is actually going to call our methods
instead of us calling the framework.
All we have to do is to plan ahead for certain situations.
If it was this situation, what do we want to happen? If it was that situation,
what code it should be run?
So it might be that all you have to do is just write some functions like hello
world and when the user goes to the website,
then it's going to display this text Hello world.
Let's try this in practice. Now by
now you would of known that as developers,
especially now that we're in the advanced stages of becoming a Python developer,
the most important thing when we approach a new tool or a new technology is to
read the documentation.
So Flask actually has a really good piece of documentation in order to tell us
how to get started quickly using Flask. As you can see,
they've described what a minimal Flask application looks like.
And this is a very simple server set up with flask.
So let's go ahead and copy this code. And I'm going to create a new project
which I've decided to call hello_flask. And then in this project folder,
I'm going to create a new file which I'll call hello.py.
Now it's important that the name of your file does not conflict with the name of
the framework or library in fact. So for example,
if I was to create a file called requests.py
and I had, um,
imported the requests library.
And then I tried to do something like requests.get.
Now when I run this,
because the name of my file conflicts with the name of the module, what will happen is
I will get this error. And it might be a little bit confusing
cause it says, well, request has no attribute get. Well
we know it does because it's in the documentation.
And the reason why we're getting this error is because we've named this file
the same name as the module.
So this is something that happens to a lot of people
so make sure that you don't commit this mistake and you could name it anything
like main or server or hello in our case. Notice that now
if I change the name, it actually automatically changed the import
which is not what we want. We want the import to be requests
and we want to make sure that we install our package requests.
And once that's done,
you can see our module and our file have completely different names.
So now, if I run this file,
it works perfectly and it ends with exit code zero,
which means everything was successful.
So now I'm going to go ahead and delete this file
and go back to our hello.py. Now inside our hello.py
I'm going to paste in the minimal Flask application from the Flask
documentation. So paste that right here.
And as you might remember,
there's several ways that we can install new modules. So in this case,
the module is Flask and we can either click on the red light bulb to install
this package. Alternatively, we can go to PyCharm, preferences,
and then go to our project and into the interpreter to add the Flask package like
this.
1: Right.
0: Now, if you have the PyCharm professional edition,
there's additional tools to make it easier to install Flask into a project and
to work with Flask projects. But because we're using the free community edition,
I'm just going to go ahead and dismiss that popup.
So I've shown you two ways of installing a package.
Now I want to show you the third way,
which you might come across if you're looking at the installation.
So notice here, in order to install Flask,
they're using pip install Flask.
So pip is something that allows us to install Python packages from pypi.
So any package that you on pypi.org, you can install using pip.
And what we have to do is to type in the name that you see right here.
For example, if we want to install Flask,
then it's pip install and it's the capital F for Flask.
So we can copy that into our clipboard.
And if we go to the terminal tab at the bottom,
we can paste that instruction in here and hit enter.
Now it should have successfully installed Flask . And once I dismiss that,
you can see my errors go away. So all three methods work.
I personally prefer the red light bulb, just because it's much quicker.
It's just two clicks rather than typing a lot of code. Now,
once we've installed Flask,
we can go ahead and add some space to get rid of all the warnings from Py
Charm. Um, we can go ahead and run our project.
Now, as soon as I run the project, you can see
it tells me the process was finished with exit code zero,
which means everything was successful,
but where's my server and where's my website.
So let's go back to the Quickstart to see what we actually have to do in order
to set up our server. So to run our Flask application,
we're going to firstly, need to export a environment variable.
So in previous lessons, we've talked about environment variables,
but basically this is going to tell the Flask framework
what is the name of the file that contains our server.
So in our case, as well as in the Quickstart,
the name of that file is hello.py.
So we basically have to point to that file to get Flask
to be able to recognize it and to use it as the server. So on Mac
we're going to type export space
and then we're going to put in the name of the environment variable
which is FLASK_APP and its all caps.
And then with no spaces, we've got a equal sign
and then the name of our file, which is hello.py.
So now when I hit enter,
that's going to add that environment variable under the name that Flask is
expecting, which is flask.app. Now,
as it mentions in the Quickstart, if you're on Windows
the keyword is not export, but instead it's set.
So again, inside your terminal down here, you're going to type set.
And then you're going to put in the name of that environment variable
which was FLASK_APP and then with no space equals and it's our hello
.py. And once you hit enter,
it should give you the prompt again indicating that everything went smoothly.
Now, once we've done that,
the next step is to simply use the Flask command called run.
And when I hit enter, you'll see that it's created my server.
So it's serving up the Flask app that I've created in hello.py
and we're get a warning telling us that this is a development server and telling
us that if you want to actually launch it as a product,
don't use this development server. There's a few steps
you have to carry out if you actually wanted to make this production ready and
ready for the internet and customers. But in our case,
we are developing. So this is perfect.
And it tells us where to go and find our website.
So if you imagine that Google is at https://
www.google.com. Well,
this is the address of our website and it's being served by our computer.
So when you're trying to get hold of a website on the internet,
then you have to point towards the IP address
of that particular webpage.
But because we've created our server locally and it's being served on our own
computer, then this is basically the local address
of that website.
Now a neat thing about PyCharm is if I simply just click on this link,
then it's going to launch my website in my browser and you can see right up
here, we've got hello world showing up
which is the same as what we've got here in this method.
So that proves that we've successfully managed to create our very first web
server using Flask.
And it's able to serve up our very first Flask website. Now,
if you go ahead and open up the Chrome developer tools and you go to elements,
you can see that what we wrote here which is just a string
was actually converted by Flask into full HTML.
It's got the HTML tag, it's got the head tag,
it's got the body tag and we didn't have to do any of this.
We just told Flask what we wanted to be returned onto the page
when the user goes to the homepage,
which has just the web address plus a forward slash.
Now the very last thing to remember is once we're done with our server,
because it's still ongoing
it's still waiting for a client to hit up this website,
we have to get it to stop waiting. So we can quit by holding down control and
then pressing C. And you can see this takes us back to our prompt
where we've got our name and our project. Now,
if you don't stop it and you try to run two Flask applications to the same
address then the second one is simply not going to run.
So just be aware that once you're done with your server, hit control+c to quit.
Now in the next lesson,
I want to talk a little bit more about the commands that you can use in the
terminal because this is called the command line.
And just as here we use the command line to run our Flask application, to export
our environment variables, we can also do other things like create folders,
create files, move files around. It's actually a very,
very powerful way of interacting with our computer.
That's what we're going to do in the next lesson. So for all of that and more,
I'll see you there.