1 00:00:00,000 --> 00:00:02,723 Hello, and in this lecture, you will 2 00:00:02,729 --> 00:00:06,089 build your first website with Flask, if 3 00:00:06,089 --> 00:00:08,849 you haven't done so already, and Flask 4 00:00:08,849 --> 00:00:11,549 is a Python framework that has all the 5 00:00:11,579 --> 00:00:13,319 tools and the templates and the 6 00:00:13,319 --> 00:00:16,469 functions for you to build the websites with 7 00:00:16,469 --> 00:00:19,649 Python, and before going deeper into 8 00:00:19,649 --> 00:00:21,719 Flask and explaining you how to use 9 00:00:21,719 --> 00:00:24,839 Flask probably for building Python web 10 00:00:24,839 --> 00:00:27,479 apps, I would like you to spend 3 11 00:00:27,479 --> 00:00:30,299 minutes to build a very basic website 12 00:00:30,719 --> 00:00:33,389 with Flask. And that is done with only 13 00:00:33,389 --> 00:00:36,449 seven lines of Python code. So that's 14 00:00:36,449 --> 00:00:39,539 what we'll do right now. To build a web 15 00:00:39,539 --> 00:00:41,579 app with Flask, you need to have Flask 16 00:00:41,579 --> 00:00:43,299 installed. So 17 00:00:43,324 --> 00:00:45,460 [No audio] 18 00:00:45,485 --> 00:00:47,609 pip install flask should 19 00:00:47,609 --> 00:00:50,055 install Flask in your Python environment. 20 00:00:50,079 --> 00:00:53,271 [No audio] 21 00:00:53,296 --> 00:00:55,877 Alright, that was successfully installed. 22 00:00:57,252 --> 00:01:01,774 And let's create our website now. What you need is a 23 00:01:01,799 --> 00:01:05,489 Python file. So I'm creating one here, 24 00:01:05,879 --> 00:01:08,639 let's say script1.py, and then 25 00:01:08,639 --> 00:01:11,249 here is where you write the code that 26 00:01:11,249 --> 00:01:14,339 will build your web app, and everything 27 00:01:14,339 --> 00:01:17,069 starts from importing Flask, and 28 00:01:17,069 --> 00:01:19,079 actually, you don't import the Flask 29 00:01:19,079 --> 00:01:22,199 framework, but you import the Flask 30 00:01:22,229 --> 00:01:25,799 class from a Flask framework. So from 31 00:01:26,134 --> 00:01:30,995 flask import Flask, and Flask is a 32 00:01:31,020 --> 00:01:34,319 class of this library. And this contains 33 00:01:34,349 --> 00:01:37,229 all the prototypes that you need to 34 00:01:37,229 --> 00:01:39,959 build web apps with Python. So this will 35 00:01:39,959 --> 00:01:41,669 handle the requests for you. You don't 36 00:01:41,669 --> 00:01:45,179 have to set up requests yourself. But 37 00:01:45,179 --> 00:01:47,399 let's focus on this example for now. 38 00:01:47,759 --> 00:01:49,919 Once you do that, you need to create a 39 00:01:49,919 --> 00:01:52,229 variable where you will store your Flask 40 00:01:52,259 --> 00:01:54,719 object instance, your Flask application, 41 00:01:54,988 --> 00:01:57,754 that will be equal to the Flask 42 00:01:57,779 --> 00:02:01,649 class, and you pass the name variable in 43 00:02:01,649 --> 00:02:03,929 there, just don't worry about that right 44 00:02:03,929 --> 00:02:09,149 now. Then you need a decorator, route, 45 00:02:09,814 --> 00:02:13,264 and here is the URL where you view your 46 00:02:13,289 --> 00:02:16,409 website. So this '/' here means the 47 00:02:16,409 --> 00:02:20,159 homepage and just after that, you need 48 00:02:20,159 --> 00:02:22,979 to write the function, the Python 49 00:02:22,979 --> 00:02:25,619 function, and this function is what 50 00:02:25,619 --> 00:02:29,669 defines what your webpage will do. In 51 00:02:29,669 --> 00:02:31,855 this case, we will return a string. 52 00:02:31,879 --> 00:02:34,873 [No audio] 53 00:02:34,898 --> 00:02:38,879 Let's say a string here, Website content goes 54 00:02:38,879 --> 00:02:41,519 here. Just after that you need two more 55 00:02:41,519 --> 00:02:43,979 lines of code, and that's a conditional 56 00:02:43,979 --> 00:02:50,609 statement, if name equals string main 57 00:02:50,633 --> 00:02:53,764 [No audio] 58 00:02:53,789 --> 00:02:59,849 app.run debug equals True. That's 59 00:02:59,849 --> 00:03:02,759 it. Don't worry about this. Right now, 60 00:03:02,784 --> 00:03:07,224 just click save, open a Terminal and 61 00:03:07,469 --> 00:03:11,787 execute your script with python script1 Python, yeah. 62 00:03:11,811 --> 00:03:14,629 [No audio] 63 00:03:14,654 --> 00:03:15,959 And it looks like your 64 00:03:15,959 --> 00:03:18,749 web app is running now. So open the 65 00:03:18,749 --> 00:03:22,259 browser, and your website should be 66 00:03:22,284 --> 00:03:23,775 live on your localhost. 67 00:03:23,799 --> 00:03:25,941 [No audio] 68 00:03:25,966 --> 00:03:30,689 Now that's it. Nothing impressive, but you see how easy 69 00:03:30,689 --> 00:03:33,449 is to create a website with Python. 70 00:03:33,749 --> 00:03:35,699 And in this case, we are just returning 71 00:03:35,699 --> 00:03:39,869 a string here to the website. So 72 00:03:39,869 --> 00:03:42,569 that's nothing fancy in there. But later 73 00:03:42,569 --> 00:03:44,729 on, I'll explain to you how you can 74 00:03:44,729 --> 00:03:47,819 return HTML pages instead of plain 75 00:03:47,819 --> 00:03:50,699 Python strings to your website, that will 76 00:03:50,699 --> 00:03:52,919 allow you to actually have different 77 00:03:52,919 --> 00:03:55,169 formats, different fonts on your text. 78 00:03:55,319 --> 00:03:58,109 And then you can also apply CSS styling 79 00:03:58,109 --> 00:04:00,689 to your HTML, and so you'll have a 80 00:04:00,689 --> 00:04:03,689 complete website with modern features. 81 00:04:03,959 --> 00:04:05,969 For now let's make sure you understand 82 00:04:05,969 --> 00:04:10,109 these seven lines of code. Yep, it's 83 00:04:10,109 --> 00:04:13,289 seven lines. As I said, the first line 84 00:04:13,345 --> 00:04:16,285 you are importing the Flask class object 85 00:04:16,349 --> 00:04:19,289 from the flask library. Then in the next 86 00:04:19,289 --> 00:04:21,329 line, once you have imported the 87 00:04:21,329 --> 00:04:24,299 Flask class in here, the Flask object in 88 00:04:24,299 --> 00:04:27,059 other words, you are instantiating that 89 00:04:27,059 --> 00:04:30,359 class, that object. And this here is a 90 00:04:30,359 --> 00:04:32,939 special variable that will get as value of 91 00:04:32,939 --> 00:04:36,179 the name of the Python script. Now when 92 00:04:36,179 --> 00:04:38,579 you execute a Python file, a Python 93 00:04:38,579 --> 00:04:42,899 script, Python assigns the name main, this 94 00:04:42,899 --> 00:04:44,288 string to the file. 95 00:04:44,312 --> 00:04:47,592 [No audio] 96 00:04:47,617 --> 00:04:49,799 When you import a 97 00:04:49,799 --> 00:04:52,529 script into another script, like if we 98 00:04:52,529 --> 00:04:55,169 import this script from another script, 99 00:04:56,249 --> 00:04:59,455 this script will be assigned the script1.py 100 00:04:59,818 --> 00:05:01,769 name. But when you execute 101 00:05:01,769 --> 00:05:04,709 the script, Python assigns the name, 102 00:05:05,185 --> 00:05:08,055 '__main__'. 103 00:05:08,311 --> 00:05:11,644 So what happens is that, in this 104 00:05:11,689 --> 00:05:15,312 line, we say if name equals to main, 105 00:05:16,174 --> 00:05:17,559 then run the app, 106 00:05:17,583 --> 00:05:19,632 [No audio] 107 00:05:19,657 --> 00:05:20,734 and this is true 108 00:05:20,759 --> 00:05:23,069 when we execute the script, as we did 109 00:05:23,069 --> 00:05:26,609 here. But if we imported the script 110 00:05:26,639 --> 00:05:29,459 from another script, this line here would 111 00:05:29,459 --> 00:05:33,119 not be executed. So this allows us to 112 00:05:33,119 --> 00:05:36,539 have control over this script using this 113 00:05:36,539 --> 00:05:39,389 line here, then we have these two lines. 114 00:05:40,169 --> 00:05:44,339 This is a decorator. Now the output that 115 00:05:44,339 --> 00:05:47,849 this function produces, will be mapped 116 00:05:47,879 --> 00:05:50,429 to this URL. So if you want, you can 117 00:05:50,429 --> 00:05:52,649 change this URL to this, for example, 118 00:05:52,829 --> 00:05:55,949 save the script. Sure, you need a new 119 00:05:55,949 --> 00:06:02,309 URL here. Save the script, run, and go to 120 00:06:02,309 --> 00:06:05,489 the localhost, reload, and this time 121 00:06:05,489 --> 00:06:08,489 you'll get an error, Python couldn't find 122 00:06:08,489 --> 00:06:12,269 this page. But it can find the about 123 00:06:12,269 --> 00:06:17,339 page. Yeah, that's it. If you still want an 124 00:06:17,339 --> 00:06:20,549 homepage, you would add another 125 00:06:20,549 --> 00:06:22,469 decorator and another function just 126 00:06:22,469 --> 00:06:23,921 after the decorator. 127 00:06:24,803 --> 00:06:27,450 So just leave this, the homepage, 128 00:06:27,474 --> 00:06:29,474 [No audio] 129 00:06:29,482 --> 00:06:31,736 and just say homepage. 130 00:06:33,330 --> 00:06:38,309 Here, save. Again, we have an error 131 00:06:38,309 --> 00:06:40,769 because we cannot have functions with 132 00:06:40,769 --> 00:06:44,189 the same name. So we can change this to 133 00:06:44,219 --> 00:06:48,179 about, the name doesn't matter. So this 134 00:06:48,209 --> 00:06:50,249 doesn't have to be the same with this one, 135 00:06:51,030 --> 00:06:55,080 click Save, run the script again, 136 00:06:55,104 --> 00:06:57,282 [No audio] 137 00:06:57,307 --> 00:07:01,488 and go to the website. This is working. 138 00:07:02,005 --> 00:07:05,164 Try the homepage, and this is working 139 00:07:05,189 --> 00:07:07,709 too. If you want to change something, 140 00:07:07,834 --> 00:07:12,555 let's say About content goes here, 141 00:07:12,579 --> 00:07:14,026 you save the script, 142 00:07:15,704 --> 00:07:16,919 and the web app will 143 00:07:16,919 --> 00:07:19,709 restart by itself. So you don't have to 144 00:07:19,739 --> 00:07:22,679 close the web app and run the script 145 00:07:22,679 --> 00:07:25,349 again as I did earlier. If you get an 146 00:07:25,349 --> 00:07:28,379 error like I got previously, the script 147 00:07:28,379 --> 00:07:31,019 will stop. But if everything is alright, 148 00:07:31,049 --> 00:07:33,255 you will not get an error. So 149 00:07:33,279 --> 00:07:35,407 [No audio] 150 00:07:35,432 --> 00:07:41,189 try this again. Homepage is fine. The About page is 151 00:07:41,189 --> 00:07:45,269 fine too, and with the new content. So 152 00:07:45,269 --> 00:07:47,309 that's about it. In the next lecture, 153 00:07:47,309 --> 00:07:50,039 you will learn how to return an HTML 154 00:07:50,069 --> 00:07:53,039 template, an HTML file using your Python 155 00:07:53,039 --> 00:07:56,489 functions that will give you full power 156 00:07:56,489 --> 00:07:58,649 to publish anything that you want in 157 00:07:58,674 --> 00:08:01,824 your website and not just plain strings 158 00:08:01,919 --> 00:08:06,179 as we did in this lecture. So see you 159 00:08:06,209 --> 00:08:07,559 in the next lecture.