Hello, and in this lecture, you will
build your first website with Flask, if
you haven't done so already, and Flask
is a Python framework that has all the
tools and the templates and the
functions for you to build the websites with
Python, and before going deeper into
Flask and explaining you how to use
Flask probably for building Python web
apps, I would like you to spend 3
minutes to build a very basic website
with Flask. And that is done with only
seven lines of Python code. So that's
what we'll do right now. To build a web
app with Flask, you need to have Flask
installed. So
[No audio]
pip install flask should
install Flask in your Python environment.
[No audio]
Alright, that was successfully installed.
And let's create our website now. What you need is a
Python file. So I'm creating one here,
let's say script1.py, and then
here is where you write the code that
will build your web app, and everything
starts from importing Flask, and
actually, you don't import the Flask
framework, but you import the Flask
class from a Flask framework. So from
flask import Flask, and Flask is a
class of this library. And this contains
all the prototypes that you need to
build web apps with Python. So this will
handle the requests for you. You don't
have to set up requests yourself. But
let's focus on this example for now.
Once you do that, you need to create a
variable where you will store your Flask
object instance, your Flask application,
that will be equal to the Flask
class, and you pass the name variable in
there, just don't worry about that right
now. Then you need a decorator, route,
and here is the URL where you view your
website. So this '/' here means the
homepage and just after that, you need
to write the function, the Python
function, and this function is what
defines what your webpage will do. In
this case, we will return a string.
[No audio]
Let's say a string here, Website content goes
here. Just after that you need two more
lines of code, and that's a conditional
statement, if name equals string main
[No audio]
app.run debug equals True. That's
it. Don't worry about this. Right now,
just click save, open a Terminal and
execute your script with python script1 Python, yeah.
[No audio]
And it looks like your
web app is running now. So open the
browser, and your website should be
live on your localhost.
[No audio]
Now that's it. Nothing impressive, but you see how easy
is to create a website with Python.
And in this case, we are just returning
a string here to the website. So
that's nothing fancy in there. But later
on, I'll explain to you how you can
return HTML pages instead of plain
Python strings to your website, that will
allow you to actually have different
formats, different fonts on your text.
And then you can also apply CSS styling
to your HTML, and so you'll have a
complete website with modern features.
For now let's make sure you understand
these seven lines of code. Yep, it's
seven lines. As I said, the first line
you are importing the Flask class object
from the flask library. Then in the next
line, once you have imported the
Flask class in here, the Flask object in
other words, you are instantiating that
class, that object. And this here is a
special variable that will get as value of
the name of the Python script. Now when
you execute a Python file, a Python
script, Python assigns the name main, this
string to the file.
[No audio]
When you import a
script into another script, like if we
import this script from another script,
this script will be assigned the script1.py
name. But when you execute
the script, Python assigns the name,
'__main__'.
So what happens is that, in this
line, we say if name equals to main,
then run the app,
[No audio]
and this is true
when we execute the script, as we did
here. But if we imported the script
from another script, this line here would
not be executed. So this allows us to
have control over this script using this
line here, then we have these two lines.
This is a decorator. Now the output that
this function produces, will be mapped
to this URL. So if you want, you can
change this URL to this, for example,
save the script. Sure, you need a new
URL here. Save the script, run, and go to
the localhost, reload, and this time
you'll get an error, Python couldn't find
this page. But it can find the about
page. Yeah, that's it. If you still want an
homepage, you would add another
decorator and another function just
after the decorator.
So just leave this, the homepage,
[No audio]
and just say homepage.
Here, save. Again, we have an error
because we cannot have functions with
the same name. So we can change this to
about, the name doesn't matter. So this
doesn't have to be the same with this one,
click Save, run the script again,
[No audio]
and go to the website. This is working.
Try the homepage, and this is working
too. If you want to change something,
let's say About content goes here,
you save the script,
and the web app will
restart by itself. So you don't have to
close the web app and run the script
again as I did earlier. If you get an
error like I got previously, the script
will stop. But if everything is alright,
you will not get an error. So
[No audio]
try this again. Homepage is fine. The About page is
fine too, and with the new content. So
that's about it. In the next lecture,
you will learn how to return an HTML
template, an HTML file using your Python
functions that will give you full power
to publish anything that you want in
your website and not just plain strings
as we did in this lecture. So see you
in the next lecture.