1
00:00:00,450 --> 00:00:01,000
Hello.

2
00:00:01,010 --> 00:00:04,480
And in this lecture you will build your first Website

3
00:00:04,530 --> 00:00:08,050
with Flask if you haven't done so already.

4
00:00:08,280 --> 00:00:15,120
And Flask is a Python framework that has all the tools and the templates and the functions for you to

5
00:00:15,120 --> 00:00:17,510
build a website with Python.

6
00:00:17,910 --> 00:00:24,630
And before going deeper into Flask and explaining you hold to this Flask probably for a building Python

7
00:00:24,630 --> 00:00:25,430
web apps,

8
00:00:25,620 --> 00:00:33,030
I'd like you to spend three minutes to be with a very basic web site with Flask and that is done with

9
00:00:33,030 --> 00:00:35,950
only Seven lines of Python code.

10
00:00:36,060 --> 00:00:40,710
So that's what we'll do right now. To build a web app with Flask,

11
00:00:40,710 --> 00:00:49,380
you need to have Flask installed, so pip install flask should use the whole Flask in your Python

12
00:00:49,380 --> 00:00:49,920
environment.

13
00:00:53,610 --> 00:00:54,190
All right.

14
00:00:54,200 --> 00:00:59,330
That was successfully installed and let's create our website now.

15
00:00:59,930 --> 00:01:02,720
What you need is a python file.

16
00:01:04,160 --> 00:01:05,830
So I'm creating one here.

17
00:01:05,990 --> 00:01:08,090
Let's say script1.py.

18
00:01:08,140 --> 00:01:15,320
And then here is where you write the code that will build your web app and everything starts from

19
00:01:15,320 --> 00:01:17,000
importing Flask.

20
00:01:17,000 --> 00:01:24,460
And actually you don't import the flask framework but you import the Flask class from the flask framework.

21
00:01:24,500 --> 00:01:33,070
So from flask import Flask and flask is a class of this library.

22
00:01:33,350 --> 00:01:38,840
And this contains all the prototypes that you need to build web apps with Python.

23
00:01:39,470 --> 00:01:41,360
So this will handle requests for you.

24
00:01:41,360 --> 00:01:47,780
You don't have to set up requests yourself, but let's focus on this example for now.

25
00:01:47,900 --> 00:01:50,480
Once you do that, you need to create a variable

26
00:01:50,480 --> 00:01:55,200
where you'll store your flask object instance, your flask application.

27
00:01:55,220 --> 00:02:01,960
That would be equal to the Flask class and you pass the name variable in there.

28
00:02:01,970 --> 00:02:04,460
Just don't worry about that right now.

29
00:02:05,930 --> 00:02:09,900
Then you need a decorator, route.

30
00:02:09,930 --> 00:02:14,580
And here is the URL where you will view your Website.

31
00:02:14,630 --> 00:02:17,530
So this slash here means the home page.

32
00:02:18,830 --> 00:02:22,630
And just after that you needed to write the function.

33
00:02:22,630 --> 00:02:29,240
The python function and this function is what defines what your web page will do.

34
00:02:29,630 --> 00:02:31,940
In this case we will return a string.

35
00:02:34,990 --> 00:02:36,590
Let's say this string here.

36
00:02:37,010 --> 00:02:40,910
"Website content goes here". Just after that

37
00:02:40,920 --> 00:02:43,010
you need two more lines of code.

38
00:02:43,100 --> 00:02:50,620
That's a conditional statement. If name equals the string main

39
00:02:53,900 --> 00:02:54,380
app.run

40
00:02:54,430 --> 00:02:59,630
(debug) equals True.

41
00:02:59,630 --> 00:03:00,600
That's it.

42
00:03:00,620 --> 00:03:02,150
Don't worry about this

43
00:03:02,160 --> 00:03:02,830
right now.

44
00:03:02,840 --> 00:03:11,600
Just click Save, open the terminal and execute a script with Python, script1 Python.

45
00:03:14,850 --> 00:03:19,930
And it looks like your web app is running now so open the browser.

46
00:03:20,640 --> 00:03:24,090
And your web website should be live or your local host.

47
00:03:26,150 --> 00:03:27,200
That's it.

48
00:03:27,200 --> 00:03:33,840
Nothing impressive, but you see how easy is to create a Website with Python.

49
00:03:33,880 --> 00:03:38,940
And in this case we are just returning a string here to the Website.

50
00:03:39,690 --> 00:03:46,520
So there's nothing fancy in there but later on I'll explain to you how you can return HTML pages

51
00:03:46,670 --> 00:03:53,480
instead of plain Python strings to your Website.
That will allow you to actually have different formats

52
00:03:53,480 --> 00:04:00,260
different fonts on your text and then you can also apply CSS styling to your HTML, and so you will

53
00:04:00,260 --> 00:04:04,050
have a complete Website with modern features.

54
00:04:04,070 --> 00:04:09,360
For now, let's make sure you understand these seven lines of code.

55
00:04:09,930 --> 00:04:11,500
It's seven lines.

56
00:04:11,540 --> 00:04:18,410
As I said the first line you are importing the Flask class object from the flask library.

57
00:04:18,530 --> 00:04:19,990
Then in the next line.

58
00:04:20,010 --> 00:04:24,230
Once you have imported the Flask class in here, the Flask object

59
00:04:24,230 --> 00:04:29,670
in other words, you are instantiating that class that object.

60
00:04:29,690 --> 00:04:35,650
This here is a special variable that will get as value the name of the python script.

61
00:04:35,660 --> 00:04:44,180
Now when you execute a Python file, a Python script, Python assigns the name main, this string to the file.

62
00:04:47,780 --> 00:04:51,980
When you import a script into another script,

63
00:04:51,980 --> 00:04:58,780
like if we import this script from another script,
this script we will be assigned the script1.py

64
00:04:58,770 --> 00:05:02,350
name, but when you execute the script

65
00:05:02,360 --> 00:05:08,000
Python assigns the name __main__.

66
00:05:08,420 --> 00:05:20,390
So what happens is that in this line we say if name equals to main, then run the app, and this is true

67
00:05:20,390 --> 00:05:23,990
when we execute the script as we did here.

68
00:05:24,770 --> 00:05:30,710
But, if we imported this script from another script this line here would not be executed.

69
00:05:31,190 --> 00:05:37,480
So this allows us to have control over this script using this line here.

70
00:05:37,620 --> 00:05:40,210
Then we have these two lines.

71
00:05:40,340 --> 00:05:42,770
This is a decorator.

72
00:05:43,010 --> 00:05:49,280
Now the output that this function produces will be mapped to this URL.

73
00:05:49,370 --> 00:05:52,970
So if you want you can change this URL to this for example.

74
00:05:52,970 --> 00:05:57,150
Save the script, sure you need a URL here.

75
00:05:58,250 --> 00:06:07,950
Save the script, run and go to the local hosts, reload and this time you'll get an error, Python couldn't

76
00:06:07,970 --> 00:06:13,760
find this page but it can't find the about page.

77
00:06:14,750 --> 00:06:16,370
That's it.

78
00:06:16,370 --> 00:06:24,020
If you still want a Homepage you'd add another decorator and another function just after the decorator.

79
00:06:25,130 --> 00:06:37,520
So just leave it as the home page and just say "Homepage" here, save.

80
00:06:37,640 --> 00:06:45,770
Again we have an error because we cannot have functions with the same name, so we can change this to about,

81
00:06:45,770 --> 00:06:47,510
the name doesn't matter.

82
00:06:47,540 --> 00:06:58,560
So this doesn't have to be the same with this one, 
click save, run the script again and go to the website.

83
00:06:59,540 --> 00:07:02,180
This is working.

84
00:07:02,180 --> 00:07:04,580
Try the Homepage.

85
00:07:04,580 --> 00:07:07,970
This is working too. If you want to change something.

86
00:07:08,010 --> 00:07:13,000
Let's say "About content goes here!".

87
00:07:13,040 --> 00:07:18,330
You save the script and the web app will start by itself.

88
00:07:18,380 --> 00:07:24,630
So you don't have to close the web app and run the script again as I did earlier.

89
00:07:24,740 --> 00:07:29,230
If you get an error like I got previously, the script will stop.

90
00:07:29,420 --> 00:07:32,320
But if everything is all right you will not get an error.

91
00:07:32,490 --> 00:07:39,000
So, try this again. Homepage is fine.

92
00:07:39,230 --> 00:07:42,380
The about page is fine too.

93
00:07:42,390 --> 00:07:50,060
And with the new content. So that's about it. 
In the next lecture, you'll learn how to return

94
00:07:50,090 --> 00:07:58,130
an HTML template, an HTML file using your Python functions, that will give you full power to publish anything that

95
00:07:58,130 --> 00:08:05,690
you want on your website and not just plain strings as we did in this lecture.

96
00:08:05,740 --> 00:08:07,500
See you in the next lecture.

