Professional Python Web Development Using Flask | Jorge Escobar | Skillshare

Professional Python Web Development Using Flask

Jorge Escobar, Technologist, entrepreneur and open source fanatic

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
81 Lessons (12h 9m) View My Notes
    • 1. Introduction

      2:14
    • 2. What is Backend Development?

      3:29
    • 3. Why Python?

      4:03
    • 4. The FromZero Approach

      3:10
    • 5. Introduction to Cloud9

      2:35
    • 6. Closer Look IDE

      5:53
    • 7. Shell Commands

      10:27
    • 8. Python Shell

      2:13
    • 9. Arithmetic Functions

      2:32
    • 10. Variables

      4:25
    • 11. Checking Types

      1:56
    • 12. Variable Format

      3:34
    • 13. Strings

      4:16
    • 14. Lists, Tuples and Dictionaries

      6:34
    • 15. Date and time

      4:29
    • 16. Conditionals Control Flow

      4:16
    • 17. Loops

      3:12
    • 18. Functions

      3:08
    • 19. Classes and Objects

      14:35
    • 20. Modules

      11:53
    • 21. Parameters

      13:41
    • 22. New Workspace Virtualenv

      8:53
    • 23. Pip Install Flask

      3:05
    • 24. C9 Python Path

      6:24
    • 25. Minimal App

      13:11
    • 26. Starting with Git

      3:52
    • 27. Debugging

      11:42
    • 28. Routing with Vars

      10:28
    • 29. Url_for

      4:50
    • 30. Get Method

      8:49
    • 31. Post Method

      7:00
    • 32. Introduction to Templates

      9:37
    • 33. Login Template

      8:07
    • 34. Login Function

      8:27
    • 35. Redirect After Post

      6:53
    • 36. Flash Messages

      8:49
    • 37. Better HTML

      3:35
    • 38. Block Super

      4:50
    • 39. Template Inheritance

      12:04
    • 40. Cookies

      12:20
    • 41. Static Folder

      4:18
    • 42. Sessions

      6:22
    • 43. Loggers

      7:13
    • 44. User Table

      15:03
    • 45. Intro Mysql

      12:32
    • 46. Requirements

      1:07
    • 47. Let's begin with our Blog

      14:20
    • 48. The Basic Structure

      9:15
    • 49. The Author Model

      8:08
    • 50. Setting Up the ORM

      12:29
    • 51. Interacting with the ORM

      12:53
    • 52. The Base Template and Bootstrap

      8:23
    • 53. Introduction to WTForms

      15:45
    • 54. Form Errors

      11:02
    • 55. Macros

      8:05
    • 56. Blog Model and Form

      8:16
    • 57. Blog Admin and Setup Templates

      14:39
    • 58. Blog Creation Database

      11:30
    • 59. Author Login

      17:03
    • 60. Login Required Decorator

      14:47
    • 61. Introduction to Migrations

      13:46
    • 62. More Secure Password

      17:22
    • 63. Checking is_author

      10:04
    • 64. The Post and Category Model

      13:35
    • 65. Post Migration and Testing

      12:01
    • 66. Introduction to Markdown

      9:56
    • 67. Post Form

      11:46
    • 68. Saving the Post to Database

      9:33
    • 69. The Article View

      14:44
    • 70. List Articles

      17:29
    • 71. Logout Links Footer

      3:59
    • 72. Pagination

      12:13
    • 73. Installing Flask Uploads

      13:39
    • 74. Adding Image Blog Post

      9:28
    • 75. View Image Index Article

      6:50
    • 76. Deleting Article

      13:57
    • 77. Editing Articles

      17:18
    • 78. Introduction to Unit Testing

      15:39
    • 79. Create Blog Test

      15:45
    • 80. User Tests

      8:27
    • 81. Final Project

      2:53
21 students are watching this class

About This Class

28254cb3

Learn from scratch how to build backend web applications using Python Flask, Cloud9, MySQL and Docker Containers

This course will teach you, assuming no prior coding knowledge, how to develop back end web applications the way professional coders do in the top internet startups. How do I know this? Because I've been leading tech teams in both large enterprise as well as startup companies in New York City for the past 15 years.

I have seen a lot of courses and free tutorials and I can tell you 90% of them just teach bad habits while promising to turn you into a real “web developer". But let me tell you a reality: There's no such thing as a web developer these days. You're either a back end web applications developer, a front end application developer or the so-called (and rare) full stack web developer which includes the other two. However there are so many technologies to master in both the backend and frontend areas that full-stacks (or “web developers") are a rarity in professional environments -- You're either a back end or a front end web developer.

This course doesn't promise to turn you into a professional back end developer after you complete it -- it takes much more than the 11 hours of of this course (and probably hundreds of hours of self-practicing) to do that, but it will give you a good foundation from where to start and continue your training, knowing the right path to become a real professional backend web applications developer using Python. My goal is to make a second course, which would introduce more advanced back end concepts and then start the front end courses (basic and advanced) soon after that.

The course goes through a step by step process of developing web applications, teaching you the Python basics for web development, introducing Flask and using Cloud9 as your development environment. It then moves to explore SQL databases, using MySQL and finally showing you how to develop a blogging application using all these learnings.

Best of all, you don't need to install anything as we will use a revolutionary online web development environment that essentially gives you your own Linux web server with database capabilities! All you need to have is a browser and internet connection and it's completely free to you.

The course is divided in 8 sections and 2 bonus sections:

  • Introduction
  • Setting up our environment
  • Python basics
  • Installing Flask
  • Introduction to Flask
  • An introduction to databases
  • Our first Flask application: A personal blog powered by MySQL
  • Final Project
  • Bonus: Running Our Flask Application with Docker
  • Bonus: Deploying our Application to a Cloud Server

The course has more than 12 hours of video tutorials as well as the source code at the end of each of the Flask application lessons, so that you can see exactly what the whole project looks like in each stage.

The course can take anywhere from 10 days to a month to complete based on how much material the student completes daily.

Additionally we're constantly updating the course, adding content thanks to the feedback of our students.

We will also have office hours where you can ask the instructor any question you might have about the course or about Python Backend Web Application Development in general.

So If you are interested in learning how to code from zero and without prior knowledge, but do it using best industry practices towards becoming a professional backend web developer, this is the course for you.

So stop looking around and start the right path to becoming a professional Python backend web developer with this course!

What are the requirements?

  • A computer with internet access and administrative access to install packages
  • A basic understanding of how to use the internet and text editors

What am I going to get from this course?

  • You will learn the basics of the Python programming language
  • You will learn what databases are and how to use them effectively
  • You will learn how to interact with the database using the MySQL CLI
  • You will learn how to effectively develop a Flask application
  • You will learn about Software Patterns like MVC and decorators
  • You will learn how to process data from HTML Forms into a web application
  • You will learn how to run Flask applications using Docker
  • You will learn how to deploy an application to a cloud server

What is the target audience?

  • Programmers
  • Software Developers
  • Project Managers
  • Computer students
  • Entrepreneurs
  • Software development aficionados

Transcripts

1. Introduction: Hi. Welcome to professional back in Web development with Pichon Flask, where you learn how to build where applications Using the amazing Python language. My name is Jorge Escobar, and I've been lucky to work in the leading tech companies for the past 15 years, and now we'll show you from scratch how to become a professional Web developer. You can find a lot of online courses that promise you how to become a Web developer. The truth is that in the professional tech industry, there's no such thing as Web developer positions. You're either a back and developer or front end developer on the skills required for each are completely. This course will show you step by step, the best practices to begin your career to become unemployable back in Web applications. Developer I will show you step by step and through the power of video on introduction toe the Python language. How to install Flask, a first look at sequel databases and then we'll build a blogging application using best development practice. At the end of the course, you will challenge to expand the application by developing a commenting system for Blawg. You will also be able to get the full court base as it looks each step of the way so you can develop your knowledge looking at how the system grows bit by bit. The course is the sign for people with little or no previous coding knowledge but are eager to learn how to build Web applications. All you need is a computer and the willingness to put your full attention toe the Metis. Listen. There are other courses that go the easy route and teach using graphical tools. I can tell you, those students would not survive a real life interview in a professional by my course now, and I will start your path to becoming a professional python backend weapon. 2. What is Backend Development?: Okay, let's take a look at what is front and back in development. Um, for that we're going to check what a Internet process looks like on a very high level. Um, let's a diagram and and understand what the steps look like when you request a page. So the first thing you need to know is that there's always a browser and a client. The browser is basically the program that you used to access the Web, the Firefox grown Internet Explorer, whatever you use, and then the server is basically a service that's out there in the Internet. Basically, um, it's a group off servers or computers that are, um, located in a specific you're out. So when you type of euro, what happens is the, um, your Internet provider will hook you up, will direct you to the appropriate server, and then that server will has a process that's a looping process that is always checking. Is there any requests there any requests? And when it received your request for a specific page, it ah basically renders or or comes up with the with the code to build the page that you're requesting, and for that it can access a database where where all this content is is located. Once that Carlton is located, it packages all that up into a webpage on B, returns it back to the browser where the browser will render it as, ah, so appropriate. So what is back end development in front of it? Development? Um, so the idea is that back in development is the processes or the software, Um, the code and the, um the different routines and algorithms that live in the server, um, and interact with the with the database, um, versus the browser code, which is the front. And development so far in development entails working with HTML, CSS and Js, which are ah, the basic, you know, languages for the basic systems that allow the browser to render the content of the server is giving back. So you can think of it as front and being, um, everything that has to do with what declines sees and then back in is all the data behind. Ah, what that page looks like. So in this course, we're gonna we're gonna focus on that on that piece, we're gonna be talking about how to code um, and Ah, and how to develop applications that are inherently listening for requests from a browser and return back ah content code, if you will, that will allow the browser to render that data that information that is stored in the Indus Server. 3. Why Python?: Okay, So one question you may ask it's y Python, where we're learning Typhon or back in development and not something else. And there's a lot of other languages that are that are suitable and have good Ah, you know, good reviews or or their talked about very well on the Internet. And I think it's a personal decision. My personal experience has been that Python has been a very, um, easy to learn, like the learning curve is not too steep. Um, but it's also a language that that it's a joy to read, um, a lot off coding when you're when you have a coating. Careers is about reading other people's code, and Tyson makes it really easy to understand. What, what the thinking waas off another quarter? Um, just by reading it without even looking at the comments, um, or documentation. I think, um, you know, Python ease is Ah, it's fast. Um, again, there are people that say that other things are are faster, but I think once you get into the, um, the real mawf Web, um, Web serving and serving times and all that I think there are many other Viable is to consider um, aside from how fast the languages. But having said that Python is Reese is pretty fast. Um, I like that. It's, ah, object oriented from the beginning. I think that we're gonna talk about a little bit. Why that that's important. But, um, s a first introduction toe. What that concept is it has to do with laying out your code in a way that's reusable and that you can leverage other people's work without having you to reinvent the wheel. Um, that's kind of like what object oriented for me is, ah, what the benefit is. It also, um, you know, building on top of that python is very extensively has a lot off, um, third party libraries that can do what I mean, there's, like, thousands off things that you can do with it from, you know, like math, um, calculations from hooking up to the most popular that air bases from, um, you know, interacting with social platforms like Facebook, Twitter and ah, you know, Ah, a lot off very, very exciting projects that, um that are across that believe across fields off different, different things, like from gaming to, like, scientific to business. So it has a lot off libraries, and the community around Python is very, um, very strong. They're very passionate about by phone, and you will hear about a little bit of the rivalries, especially with the folks that that, like Ruby on rails, which is kind of like a ah, big, um, it's a counterpart or or competition for Python. Then you know, you have to know jazz community also popping up. But it's a it's a friendly competition. I think that, um, you know, each language has he has its own strengths and its deficiencies. But I just like Python. I felt very much at home. Um, I came from, ah coding in peril and then pee. It's B and, ah, now python. I kind of like I look back and it will be very hard for me to go back to another language 4. The FromZero Approach: Hi. I wanted to talk to you a little bit about why is from zero different or better than other courses? Well, first of all, I have experience with complex projects. I've been working with both big companies and start ups, and I've completed a lot off high traffic and fast response, very complex projects that have, um, dozens off developers committing and contributing. So I know the best way to, um, approach a project and make it efficient, scalable. So I'm gonna teach you all those things that I that I know. Um and, um, one thing that I I I'm always striving to doing these courses is to guide you through the basics. Ah, or start with the basics. And then we want to advance topics in the most efficient way. I e I'm not gonna go into small details or maybe go through all the, um, all the chapters of a for example, Baekeland book. But I'm gonna go through the most efficient way so that you can get yourselves up and running and ready for development. Um, I'm also gonna be teaching this us hands on course in all the courses. That means that you're always gonna learn by doing and not just getting a lot of information off the projects or the courses or the languages before actually doing stuff. So it's gonna be very hands on, and I'm not gonna sugarcoat it. Um, it'll be I'm gonna teach you the hard way. Which means I'm not gonna teach you, for example, how to use my sequel or a database using a coolie like PHP. My admin. That's another way that professional developers do it. So I'm not gonna go ahead and just teach you something That's E. C. Because it's just easy for you to use it. You're gonna actually learn the tools the way that professional developers do it in in the leading tech startups. So I hope that you join me and that you enjoy this course on bond. I'll be there to assist to in every step of the way. Um, that's why let all the other students, But I can promise you that you're gonna learn, even though it's gonna be hard sometime, Um, you're gonna learn the proper way, and you're gonna be a professional Web developer in no time 5. Introduction to Cloud9: So now we're going to talk about what is the development environment that we're going to use. And I did some searching and I decided to finally go for Cloud nine. So Cloud nine is basically, ah, Web development environment. Think about it. I say your your server in the cloud. You can add it coding, and you can start databases and it works based on a technology every like, which is Doctor Um so I think it's It's ah, it's a great way for us to learn coding without, um, having to install things, installing python. And if you've if you've seen some of the courses I have, I always go into, like how toe set up. You know, the different environments and the databases and and all that. But I found that students, because they were just like starting to learn it, was kind of like an additional hassle for them to learn how to set up, you know, Windows, Paice, owner or Mac by phone, and then the data basis and all that. So I've decided to start using Cloud nine as the development environment for all my courses , and it's gonna be good because we're gonna be able to basically install and be able to code and work on this platform without having to install anything on our computers. And it doesn't matter what operating system you have for if it's Windows or Mac, you basically will have a lean it's machine running on, um, on the cloud, and you can edit and work on your application wherever you are. Um, the good thing is that they're they're pricing model. They have a free tier here, as you can see. And, you know, you can basically sign up, just enter your user name and password, and he has some pretty cool features like, um, collaborative coding. So, for example, you can, you know, called with another friend of yours or another student on you can we can install data basis and set of frameworks without any problems. So I think it's gonna be a good a good thing for us to use. And we're gonna go over it, um, a little bit us. We start doing the scores 6. Closer Look IDE: Okay, so let's get a little bit acquainted with different areas off cloud nine. So first you head over to see nine dot io, and you can basically just click on tried now to register, um, your new account. Um, once you click there, you can sign up with. If you have a gate, have account or a big bucket account, you can sign up with that. All the ones you can choose a user name and email and a password, and select here so that you can get, like, a capture and then create your frequent account. Like I said, they have ah free account here. They also have, like, some paid paid account ears. But for the purposes of this course, you don't need to really get into a a paying course. I mean, account. So I'm gonna log in with get hub. And when you when you sign in or you register, you get basically this area, which is the dashboard, and you have what they call workspaces and workspaces. Think of them as basically different projects and they're actually basically certain, like separate servers that you're creating with applications in it. Um, so initially you get this test workspace, and then you can create a new one, which is what we're gonna do when we start our first flask application. But let's see what happens when you when you select that existing workspace. But before we get there, if you click here on the name off the workspace, you basically have the capacity off. Like doing some editing you can have. Ah, you'll see a read me. You'll have the files that are in there. Um, so you can take, like, a quick look on. Leigh, Read me. Um is here in this in this test project, and here's a very interesting one. You have members and in members you can basically invite. If you have invited people, you can see them in there, um, to invite people, you need to be inside of the off the actual project. But you also you'll also see how much CPU using how many, how much of the ram and how much of the storage you're using. And they have. For the free tier, you can have up to one CPU 512 megs of ram and one gigabyte hard drive, which is more than then generous I find, um, but now that we know more or less, what's that about? Like I said, you click on the name itself. You'll see those those those statistics. But in order for us to actually start doing stuff, you click on this green open button. So once we click open here, well, we're gonna be taken toe the what's called the I. D or the integrated development environment. And basically, this is kind of like a code editor also has, like, a terminal here for the server. And you have a basically a file navigator, as you can like, you recognize from from similar type of applications, like coordinators Or, you know, even like, you know, work or editors. But, ah, here you can basically navigate what's in the folder. Right now, we only have this file read me MD on the side. Here we have basically, you can edit. Um, you know anything and you have an undo function as well, if you want. Um, it shares like a lot of the things that normal coordinators have, so it's It's very, very well built, Has a lot off capacity. You're not giving away a lot off, um, off power because you were using this disintegrated Ah, development environment. And here the bottom. We have the terminal, and this is something that I always kind off force students to kind of get very acquainted with because it's basically the the way that you interact more with with with systems we don't want to get used to using, like, graphical things. We want to be very comfortable with the with the terminal. If you click on this little kind of like a window function here, we will get a, uh, a standalone editor. And you can minimize again by clicking on that, um on that is likened. And 11 important one that you want to remember is control escape, which basically shows and hides that that terminal back and forth. Um, you can also, like, add tops here, for example. You can add a new like and I think when you when you've opened it for the first time, you'll see this in needed JavaScript editor. I usually close it because it's not something that we're gonna be using, uh, for this course, so you can go ahead and close it, but you can have another terminal window. Let's say from Ah, with another folder open or things that you want to do a swell. But in any case, um, it's it's ah, it's a very nicely integrated environment and what we're gonna look at next is some basic terminal function so that you guys get better acquainted with with terminal commands and how to copy and navigate directories and things like that. 7. Shell Commands: So the star playing a little bit with the terminal? Um, I have modified the callers as we can see here, Um, I going to Ah, you go here to Preferences, and then you go to, uh, terminal here. And then I put the background colder by clicking here to black so you can basically put any any combination of colors. I also increase the phone size to 18 so you guys can can see a little a little bit better. Um, and I close that and then let me put that in full screen and start, um, doing some basic a man so you can get acquainted with how, basically our linen, um, file management and and commence work. So the first thing we're gonna look is we want to know where we're at at a given time. And for that we use a chemical PWD, which is present working directory. And here it's It says that we're at home boom to work space. You see, guys understand. Home is basically usually the folder where the the accounts the user accounts are placed in . So if you had another user cold, you know Jorge, it would be slash home slash Jorge, and that's called the Home Directory for that user. And this still the basically represents that. So if you're in any that say we moved to any other folder, um, we can quickly go back by doing CD, which is change directory and putting that till they're there. It'll take us back to the to the home directory. Um, so and in cloud nine, there's a There's a workspace folder, which basically is where the projects are stored. I don't have a problem with that. I usually when I work on Lenox, I have, um, actually worked with the O. P. T directory. But workspace works finest well, so again to let us know where we're at PWD. So we're in the home Bhutto, which is the user that that Chlo nine assigns you automatically when you when you select the the bone to environment and then work space, which is a directory where, where we're at. So let's see at how we can see what the contents of the directory are. And that's basically the command ls So ls will show us what What files are in that current directory. And so, as you can see, we have read me dot MD, which is that Read me file that we saw on the editor earlier. Um, now, let's say let's say we want to create a new directory within the home. A boom to work space. The command for that It's make directory M k the I r. And let's say we create a direct trickle test. Um, Now, if we do ls you'll see that we have read me. And then we have a directory called Test. So in order for us to change the directory, get inside of that directory we used CD and then test. One trick that you guys want to know about is that you can put the first, like letters off, basically commands and directories and all that. And then if you press the tab, you'll see that it auto completes to whatever, um, the best matches on if you continue clicking tab, you continue seeing through those things. So if I If I do see the r and then click tab, you'll see that, um, it should get me toe. Since I'm not in the Marine, the workspace. So what? Basically, you will get the read me empty, but I'm going to go to the so if I do ls you'll see that? Read me there, um, on gonna change to the test directory here. So right now there's nothing in there. Um, the next thing we're gonna do is we're gonna use a common cold touch and touch is useful to create empty directories. Like, if you want to just create a quick, um, file that has nothing in it. So we can do, Let's say, test test, not txt. Um, if you do a less now, you'll see that, um, there's a test txt now in there. So that's a useful command we're gonna use it to. There's a, um there's a file name called Any P Y that we use for initializing directories as modules for python ball will get will get toe that quickly. If you want to clear the screen on, go back to the top. We used a clear command, and that puts us with a clear with a clear screen. Um, some other useful things. Let's say we have Ah, the test directory. Here. Let's create another directory called test. Um, in Let's say test in. Okay, so now we have tastic City and then another folder. We think that, um, cold test in. So let's say I want to move that folder. I mean, that filed spoon that's dot txt two. Their test in I use the M V command, which is move, and basically I'm gonna move test the txt to, ah, test in directory. So I do that. So I move. I'm saying move test txt to within the directory test in effect press enter there. Now, if I do a last, you'll see that we don't have test txt anymore, and it's in test in. But how do I checked up without having to go again? Like doing seedy testing You can actually do l s and then type the name off a directory. And if you press enter, you'll see the contents off that directory within, um, that that that folder that you have selected so you don't have to go inside of it. So that's the move, Command. Um, there's also, um, a very useful command called Cat. Let's go back to the to the home folder and then workspace. So see how I did that? Um, um, I'm now in the homeroom toe workspace, so there's a chemical cat and cat basically allows you to see the contents of a file quickly without having to, um, open theater or anything. You just want to, like, take a quick lands. So there you go. So if I do can't read me, I see the contents of the file without having to go to the to the editor. Um, if we have a long file, we can also use a command clear the screen called more. So if I do mawr and then read me, um, it basically will Pagine eight because this this file can be seen within only one page. There's not a whole a whole lot off use for it. But if you if you type more and there was more than one page, you can basically see the contents of the file in a paginated manner. Um, one more useful one is called man and man. It's like the manual so you can see the options that you have for any in UNIX or Linux commands. So let's say if I type man CP, I can see the Aled the options that I have for the for the copy command and you can press down arrow or up arrow to see them. The file. Or you can also press the space key and then paginated by page. When you want to exit, you just press the queue, the queue letter and it'll go back out. So, um and that's what basically, it's using the more command, which I was telling you earlier. Um, the last thing I want to talk about is the wild card. So if that they have, let's go to the test folder and let's say, let me create quickly, that's two dot txt and then test three dot txt. And there's something called Wild Car, which is the asterisk. Um, and it's a wanted to move quickly a bunch of files that started with test. If I do move, Test star or ASA is the T Eckstine and I want them in the test in folder, that ass teres is going to say, OK, anything that has that begins with test, no matter what, uh, letters or numbers come afterwards and then after that, a dot and txt are gonna be moved. So if I press enter there, you'll see that, um, I don't have the test. Um, you know files anymore, and they're all in the test in folder. So that's a quick look at terminal commands, and there's many, many more. And there's, like different flags that we can put, which are like parameters for those commands. But you're you learn them ass you as you use them Azaz. We go through the 30 course, but for now, that's Ah, that's a very quick introduction to the terminal commands. 8. Python Shell: So let's start playing a little bit with Python and we're gonna use the terminal again. Time will maximize this here. Um, with cloud nine, we have an option to use sports by Thorn to and buy from three by phone to is still very much in use because there's some libraries that still haven't been ported to buy from three . But I would say these days, most off the off, the useful or most common libraries and frameworks have been updated. So I'm definitely start. And I would start using python three from from this point on, Um and you know, it's it's ah, It's a good thing that we start using by from three projects to, um kind of like move our community or python community towards that, that newer version. So the way we access, if we use only python like this, we're gonna use we're gonna be basically using Python 2.7, which is the the last two version you can do dash V to see to check the version of it. So as you can see by phone, 2.6, 2.7 point six I mean, is what you get from python But we can also use Python three by just typing by from three, and then you'll see that we have 3.4, which is the latest by phone version. So that's that's good. So, um, there's there's a way to play with my iPhone and that's through the python shell. And the way you do that is you just use your state by phone or by from three and then press enter and you'll enter this Ah ah, basically, uh, terminal, where you can play with and do, like, small kind of functions and get to know by son better. So let's start doing some commands and start playing with it. 9. Arithmetic Functions: Okay, the first thing we're gonna do is, um, use by phone as a calculator. Yes. So you get an idea of what things we can do. Um, if I type through plus three, I get five. So, you know, that's that's, ah, basic stuff in there, but, yes, you get a feel for it. So three minus one is to for multiplication. Used to star three times eight for division used to slash 10 divided by two. Ah, One thing you'll notice is that in this operation we got a what's called a floating number . So it wasn't five integer body was five point. Oh, that's because my family is basically kind of like, uh, advancing or forecasting that you might get not, um, integer number. So if we do then divided by three, you'll get 3.3333 which is, you know, the floating number. Um, we can also do, um, exponential by doing two asterisks. So to the power of three is eight. Um, and you can also get the module is which is the reminder of a division. So 10 modules, three iss one because you know, 10 divided by three is three and then you get one more as the, um, seem arduous. Um, the last thing here's like we can, um, group terms together because usually divisions and modifications come before substructure in and addition, for example, if we do two plus three, uh, times five. What will see here is that the multiplication comes first, so it's 15 and then it'll add to two, which is 17. But if we wanted to do the two plus three first you put a parenthesis there and then in that in that case will get two. Plus three is five times five is 25 so that's kind of like basic arithmetic functions. 10. Variables: one key concept in by phone and in other languages is the concept of variables and viable czar basically boxes where you can store, um, some value in it. And then we can reference that value from from that boxes name, so to speak. So let's say if I have, say, a viable called X, you assign it a viable using the equal command, and then you do three. So from now on, X is equal to three. To see the value off a viable, you can use the prin command. So you do print Were you spying this season? Python three used to be print with apprentices X on python to So now we get the value of it . Um, because it's a viable we can change the value whenever we want. So now if I do X equals four and do the print of it, I'll get the do the new value. So as you can see, you can change um, the values off off a viable religiously Um, you can also a sign say we have another viable call. Why? And we can also then now a sine x equals why? And that all that will assign the value off X toe the current value of why, And we'll see why I say that. So if I do print exe now you see that I get seven. But if I change, why toe 10? What do you think? The value of excess the value of X is still the old value. Because this was passed as a value and notice of reference toe the value. So a for for me to change the value of X, I need to, like, reassign x equal. Why X equals y. And then if I do X now, get the updated value. Um, and of course I can. Some those variables, um, explosive y equals 20 um, and you know, do multiplication is and all that. Um another interesting thing is that I can also a sign, uh, strings to viable. So if I do that, say, now Z equals hello. Um, if I pray and see here Ah, you'll see that I have Hello there. And, um, if I have another one, it's a Z Z equals world. If I type c plus zzz, what happens? There is a concatenation. It's not gonna add the values of it, but it's gonna just contaminate the strings. However, notice that we have no spaces in between, which is you know what? We're basically telling it to the self. Refer us to put a space. We would have to do something I z plus quotes plus c z. And then we get the proper hello world. Um, one thing that you cannot do, it's at a a string to a number. We get a an error there because we need to, um, basically convert that one of, you know, the Z toe. Ah ah number which is not possible. But for example, if Z was one right and C c watts equals to two, if I do C plus easy, I get that era. Right? But if I do end of Z, that's basically convert see into an integer and then I do plus easy, Then I don't get the proper result. Um, so that's more or less an introduction to Virals. We're gonna use them a lot, so we're gonna get pretty acquainted with them as we move on to the course 11. Checking Types: a way to check the type off, the viable or the or the number, not the number about the, um the the term that you're you have is through the Taiko man. So, for example, if I do type off one, it says, it's ah, it's a type off integer. So basically it's, ah, it's an integer number. If I don't type 1.0, then type tells me that's a floating them. That means it has, you know, decimals in it. Um, if I do X equals five and then I do type off X, it's as it's it's an integer because it's it's assigning it to, um to an integer number. So that means the viable is inherits the type off, the off the value on If I change that to a string. Um and then I do type X again. I get, you know, the nuclear new classes. It's a string. Um, one thing that do you have noticed maybe is that I can press the type the arrow top and bottom to kind of like go through history so I don't have to, like, retired things again. Um, and once again, you know, if I do type. Hello? Which is a string I'm going to get type off strength. So that's a useful thing. Toe Know and learn. Um, when you want to, like know what? What? The type off a viable that you don't know exactly what what it is? Um, um, kind of like it's Andi. Schools celebrate as just as you see. Very. It says class, we're going to get into classes in later in this section. 12. Variable Format: one thing that you want to get used to, um or know about is something called Pep eight. So if you go to Google and just search for eight, um, and foot Python just in case you'll see this pit eight, um, guideline. And there's basically the style guy for the whole, like by phone coding. And that includes, like, you know how to use code using taps or spaces. What's the maximum lying length? And it basically goes through all the conventions for, um for those, um, you know, functions. Oh, are things that we normally do when I daily basis. So when you have a chance and you have, like, you know, you're having a long lunch and you want to get used to or get acquainted with all the recommendations for for how you know we type code in python, then you should definitely read this. Um, but in any case, um, going back to variables for valuables, we use basically numbers letters and underscores, um, so basically, you know a good noma Good. Ah, viable name is, um you know my var Sony's I use a an underscore whenever I have spaces in there, so I don't do this. My bar, um or do my capital case bar, which is something that jealous refuses. Um, so on the other thing is like try to make viable names as explained, explicit as possible. Like make them meaningful. Don't don't do like, you know, X X. Like I was doing zzz before That was, that was, that's not a good practice. If you're like writing a riel project, it's better to put, you know, you know, database, um, index value versus just like DB I, you know, try to use explicit names, things that you can. People reading the code. I can't understand what's what's going on. Um, there's also like a uh, it's not. None of this is really enforced, but it's it's again what Bebe dictates. We have a constant, which means it's a number that's never gonna change. We use all caps. So, for example, um, data bays ah, name, for example, equals test. So that's that's kind of like it's a good practice to have caps, because that means whenever the person or the developer reading the code looks at that, he knows. Oh, this is something that doesn't change throughout the code base it's it's Ah, it's a cat, it's Ah, it's capital capital life. So that means it's not gonna change, Um, but there's there's all the things that you should definitely look at and take a look at pervade when you have a chance because it's, ah, it's useful for you to get acquainted with it. 13. Strings: Let's take a look at some strength functions. And, um, and helpers. Um, strings can be expressed as quotes like X equals hello or as singles xolo. One thing to know is that strings are basically a raise, and we're going to see a raise, which in pie thunder cold lists in a little bit more detail afterwards. Spot basically think of Honore as a collection off individual characters. So what that means is that if I do, let's say X off zero. That means that I'm going to get the first character off the string. If I do X off one, I get the second. So as you can see, it's basically a collection or array off the strings. H e l l O. And that's something to remember when you you can kind of, like, get the, um, you know, sub strings off off a string by putting a range off characters. For example, if I do zero calling three, I will get the 1st 3 characters off that string, Um, the same way. If I do three through five, I get the last two characters. Um, the thing is, um, as we were seeing earlier, we can con card innate to string. So if I to why equals? Um hey, then we can do explosives. Why? And e get Hello, Paul. Um, if we want to do, like, the the space between the tool I just insert that space in there. Um, we can convert, um, a string that I mean a number toe a stream by using the str function. So, for example, if I have, it's a c equals three. Remember how we said that we cannot come cut in eight? Um, the integer and a number if you wanted to say hello, Uh, three. We would do X plus str off three off z. So str will convert the number three, which is an integer to us strength and their their works. If you wanted to the opposite, remember? Like if we wanted to have a a string that was, um, basically a number we want to get the value of it. Then we do I nt There's also some methods that we can use that are already built in on the strings. For example, if I wanted to have the hello all in uppercase, I would do x dot offer and, um off That doesn't make a lot of sense, I guess. Um, let's say why don't offer and we'll see Paul all in caps and I want to get them all to lower than we do X off lower. So there are more methods like that. But you get an idea off off some things you want to, uh you know, there there are certain my foot. So you can that you can use and one less one that you'll use often is Len, which is what's the length off a specific string. So, Len off. Why is four which is Paul? Um, so, you know, take a look at string operations. When you have a time, there's lots of them, but these are kind of like the ones that are used most after. 14. Lists, Tuples and Dictionaries: So let's take a look at three. Um, very useful types off variables when it's called lists. Um, list is basically a ah, a list of values that can be numbers or strings or both. So let's say X is one common to come on three. Um, and you close them with brackets, and that means that that's a list. So every print exe you get the whole list even to get type X, You get the type class is list, um so least are very useful if you have, ah, basically an ordered or in order kind of like least off values that are, um you need to get access to. And a lot of times, things that come from like the database are are returned as us lift. So you want, like, be able to look through them and get their their viable their their values. One thing you can you're able to do is loop through a list. Um, but that's something that we're gonna we're gonna look at a little bit later. Um, another type, um, or another, uh, yeah, kind of like different storage. Um, Function or or or class is Stupples and Topolsky is basically used with parts. So you can do you know, X equals one comma too. And that's a Topol. So if you do type off X, you get a Topol. So one thing that separates the list on the two poles is that you cannot once a to police, they find you cannot update the values. So it think of a Topol as a constant of, ah constant list or a static list that you cannot change afterwards. Um, to get both a triple and a list value at a specific position. You do basically, um, x off. Let's say the index so actually X off zero, which is this Topol? It's one x of one is is to So it's ah, it's always you always starts the next at zero something that sometimes we forget, Um and what? Basically, you can get any any of the off the values off that off that list or or to pull using that notation. So let's say that, um, we want toe. I want to show you how the we can change the value off off lease, but not of a Topol. So does the find that ex again as 123 If I do X off, one equals four now if I If I print exe, see that I just replaced That won the second value 24 But if I did this Tupelo 1 to 3 and you'll notice that I put a comma after worst, that's kind of like the the way that you work with topples, you have to kind of like ended with with the coma, even though you're not putting a value there. So if I do why off one equals four, I'll get in there because there's no item assignment on totals. Um, the last type that we're going to see is called dictionaries and dictionaries are basically key value, Um, objects. So basically, if I do, let's say, uh, first it first is Jorge and then last ISS Escobar. And then I used Carly brackets for those, so they say is a dictionary and the way you reference it instead of doing X off zero, which you would do in a list, you do X off first, and that will return Jorge. A lot of times these are called objects. Dictionaries are called objects because it's kind of like the first on the last are properties of that object. And, um, and their values are the ones that you put afterwards. Um, the last thing that that we want to see is, um, you can do a list off dictionaries, which is something that you'll see a lot when you're interacting again with with databases . So, for example, let's say we have, you know, users equals and then we have, um let's say, ah, let's to find another another user. Us, uh, why equals first Paul. Ah, and then last name is, uh, Gram. So now I can do users equals ex calmer. Why? So that's a list off users. If I print uh users. I see that I have all these objects there, and something you'll notice is that I eat set to the dictionary first equals pole and Lasses gram. But now it's printing. Last first and 1st 2nd That's something that you can not control on dictionaries. Is three ordering off the off the key? Um, off the keys. Um, and it's store basically randomly in python. Basically, there's no guarantee off that off off. Ordering off the off those keys. Um, but there are things that you can do to kind of like manage that Bought something for for you to like always. Remember 15. Date and time: Python has great control functions for dates in time. Um, again, there's a lot of information, but we'll see some, like, really simple things that you can do. So the first thing you want to do is import daytime, which is a collection off date and time, um, functions and and utilities, By the way, we haven't seen this before, so import basically means, you know, loading toe memory, this specific module. And this is a lot of how Python works. It's, um, basically, you import or you kind off ad any off the libraries or utilities that you have in your in your false system. But you don't need to, like, have them all loaded at once. So that's how you, you know, pick and choose what utilities you're gonna be using in a specific file. Um, and we'll see a lot off that asses. We get into the to the course, but now, after I presenter, I have all these daytime functions available to me. But when we're going to see is like, um, it's this called daytime, uh, daytime now, and what that means is that it's if I print that I get a the state, which is, you know, today, state and the the hour. Um, right now, off, off, off the computer. Um, so basically, since CLOUD9 operates in the cloud, I believe this is said already to, um two UTC, which is basically the the, you know, universal time or green each time as a So you have may have heard. Um and this is have very good practice. You never ever used the local time toe store as your time stamps because, um, you're gonna be basically, users are gonna be in different time zones, and you don't want to be doing conversions. Um, after the data husband stores. So remember, this is a golden rule. Always store time or dates in the database as UTC. But we'll see what? What? How we do that for now. Let's say this is the local time. So if I do now, um, it's a type off. Now you'll see that it's our off type daytime datum. Um, but now I can just things like Why don't year? Um oh, sorry. Now the year and you'll see that I get the year for that date. And I can say now that our I will tell me the hour. So, you know, I can basically, um, be able tow bars. Different difference. Different parts of that off that data. Um, you know, in order for us to start the the UTC properly, we need to import this specific, um, class or function ical daytime from daytime import datum. And now what we can do now is, um it's a time you to see, and we assign that to daytime UTC now. And that will give us, um, the UTC, uh, for for this exact moment. And as you can see, it's the same, basically the same hours as Cloud nine gave us as the local time, which is smart. I mean, they're using UTC. Yes, there as their as their time. So you don't have to do the you know, you don't have to deal with that conversion. Um, manually, but I still kind of expecting and and store things using this function You to see now versus this now function, Um, something for you to always remember 16. Conditionals Control Flow: So let's take a look at, um at two very important parts of the iPhone called conditional is and control flow. So conditional is basically you want to compare one value with another. And, um so basically, if we have X equals three and why equals four, um, a condition would be is X lower than four. And all those conditions give us a either true or false. They're basically billions. Um, billions are, like, you know, logical, true, false, kind off virals. So, um, if I do X is greater than four, then I get a false because threes is not greater than than four. Um, I can also do, um, is X greater than why? And that gives us a false a swell. Um, one thing that we can also do is X is not equal to why, and that will give us basically is eggs. And then than the value of X is not equal to the value of why um, we can also to, um X is minor or equal to its a three. That's true because even though it's not less than three, it is equal to three on the same thing. Why is greater than or equal to four. It will give us true as well. Um, so those are the basic ones? Um Then control flows is basically you can control the where the program goes by doing, if conditions. Right. So if I to If X is lower than four, then you would have a semi colon there and you press enter. You see how we have three dots there? That means that by phone is expecting you to put a kind of like the the inner peace off that conditional. So you can press one space normal you have to do for I mean, the editor, you'll see four. But in the terminal, you can do just one, and that's fine. So if if xx minor before then, then print exe is lower, then four. Okay, so then if I press enter and then print, print, enter again, then it'll execute that because X, which is three. He's lower than four. So that that does execute. But it's safe. We wanted to do if why is greater than five, which is false, right? So we can do, uh, print. Why is greater done for? What will happen here is that nothing will get printed. Why? Because you know why. It's not great and five so it escape and continue on. But there was nothing else to do there. So one thing that we could do is do an else statement. So if you do if Why, uh, these were than five, then I bring this and they put Els and also semi calling there and then put print. Why ISS say not greater than four. So if I press enter again here, you'll see that it'll it'll execute the second line. See? So that's if else, um, there's there's like you basically build all your your programs, using a lot of this like building blocks. So it's very important for you to to know them. I will get to know, um, more off them us. We continue with the course. 17. Loops: Let's look at loops. Loops are basically like the word something that cycles until something happens. And then usually they exit. Although we can have, ah, limited loop. And then, um, you can go on for ah for a long time until you basically stop the program. But let's see what that looks like. So let's say we have this list, Um, and five x 12345 We can look through all those values by using something called a four look . So what, What Four doses that basically you assign value. Let's would call that vow in X, and you put a semi colon and then you'll do something if, for every single item, it will execute that and it'll have vow as thieve value for that for that love. So let's just go ahead and praying Val in there. So if I pressed Enter, you'll see that he went through each one and they printed that that value. So that's called a for loop. You use that all the time. That's coming, kind of like one of those, you know, very useful functions. Another one we can we can use is, um, a while loop and for that. Let's try something else that say that X equals zero on the local here will be while X is lower than six. Let's say then we do print exe and then we'll do another one, since we need to, like, change the value of X because that'll be checked every time that loop ends, we need to say that X is equal to explosive one. That means now the first time it'll be zero. When he gets here, it will be at it. What? And now it's gonna be one. Any checks again. So now X, which is one is lower than sex. It's still prints that goes one of off. So X now is two checks that and so on until he gets to five. Right? And when the springs here we bring it will bring five. It'll add 1256 And when he tries to run that again because because six is not no lower than six, it'll exit and continue with whatever comments you have afterwards. So they see that inaction. And, as you can see, it'll be executed from 0 to 5. Um, the moment that it became six, um, it didn't execute anymore. Um, so these are some of the most used look bloops, but there's there some other variations, some of the things that that you'll discover as as we go alone. 18. Functions: okay, We'll take a look at functions now so we can define our own functions by using the keyword death, which is, like, define So they're fine. That's a, um, plus one. And then basically, you pass. You can have nothing here, or you can have parameters which are basically virals that are set whenever this function is called. So in this case, we're gonna pass. Ah, next. Viable. Um, so once that is is set, we can do stuff with that. Viable. So, for example, we can say, um, X equals X plus one, which that's why the function is called plus one. Um And then we can then print. Exe. Okay, now we have that that function. And if if you type pl us and then press tab, you'll see that Python order completes that because it already has that in memory. So for us to be able to see what what happens there we can past five. And when we enter that it'll return six. So what happened there? It called the function it past five us. The X basically X equals five. A merry did X equals equals X plus one, and it printed that value So if we did hear 15 we get 16. So you get the idea. That's kind of like what functions are And, um, we can we can we basically use functions all the time as well? It's very important that we understand how they work. Um, but play around a little bit with functions and see what we what you can get out of them. One important thing that I want to, um, two guys know and remember, Um, because it's a fundamental thing. There's something called scoping, and basically the variables that are defined within functions are not exposed outside. So in this case, if I print exe here, um, you'll see that I don't get anything because it's not define, even though it's their within the plus one. But that viable is called. It's called a local viable toe. That function, and that's the way that python kind off like protects the data from from the outside to within the function, Um, by using that separation. So remember that sometimes you know, students forget this, and they're like, Why? Why can't I get X if it's seen within the function? Remember that that viable is on Lee seen, and it's only apparent or usable by that function, but not outside 19. Classes and Objects: So, um, something that you may have heard is that Python is an object oriented language and, you know, object oriented programming is something that has bean, you know, being used for a long time. And there are languages are object oriented, like java by phone. But some others are not properly object oriented, like JavaScript, um, and ah ph B. Which they kind of like have bean getting towards object orientation. Um, and the main benefit off object oriented programming. And using that concept is, um is you know how to better manage data and make things were usable, um, and basically allow you to to separate, you know, logic in a in A. In a better way. It's a concept that you'll not grasp immediately. It's something that you'll kind off strong a little bit on, and you'll get to it by by practicing a lot. But think off objects as, um off, Let's say a basically a a physical object that's represented digitally with basically two things. One is properties on the other s methods. Properties are, for example, if you have a car, property could be the number of wheels right, which is, you know, for a normal like to say sedan. It's four. But if it's a truck, it's like 16 or whatever the case might be, that's a property. And a method would be, you know, start the engine. Something that its's kind of like a function off for that object and the class we call classes basically as a blueprint or the, you know, the schematics for you to produce an instance or a clone off that off that object. So let's do an example again. If you don't understand completely, there's a lot off things that you can read or even by using by phone and flash. You'll get a better understanding of what classes and objects are. But let's let's do an example. So one thing you you'll want to remember is that classes are they find with, um, basically a a Oprah case for each one of the words and no on this course within them. That's kind of like the the pep eight off them. So let's say, um, we're gonna define a class gold car, right? So the car I'm going to just have a, um, basically a property cold brand, so I'm going to define a method called. Um, you know, said Brand, and you always put self as the first parameter for these functions that are within the the class. And it's basically a way of saying this can only be called from an instance off that car and we'll put something here called Brand. So then I'm going to say the the brand of the car is whatever you passed as that brand on that function and let's let's leave it at that. So basically, now I can I can the fine copies off that class or objects or instances, basically instances of car that are basically clones of each other, and I can set a brand for each one of them. So, for example, if I say X is on instance off of a car and I put far into this year, if I put type off X, you'll see that it says it's It's a It's a type of car, right? Um, but now you know, if I print exe, um, I I won't get anything. It's just that a car object. Um, but now if I want to set the brand, I'll call that that function by doing x dot Set brand and they will put to your Okay, So now if I If I get the brand of the car, I does x that brand and I'll get Toyota. Um, so then I can do another car, which is why. And then I'll do Ah, why? Said brand. Ah, Ford. And then if I if I tried to get X to the Y brand again forward. So basically ex. And why are instances off the off the off the car class? They're basically cars, and I can set Mawr functions. Tow them, which are called methods. In this case, said Brian, it's a method. And, uh, you know, manipulator defined properties like Brand is a property, so we'll see up a bit more of that asses we go along. But that's basically very brief introduction of what glasses and objects are. A couple of things I wanted to add to our class lecture is one of them is called Ah, magic methods. Oh, are their kind of built in methods, and we're going to see one that's really important. It's called the innate function, um, which basically instructs how we initialize an instance of the class. Ah, by requiring some some Bibles to be to be passed. And the other one is, um, the notion off, um, subclass ing, um, which is basically you can take a class and then, uh, extended. And ah, that's gold in inheritance. You can hurt the older methods and all the characteristics of that class bypassing the class us the s a first parameter. But let's check out how how that works. Because we would not enter the Python interpreter again by from three. And we're gonna define a ah, again the class car and without any parameters in there. So I'm gonna define a, um, a new method, a magic method which is present in many classes. It's very often that we use this and it's called in it. So we need. What it says is that whenever you create a new instance of the car, you need Toby, you need to pass this viable that. I'm gonna be listening here. So I'm gonna do that by saying I need to be, ah, provided the brand when I create the new and the new car. Instance someone and then here again. And what that innit method does is it's gonna set the sell brand toe. Whatever you passed in, Brand here. Now I'm gonna create another one. Another method. This one is not a magic method, which is called Get brand. And what that does is it's gonna just, um, print the brand. Okay, so now, um, if we ah, execute this. So let's say we're gonna have our first car, and it's gonna be, um Let's say it's Ah, it's my car. It's called it my car, and it's gonna be a type off car. Right? Well, remember now we need to instead of putting to parents is like we did before. We need to pass this magic method in it and they need by the wedding. I don't know if I said it's too underscores in it. And then to underscores all magic methods. Have that that, um um, that protocol you need to pass to under underscores. So ah, here. We're gonna pass Toyota as the other brand. So what happened? Basically, I create a new instance off off a car class called my car, and when I initialized that I need to a needed to pass this this brand. So this this Toyota string is gonna be assigned toe that um viable that were a defining on the in it. And then it's past here to the self brand, which is the the local or the local viable brand that that car has. So now if I do my car dot get brand, it brings Toyota. So that was said properly right? It is the same thing That's just accessing the brand directly here, which is Toyota. But as you noticing that in the top one, the top example, it's not. It's not returned with quotes because it's just printing out on the screen versus this is actually the viable Toyota. So that's, Ah, magic method. There's many more one that we're gonna be using on our owner examples on the database. Um, it's ah, when called ar e pr or reproduce, I guess it's ah, it stands for and that basically will print Ah, the representation off that record off the database record on the terminal whenever you you pull it and there's their soldiers that we're gonna be looking at as well. Okay, so now let's look at our inheritance so inherent and is basically I want to create a new I knew collapse, but I don't want to like, let's say, do this all the definitions and all the things that that class already has, so I want to build on top of it. This is very common when you like. It's a working on an open source project, and some other person wrote all these Great. Um, you know, functionalities around. That's a communicating with a ah, with an Arduino um, on your, um, that you can connect your laptop, Um, and you want toe build from our toe on top off those libraries. Then you can inherit those libraries or those classes that that library defines without having to repeat all the work that that older developer did. So this is a very powerful thing that object oriented programming allows us to do in Python . So let's say that, and I want to create a new vehicle type of class called truck, But this time around, instead of leaving it empty, I'm gonna pass car asked the, um, as the parent class on there's like a it kind of like a parent, um, child, um, kind of like relationship. And there So this class truck, when I do that class truck car, it's inheriting the innit? Method and the get brand method, so I can basically it's basically the same thing. But then I'm gonna, like, defined a new kind of, like method on top, which could be, let's say, Ah so car or base or rather, this tocar so I would have a tocar method. And I'm going to say when? When? When I call that, um, it's gonna print out towing car, right? So that's it. It's a very, very simple classical truck. So now I'm gonna my I'm gonna define an instance of that. It's my truck and I'm gonna pass truck as the class. But notice that I need to pass the the brand because it's it's basically that is required when when I defined, Ah, the truck class as a subclass, um, off the of the car class. So let's say this is a four truck, right? So see that it didn't give any air or anything. It's it's It's fine if I get in my truck, um, that get brand, you'll see that even though I didn't they find that method in here, I still have it available, and it says Ford C. But my truck has a very specific thing that it can only do not. The CART cannot do that, which is a tow car. So if I do talk, are here, it's a stolen car. What do you think happens if I try to tow a car with my Toyota, which is my car? Any guesses? Well, we basically get in there because it says that that car object has no attribute. Tocar and it's it's saying, attribute. It's basically like a method, but that method it's not available. So that's that's what happens to that original class. It's not modify at all by this new, um, new method that truck is defining. 20. Modules: Okay, So, um, now we're going to see how to do this kind of operations without using the, um, the python terminal. Um, and basically, we're gonna do start creating some files. So to do that, I'm going to create a new directory. Um, so we're here in work spaces. Eso We're gonna make a directory called module tests, and, as you can see, there it created model tests. Um, and there's no files in their asses off now. So what we're gonna do is we're gonna create, um, within that. So I just control, click or out Click. And we're creating a new file here, and it's gonna be cold car, not B Y. Uh, usually you name the the files as the class they contain, but it's again. It's not a hard roll. It's not required. Bought? Um ah, we're gonna do that here. So now remember how well we're attack of before we were doing, you know, glass car. Um and then we're gonna say in it self brand, and then we're gonna do Ah, self brand equals brand, whichever you you're passing there. Um and they were gonna do another a method called get brand. I mean there were gonna print. Ah, self brand. Um, so now we save it. But when this little gray little ball there, that means that we have unsafe changes in the file. So make sure that you always you always see that little X instead of the little great ball . I don't know if you noticed. When I pressed the safe, it went from, like, yellow to green really quickly. Ah, yellow means that it's like the request is going out. And then green make make means that the server, the Cloud nine server. Actually, I was able to save the file to list right now to interact with that with that class. And so before you do anything, uh, we're not on that directory model 10. So make sure that you're going inside and you'll see why, Um, a little bit later. So another were with same module test. I'm gonna call the interpreter. Um, the terminal and we're gonna now be ableto work with that with that class. So the former for that is you usually put from file name without the P y from car import car. So I'm telling python from the car p y file import the car class so that I can do stuff with it. So as you can see here, a new directory popped up here called Pie Cash on the score, on the score, by cash on the score, the score, and within it you'll see that he has some, like strange file with That ends in P Y. C. And it's basically this is the compiled version off this file of carp. Ey off all this, and it basically allows Python to execute this files faster. So that's why by Chinese are very good language in terms off speed, because it will use this compiled versions of your foul instead off having to run the file from scratch every time. So now whenever I execute anything from Carpi, why it's gonna look first if it has a recent compile version of it, and then it'll it'll little done instead. And if he opened that foul, you'll see that it's all giver it. So it's all like machine code, so something to look for. Also, make sure that if you just get to ignore underscoring the score pie cash on the score in the score directories because there's no use for you to put this within your repository. So now I should have the car class a, um, available to me in memory. So I'm going to do the same thing I said it before. I'm gonna do an instance off the car class with the brand Toyota. Um, and now if I do my car, get brand and you'll notice here that I completely with with other, complete with tap. So say this. If a president the top here, it already knows what what methods it has available. So that's pretty musical. So if you do this, I'm going to get to Europe. So remember how I told you to going inside that directory before. So let's exit. You can do control the or type exit like this, and let's go back to the previous folder directory workspace and let's stop by from three. Right? And I'm gonna do the same thing from from car import car. I get an error. It says No module name car. So this car file it's not being able to Toby loaded on the, um on the directory from that directory mean So what's happening here? So, basically, if if where you run python from makes its a lot off. It's very important. And you're going to see no module name. Block a lot in your careers is just, you know, try to do some common sense and ask yourself, where am I running python from? Are the directories that I need? Um, for those, um, available to me. So the way that you fix this error is basically, you need to check that you have a very magic another magic, Um ah, thing here. But it's but this time it So it's a file, and it's called in it b y just like they in it on the class. Now we're gonna need to create an image file within model tests. But before I do that, let me delete this. And one thing that you'll want to always, um, do in your in your work is to be organized well organized. So when I delete the by cash file and I'm going to create, I'm gonna see the toe module test. And I want to create a director called vehicles because when I want to do is I want to put all the, um, the classes that have to do with vehicles within this folder. So I'm gonna move Car B. Why? Within vehicles? OK, so now vehicles has that and for all, still be ableto call vehicles A module. Okay. I want to create a file called in it B y. So new file. I'm just going to score in it that Ah, and it I'm just going to score p y. So it's on this going to score in it on this Going to score dot people with this file, you don't have to put anything in it, although you can. Technically, um, gonna say yes to this. No, don't. Don't keep it. That's the old carpet. Why? Okay, let me open it. Here. Here, you can put also code. And whenever you import from that module, that court is going to run. But for this this time, we're not gonna do that. Okay, so now notice that I have, um, that vehicles holding their if I see what's inside vehicles, I have to any people on the carpet wife. And I'm gonna run by fun from the top folder module tests. OK, now you're going to notice how the foreman is going to change a little bit. I'm going to say from vehicles dot car import car. So what's what's Python saying? There is that from the vehicles module And what defines that as a module? Because it has an NDP. Why fire within that folder? So it treats it fits vehicles as almost as if it was a P Y file itself. You see what? That how interesting that is. And then you're importing the car file. Ah, um, on inside that vehicles module, and then you're going to import car the car class. So now I can do the same thing. My car equals car Toyota, and then my car get brand. See, everything works perfectly fine. So I'm running by thumb from here from module tests. Um, what I importing from vehicles? Carpi? Why the car class? Okay, so that's something that it's really important for you to, um, manage and to be able to master on something that even, you know, it's us. And experience developer, this model not found. Thing is you're gonna have You're gonna have it. And you just have to, like, stop for a moment and think of yourself. Where are you going? Running python from those are called a path. So you can. You can also like the fine paths that that can be searched. So so to speak. We're going to see that through other course, but But remember to have that in it b y file on your module, so you can you can do stuff with it. Okay, One last thing that we're going to see here in this. Ah, this lecture is Let's say that I want to do by found three and then import the dual that I want to like Run is correct. That does all that for me. How should I do that? So we're gonna create a new file at the level of model tests, and it's gonna be cold. We're gonna call it wrong people. I okay. And what's wrong? P i b we're going to do You're gonna be able to basically do what we said or we did on the Python interpreter, but problematically So we're going to do the same thing that we did before from vehicles car import car. Um, and so now we're gonna do, um let's say my car equals car Toyota, and then we're gonna do my car. No, my car. Get Brent. This is something really interesting. You see how Cloud Nine's editor is Is kind of loading the models or the methods? Um, it even is gonna, you know, tells me, like, what? What parameters do I need to pass and all that? So this is pretty useful. It's kind of like already giving me hints off what methods are available. So that's that. We're going to save that. And now instead, off loading the Python terminal, we just do python three Run B Y. And there you go. You get Toyota. That's what it just ran all that. All those commands and it printed out to yoga. 21. Parameters: Okay, The last thing I want to cover here with with our fight. But python introduction is function arguments or in this case, classmethod arguments. And you'll see this a lot in working on a daily basis. So there are two types off functional arguments, um, or function arguments rather. And they're either keywords, um types or positional times. And what that means is that when you have, ah, method like get brand in this case, we don't have any arguments were no, we don't require any arguments to be passed. However, there might be an instance where you want to have, um, another function. And it's a that's great one that's called open door for our car class. And in this case, I'm gonna say, Ah, door number is a is a parameter or ah, one argument for that for that method. So what that means is that you need to pass in this case a an open door function, toe that class toe or rather that car instance, and define what door number you want to to open. So let's say it's one for the driver and two for the to the side of the driver's side and then three and four for the two in the back of the car. And, you know, I'm assuming that it's a four door car. So in this case, I'm going to just say, um, let's do a print statement and and will say Opening door and then, um will just tack on the door number and let's make this is us three str because it's ah, it's a number. And that's the way that we should be able to to go incarnate those two strings, right? So we saved that eso here. What I'm defining is that the door number is actually a positional argument because it's the 1st 1 Okay, and we'll see. We'll see a better example when I do another one, but let's some let's play with this. I'm just gonna run the terminal and, um, let's say from car from vehicles, the car import car Oh, I think I'm not in the right directory, So let's go to model tests and try that again. So there we go. So now we have you know, that's a my car, vehicles and instance of car, and I believe now we need to pass a sparkler in it. The the brand, right? So it's a Toyota. So if I do my car get brand, um, we'll get that, but notice. So I'm gonna I'm gonna try to open a door, right. So let's say what happened? The driver's door. So I say, open door. And if I don't best anything, I'll get in there because it's saying it's requiring one position argument door number. But that means is that it knows that the function requires that the first does the positional moniker here. The first argument needs to be some sort of off value that that the function needs. So I'm gonna try that again. I'm gonna say my, uh, my car, Open door one. So now I get opening door one. If I passed too, I get opening doors so you can see that that viable has been assigned here. When, when I call that function and then it's it's printing it out here, and it's kind of like available for that for that function from that from that point on. Okay, so now let's destroy another. Another method we're gonna add, which is gonna be, um, we're gonna have a ah function or a method for these class that allows me to turn on or off the, um, the blinking lights. OK, so let's say deaf, um ah, blinker control. And what we're gonna pass here is gonna be left. Ah, blinker and right blinker. So I'm gonna have to say it's either on or are off. Okay? And here I want to keep, like, the status off the blinkers in the kind of like in the class itself. So I'm going to say self left blinker equals left blinker self right. Blinker equals right blinker. And I'm going to just say, um print, uh, I want to say left. And then the status off that linker and then right, str so right to blink, Okay. And, um, close that. So what I'm gonna do is I'm gonna actually check with an if so that I can turn on and off. So if left blinker, um actually, no, that won't work. And I will tell you why in a second, So we need to pass both both of those functions. Okay, so once again, I'll do the python here, and I'm gonna import and put an instance off the car. So now I'm gonna do my car dot blinker control. But I need to pass left Linker equals, say own and right blinker equals off. So now it's saying that the left blinker is on in the right Blinker yourself, but you know, in when you're driving the car, you don't really tuggle or or passed both of those things. You either passed one or you pass the other. I either little kind of lever, right? When you put the lever toe the, you know, upright position, you're turning right. And then if we put it on down position and it's it's you're going left, so I wouldn't make the function kind of like behave a little bit like that. But the problem is that I need to pass both. So what happens if I just pass? Um, the right wing or the left blinker? Only I get another. It says missing one required positional argument, Right blinker. So you see here is like saying I need to pass the ah, the 2nd 1 And so that's kind of like annoying. Right? Um, So what do we do there? So we have the option to do something here, which is we convert this instead of being a um, a positional argument we're gonna do This s A S. Ah, it's ah, keyword argument. The way you do that is you can put you can put an equal signing here, okay? And then you can pre ah, load the the value off those off those arguments. And, um, and from that point on, it's ah, it's gonna be an optional or or or basically you can either pass it on or off. But, um, let's just start with this one with the right blinker. So you see this how this works. So I'm going to say the default value of right blinker is going to be off. Okay, so if you don't pass that it's fine. It's gonna be wife is going to say All right, wing was impasse. So that's fine. I'm just gonna Preed defaulted toe be off. So you see that in action? So we'll do this, um, important this and then I'm gonna say my car is car Toyota and now I'm going to just pass the 1st 1 which gave us a never before. So let's see what happens. See, now I can pass only the 1st 1 I mean, the left blinker one and everyone and I didn't need to pass. So that is from that point, it's It's called a, um, a keyword argument because the keyword is right blinker, and it already knows that that might be or might not be passed to the function. One golden rule that you need to know is that you always put past the positional or required parameters to the left, and then they once are optional. Um, or key were arguments to the, um so the right oh are basically trailing. So so always list the ones that required first and then the the ones that are optional. Or have a pre set value toe the toe, the trailing side. So now I can really do what I wanted to do at the beginning, which waas basically have this kind of on a total basis. So if I put both by the fault are off, I can say if left blinker equals on, then set the self lit linker to to on and then put the right blinker to off and the same thing. Um, if right linker, then that's like else. If else if right blinker is on, then you want to um, basically put the right blinker toe that value and turn off the left Linker if he was on already and take this out. So now the function is gonna be If I passed the 1st 1 us on, then the right blinker is going to be turned off. And if I passed the right blinker on, then the left thinkers gonna be off. So let's see if that works. Um running stunned, she ate the car. Perfect. So I'm gonna do my car. Ah, blinker control. And then I'm gonna plus left blinker equals on. So what? What happens here is in passing this on. So even though the, um, the right blinker value can be be on, it'll be turned off. So let's see that Words. Okay, Perfect. So now I have the left lingers on and the right one is off. So what happens if I instead buys the past the right blinker? I'm gonna turn right, Right. So now they see the life. The left one was set off, and, uh, the right one was turned up. So I know there, uh, quick thing that I want to mention is that if you can, you can also pass or make this parameters be none. And what that means is that there might not even be be passed, and they're gonna be initialized with with a non value. So that's that's the way that you do like you. No parameters that you want to be able to pass or not pass. It doesn't really matter. And you can, um and you can make, you know, check if they're sad or whatever they can be. They can be passed the last thing. And again, this is this isn't for you to remind this. Like, if this is a required one, always put it at the beginning, and then the ones that are not required to the to the end. So that's it. I think we're ready to start building our Fleiss application and, you know, play a little bit with this and and have, um, you know, make up some some fun, more features toe our car class. Um, and you know, if you have any questions, let me know 22. New Workspace Virtualenv: perfect. So we're going to start our flask, Um, adventure here, and we're gonna start by creating a new workspace and, um, basically start our first flask app. So create a new workspace. You're gonna have to define a ah project name. Uh, and we're going to call this the, uh hello? Up. Um, you can put out Trump description there. Uh, there's no need for that. You can also, um, make this private or public for for paying customers you can do. Um, I think more than one. If you're like a free customer, you can only the one private Berber account. And you can also clone from existing get, um, repository, but for us, we're going to start from scratch. Um, so there's also these templates here. You can do, you know, custom. This is Theo Bone to basically logo. A custom open to installation. You can do which they may have five. No Js. All these things were gonna just use custom here and then create war space. So now it's basically creating a new server for us. Um, and, um, it takes a little while while it's creating that container, which I, as I mentioned before it's using docker containers. So here we go. We have are our container there One thing that I will tell you right off the bat this. Hello Up here on, it's something that I'm gonna probably contact See Cloud nine to see if they can change that. It's a little bit misleading because it appears as if you were in a hello up folder. But if you see PWD, you're actually on home a boon to work space. So, um, this is referring basically to leave toe the workspace folder and not the, um the hello up There's no hello up folder there. That's something that tripped me off a little bit and it tripped. Also some, um, some of my some of my students there's also, like, navigate and commands here, but I always keep it on workspace there. So, um, let's create on actual folder for our our ah first project. So we're gonna do make the air, and we're gonna call it flask. Um, underscore in it. Always used, underscores for folders on you can use Stashes as well. The foul system will let you. But if you're doing python work on, you're gonna up half modules that, um, that you're gonna have to import it center. It's very to use on the score. So, in one in basically a golden rule that you wanna have from now is that always always have folder names with on this course and not not dashes. So that's why I recommended you put a dash in here. So you remember this is not a folder. So but hello up or, you know, whatever the words temporary by dashes until you remember. Okay, so now we're in the flask. We're gonna go in the flask, innit? Folder? And that's how are basically our home folder there. And the first thing we're gonna do is, um, we're gonna have, um we're going to create basically something called a virtual environment. What's a virtual environment? Virtual environment is, um a It separates each projects where you have you can have multiple projects. Were simply can have a flask in it. Project that you can have another like, the flats block problem that we're gonna do afterwards. Is it gonna be another folder? And then each folder it's gonna have its own set off libraries, um, version off python, and you gonna be able to run all this in the same server. So that's a pretty big deal. It's It's a great thing. And it allows you to track which which libraries? You installing that folder by using something called A requirements file. But don't worry about all that. We're going to see that through other course. For now, Um, just make sure that you created your folder, and now we're gonna create this virtual environment. So how do we create a virtual environment? It's very easy. You use virtual and which is this command that, um that is, um allows us to create this this this separated environment. And then you're gonna dio basically you put a folder here where that well, where those files, those libraries, those python versions we're gonna live in. But because we're using Python three and boom to or at least cloud nine, you can check the version here. But if you do python, that's fate. You'll see that it's by from 2.7. So we want to do a veto environment that uses by from three. So for that you do virtual and then you pass Dash B, which is python version, and you put by phone three. That's basically the execute herbal that we're going to use on. Then the name of the folders gonna be ve envy. Um, you can put any name you want in there. However, it's easier if you use it always the same the same name, because you know it's gonna be second nature for you, too, to always activate this virtual environment, and you're gonna see that in a bit. So virtual em dash be Faison, three VND. So what happens there is that it creates a new virtual em with with python three within the flask. Innit? Folder. So now, um, if we see here, see, you'll see that it has a new folder called Virtual Environment. If you're looking there, you're going to see Ben, which is the basically some commands that retirement needs to execute another. See Lib Labour's world. The virus will live and if you click here, there's python 3.4 and then side package is going to be another important one because that's where the older packages or the libraries and when stole, are going to be installed at. So, um for now, just know that we have that V and V p um folder in there. So now what? What What is it that you do? You basically need to activate this virtual environment. Activate means that from now on, all the commence for installing things and for running things are gonna be done as if it a CIF This environment was your whole server. So the way to do that issue have to memorize days, but you're you'll remember That's you Do it over and over again. You do Source V E N V. Which is the folder with it before been activated. Okay, I want you to look at him. This here. You see how now he has a parenthesis at the beginning? I ve envy. That means that the virtual environment is activated. Do not And I repeat this in like red front Bold letters. Do not do anything with your project unless this V envy thing is on the on the beginning off the of the foul, um, structure or or the prompt, because otherwise bad things will happen if you, you know, install packages and that V and there's no turn on. He's not gonna install them here on B and B. It's gonna install them side wide or server wide and a lot off things will happen. So remember, um, always have that V and be there. So let's how does the act How do you deactivate? Well, once you're activated, you just type deactivate, and you'll get out. See, now there's no being and be there. Um, but remember your you know your had your coffee, your open up your cloud nine. To start working, you go to your directory flask in it or whatever your product is working and without, don't even open the editor. Don't do anything do source than being active and press those types. You just have to play like, type like two letters your spreads tab, and it'll order complete and make sure that you have that ve and be at the beginning. 23. Pip Install Flask: Okay, so now let's install flask. So flashes a basically a collection off libraries. And, um, remember that we have the ve envy in there. That means that we're good. Okay. Do not do this with without that. Also, just to make sure that you're on the underwrite, the bright version of the iPhone, and that the virtual environment installed the right version. Just a python Dutch, be if you don't see 34 You see, you know, two point something and you don't see three point something that your you know, your put your very own environment. It's not defined, um, or activated. Or you didn't pass the dash p flag properly and he didn't find a python. Three. Execute herbal there. So hopefully that's not the case. You should have python three 0.4 or something similar in there. Okay, so we have that. So how do install packages? We use something called Pip on. That's the bike by phone package installer that allows us to easily just, you know, do pip install X, and it'll go in the, you know, the internet. Look for that package, and then if it finds it, then installs it locally and it's gonna install it again in this ve and be labor python. Three side packages. So I'm gonna leave that open, see if we can check out when flask is installed. So, like I said, you do pay ping stall, and then we just have to pass flask. And as we press enter, um, let me put this a little higher here. Um, you'll see all the packages being installed in there. Yes, so you can see here. There's now flask. Ah ah, Flex folder. There's ah ah Jean job, too. There's a markup safe on. If if you read through the funds that were being stole the same thing you you see that, um, there was a flask voreqe soy ginger to its dangerous markup safe. So those are old libraries that flashed requires for a two to work properly. But, you know, you should see something that's a successful installed flask and all these other files if you got any errors, you know, try to check if you're missing something. If you mistyped it. Ah, etcetera. You should You should be fine. Um, And that means that now flask is is available. Um, so that's it. We're gonna now in the next section, start building our application or our first flats application rather step by step and let's ah, let's go there. 24. C9 Python Path: So I wanted to do this clarification because there's something that cloud nine recently changed in terms off there. Um, linking on. Lending is kind of like a a program software that is constantly checking if you have any kind of, like mistakes or errors on the on the code that you're writing. And they recently they're being kind of like improving their by phone support for, you know, things like, uh, maybe I and, um better, better kind of like linking and support. And one thing that introduced that it's not, um, doesn't help. Uh, kind of like our The way that we've been writing the code is that, um they now you need to like, um, specifically, uh, tell them or tell the editor the path where your virtual environment libraries work, and I'm gonna show you an example. And so you have on idea of what I'm talking about. So I have this, like, test import, um, workspace, and I'm going to do a new file here, And, um, so I'm gonna tow um, actually, let me let me save it. Then we create the file from here, and you file and I'm called going. Dad. Hello? p y. So I have a visual in for Eddie that have that has flask imported, I mean, installed in it. And as you can see here, if I go to side packages, you'll see that I have. I have flask in there, right? However, um, there's a little problem now that, um if I if I go ahead and start doing my code and remember that you need to like, um um source, uh, virtual environment. Activate here. But that's even before that. Um, if you do from ah ah flask import flask. Right. And you try to, like, continue working in your code base. Um, you'll see that you eventually get, like, a little like red. Uh, sign here saying, like meaning that the court editors not finding flask. Um, sometimes it takes a little bit to appear, but, um, it's if I can open another c opened. Read me here. I'm looking at Harris This here and see. I get this, like, unable to import flask. And let's forget for about this for a moment. So what's happening here is that it's, um the editor is not able is not finding the where the what That flask libraries, and it's currently looking just at the at the current path, uh, where the python path that they have pre installed, which is the one on the, uh, on the main server. So what we need to do to have that disappear and you'll see right access for other things that are imports from your virtual environment packages. I spoke to Toe Cloud nine and they actually, like were very helpful. And try to, um, kind of like work with me this this issue But what you need to do to get the red the red Xs go away is you need to like it's explicitly tell the editor where the side packages off your virtual environment are, and the way you do that is the following you go to the re envy go lib. So open your live here and then goto bison 34 right, And then go to Sai packages and they're right clicking there and use Put something that says open terminal here. If you see that, um, it will take you to the toe, the toe, that path and then you just dopey wt which is, you know, person working directory on you get this this this this path, this is the path from the beginning. I mean, from the root off the server to your side packages off your virtual environment. So just make sure to copy their, um um command, see or control. See? And then you go to the gears here and where it says, um hints, and I mean language support. You see, there's a bison path now in there and you see this there's there's two patsy there. Those are the ones from the from the server. Make sure you go to the end of the line in there, put a semi colon and then paste the path of you. Copy it. So you see, here it's the sack package, A spice on 34 the envy live, etcetera. And, as you can see here, it has the same. My column would be before there, the one that was already there. So you press enter. You don't need to do like, say, for anything. It automatically saves that and then close and close this one as well, and you see that this is going to go away in a in a minute. Um, because now it's finding the path were for flask, so you can continue working here. Um, and it eventually goes away. See? So that's the thing. It's a little bit of, ah hassle, because before, at least from when I was working with it, they didn't have that that problem. But now you need toe do that. You need to set your python path. And at the, um, at that library, um, that side packages path on if you see any, any any little right access with any of the packages trying to import, kind of like look at where they're stored And if you don't have that kind of liking here, but it's installed somewhere else in the spice and 34 um, kind of like directories. Just go ahead and put another semi colon and at the path where the containing path or folder where that libraries. So that's the update. Hopefully, that will solve any problems. And if you have any issues, just just think me 25. Minimal App: Okay, so let's start our ah flask. Um, journey here. Um, you should have the handle app workspace ing there. Um, we're gonna go ahead and open it. Um, perfect. So now we have we have our virtual environment, uh, within the flask. Innit? Folder? So remember, Hello? Dash up. It's not a folder. It's just the name off the, um um off the server of their workspace and a flask in it is the actual folder that we've created. And then virtual environment is the virtual environment. So before we do anything, remember what we need to do here before you start coding or doing any any kind of operation . That's right. We need to activate our virtual environment. So you do sores Vande being activated. And remember, you have to be there on the flask. Innit? Folder. So if you by any chance it's ah, Control. See that, um, it's a your in your workspace here or in home. You do see the workspace flask in it, and then in their you do the source. Um, sorry. Sores. Um, van being active. Okay, Now that we have RV and be there, that means that we're good to go it's clear the screen and the Stipe start typing our first flats application. Okay, so let's create the file where we're going to start the the application. Let me close this. Read me because we don't need that. And the late the file, um, the leap. Great. And I'm gonna briefly disappear the dis terminal by doing control escape. So let's grand your file here, and it's gonna be called Hello, P, y make sure that it's on the flask innit? Folder, and it's on the same level as they ve MB. Now, let's double click there to open it. And so the first thing we're gonna do is, um we're gonna type from flask import flask as you remember this from the modules lesson that we just saw on the previous section. This is just saying from the flask file import flask. Now, if there's no flask director here, how's it finding that? Well, so the thing is that we've seen a spot of the V envy activation, um, python eyes said to look for files within this side packages a swell of other directories. So that flask, um um, file or folder? That's being, um ah, reference. There is actually this this flask here. So that's how it's ableto find it. But let's continue from flask import flask. Um, usually there's the first thing that you need to do on the flash application is to define an instance off flask class by doing this line up equals flask and then a magic meant method called name on this Going to score name on this Going to score this name or the department or that's being passed here is just to make sure that you have on unique application, um, name, um for the for the flask instance. And on this kind of score name just contains a string that, depending on where you call this file, is gonna be either underscore underscore main underscoring the score if it's called from the terminal, or it's gonna be called hello if it's called from the from the python terminal. So basically just side that is usually what you do, um, to make sure that you have a unique instance off that off that flask app. Um, the next thing is, we're gonna define ah route and around it basically, the your l that you're gonna be hitting, and then we just put it in here. So you see, um, this little funny at sign of the beginning off this, um, off this method off the app instance is called a decorator, and a decorator usually means modify the next line or the next thing that happens after this, Um, with some sort off behavior and what we decorate is these function called Hello world. Okay, now, don't don't don't worry. If there's a lot off new things happening here, you'll get the hang of it as we continue to play with this. But we're basically they're finding a function within this file called Hello World, and we're gonna return. Um, hello, world. I don't believe we've seen return, but return basically tells a function when it's completed. Um, Or when he gets to that point, just throw that whatever viable or string or, um, uh, expression you have after the return back to whoever called it. Um, and we're gonna see how that works in a second. So now we're gonna say if name, which is the same magic thing that we have here. The magic method. If name equals main, then that means that were being called from the from the from the terminal from the from the command line and not from the python. Um, terminal. Um, so then we're gonna do app that run, and we're gonna basically leave that I say us this is to print it. So what run does It's basically create a server. That's all the time waiting until somebody hits a on address off that application. And, um, and just, like, routed throughly through this routes to whatever function it matches in terms of the folder . Um, however, since we're working with with cloud nine, there's a couple of things that we need to add in here. One is, um Before I do the import there, you need to refine a host. So we're gonna do this by saying host equals, um os that get environment viable. I p comma 0000 And this is basically saying the host for that For this, for this application is gonna be either the i p that's passed by Cloud nine just like a preset environment viable. That contains the servers I P. And we also need to define a port where this is going to run. And in in this case, we're gonna have, um, convert toe into Jer the, uh, environment viable cold port or past 5000. Okay. And then we close this, okay? And now we have we passed this as host equals host, comma port, equal sport. Okay, so you see how I'm getting, um this this errors here? Um, basically, I need I need to have always available, so I'm going to tow import. Oh, as and we should be fine. Oh, this air here saying on the fine, viable name. Yeah, I'm missing another score, so see how cloud nine centers? It's telling me all the time, Like if I have errors, that's very, very useful on the heirs. Are you city? Very expressive. So, um, that is ah, pretty Brickell thing. So let's save that, um, control s or command s if you're on the Mac and we should be good there. So whats happening again? Um, we're importing the flask class and extension eating an app or application using the underscore. Underscore name. Then we're saying if anybody hits the slash, which is like the top domain then executed this function and the function is called hello world and it returns to string. Hello, world back to the person that that hit that euro. And then this is like the main kind of like a runner part of the script, which is, if name is Maine, which means that it's being run from the terminal. Then we're going to define a host, which is the Cloud nine host and the Cloud nine port, which is Ah, the I p viable if it's set. And if not, it's 0.0 point 0.0. We just an I P. That's, ah, you know, basically explosives, the application to the world and then port Is the integer off the port m environment viable or 5000? Which is the flasks? Default. If you were running this on a kind of like a Lennox machine or or Windows or Mac locally, you don't need these two things and flash, but basically run by the fault on the 5004th. But that's, um, that's all you need to have for our first application. So let's see how it runs. So do control escape again, eh? So we can bring up the terminal and remember, ve envy has to be there. It's activated, and you need to be on the flask, innit? Directory, and you need to have your hello p. Why they're your B m v. Um, so basically, we're gonna do Python. Hello p. Why OK and hit Enter Perfect. So it says now that it's running on HDP zero points Europe, once you're aboard 08 80 which which is basically like it's running for every anybody that hits the port 80 80 which is the Cloud nine's development environment. But it's not really, um, you know, it's not really the, um ah, that port on the outside, it's it's bright running, really on the port 80 which is the normal, you know, kind of like your URL for extra P services. So how do we see this like, there's no how do I kind of like, look at the running application. There's this. Previ and run bottoms do not work with those. Preview just allows you to like preview a file, annexed email or static content and run executes the file, but unfortunately it uses the python 2.7. That's not our virtual environment, and there's a way to run it, using the kind of like you can modify what python execute herbal and you could potentially define despite unexcusable here and vent. But I haven't found the time to do that yet. Maybe a student that has a little bit more time Can ah, figure that out and put a example there in the in the forms. But for now, the way we see where it's running, where you go to the share here on the top and you see that you have on enter application and preview editor is to share your your code. Um, and application is the the actual were all there we want to look at, Which is these this string? So it's basically you are work. Um um, your ah, your projects Name dash, your user name dot c nine dot io. So if I clicking there and I click open, you'll get another tap. And there you go. We have Ah, hello world, Which is Ah, basically our first flax obligation running. Congratulations. 26. Starting with Git: okay, There's our beautiful flats application running. And as you know, um, um, I'm a big, um I recommend, like, really strongly that you they learn and, ah, use Get as your version control. If you haven't heard about get or you don't know about get a lot, um, there's definitely of course I want to recommend to you, which is the are owns essential get course, which is also here in you, Timmy and basically essential get course teaches you like the basics without going into, like, you know, cause Skip gates are very complicated tool that you can do a lot off things with here. In this course, I'll teach you basically the very essential things that you need to to have as a developer to work on a daily basis like there's some other, more specialized things that you could potentially do. And you can learn after you do the course. But a taste for basics stuff and to get your your day to day operations for forget, basically recommend that you do this or, you know if you want. You can also look at other get courses in una me or on the Internet, but I went out like that. You are quickly how we would do that here. So whenever I started project, I initially, you know, from the beginning, I just start getting my my get set up. So, you know, for those for us to do that, um, you have to be on the root folder, and you do get in it. That basically creates a get, um, I get in. Ah, folder. Um And, um, we want to have a did ignore file so that we don't have, for example, those python, um, compiled directories or things that we we we usually do. So I have a, um a starter gettinit file that we can use, which is the following. So being a new top here and just go to a guest. Doug, you have that come slash from serie D you and you'll see a get ignore fouling there. This is the getting nor so you should start with this one. Um, you can put raw here, and then just copy and paste that. And so we're gonna create at the same at the ruled level. Um, new file dot Get ignored. No, I did that file and put this which is a good starting point for funds that we usually don't need to check into a repository. Perfect. So now I'm gonna do get status to see what? What? What's new have to get Get ignoring him. Hello, P. Why I at those files in there, and I'm gonna commit this with first coming perfect. And now I can basically push this to get hub again. If you don't know any of this, that's fine. And you can continue the course without it. I strongly recommend, though, that you get yourself started would get. And, um, you know, let me know if you, um you know, if if you have any questions on how to get the course with with a discount, um, but definitely will recommend you to get get a spy off your professional developer tool set . 27. Debugging: Okay, so we have our application here. Um, with the hello world Askew can see everything looks pretty much the same. If I see here, I see Hello, world. That's perfect. So I'm gonna do control escape and put the make the application run again. Um, and there's the other world. One thing that you're gonna be finding yourself doing a lot is editing code and then checking the of this. Ah, if this change looks good and nothing is breaking So let's say we want toe change world to Hello, everyone. Um, I saved the file and then go and reload the page here, but I still see Hello world. So what's happening? Well, what happens is when this page is already, um, let's say this application is already running. Um, this, ah, this code basis not being updated because remember what I told you about having the application being cashed, which means that it's it's kind of like on a static mode, and it's just retrieving the the most recent version of the code base. But it's not really, um, reflecting the changes in there. So that's that's the pain, because that means that I have to like control. See here and then put python Hello, p y again. And now if I reload the page, then I'll see my change. But I don't want to be doing that every time. Right? So again, let's save if I change this through Hello world, save it And then we know the page Nothing is there. So one way that we can go around That is, um, by passing another flag and that flag, it's called debug. So the way we do that is by doing the following, we just have to add a We referenced the app, the app instance. Which is this happing since here. And, uh, let me clear that and then you put the bug equals true. Now you don't want to do this on a on a production environment because app debug actually is lower. And you will also are us weaken. We're going to see later. It will display the errors on the page. Um, I say as they occur, and we don't want to do that for, like our public facing application. We just want to, like, display Ah, a generic, You know, 500 error or application there with some like you know, nice image instead of like, spitting out, You know, the by phone error, stock trays. So let's see how that works. So I've did active articles. True. Let me bring up the, um, the server again, and I'm gonna run this again. Python had Opie. Why? So if I refresh here, it says hello world. But notice here on the bottom as I changed this to Hello, everyone. And then save as you can see, it says detective change in this directory reloading, Restarting with that so I don't have to stop and restart the server. The changes are reflected just by pressing refree Load on the on the browser. So that's a big help. And that, um that also helps you, you know, continue working on your core base without having to like, doing control. See? And then we started again. Another cool thing that we can do here is, um, when you have Abdi particles true. Well, let me show you before without the Abdi particle structure, let me save this, um, any control, see, just in case. And so what I'm gonna do here without the active illegals? True. I'm gonna introduce in there. So one thing I'm gonna do is like, let's say I'm gonna put I equals three, and then I'm gonna put, um, you visited. I need to put dollar courts because I need to put a You have visited something I'm gonna put, um, I times. Now, if you remember, um, from the, um, viable strings section, we cannot put a plus I here because this is an integer and this is string, so that should return in error. So what happens when you don't have to? Barnicle's true, and I'm gonna reload the page. I get on internal server error, which is this like how it would look on normal size and that's preferred instead of like, showing toe the public users What happened there's, like a more, um, like, nicer communication toe. The the user saying something happened and, you know, there was something unexpected. But when we're developing, we want to probably see what's what what went wrong. So, um, again, we are gonna put the barnacles true here, and we're going to run the application again and see what what happens. So see, this is a much different error message. Now it's actually telling us where the air is, and this is called the stack Trace. As I mentioned, it basically says, you know, from from the very in in ward or internal peace, What what happened on do usually just go back and kind of like escape until you get to like the actual application. Although after you've been doing this for a while, this this, some of these things might make it might make sense, but, um so here's the air that we really are interested in. It says type air can convert int object to string implicitly. So that means that I need to put a conversion of that I toe string before I can add it to those two strings. So that helps a lot. And this is something that, um, that we can we can definitely use while we're developing, but not not for the actual like public users. So the way to fix that as you like no, is I put stir, stir I around that save, and we started automatically. And then if we reload, we see you visited three times, so that's working perfectly fine. Another cool trick that I want to show you guys is, um, something called the by Sunday bugger. And, um, I want to just do something here, E I equals I close one. And, um, just putting some extra lines of code visited equals I and then put visited here. So I'm just like, starting that with with equals three. And then I equals I plus one, which would be four. And then I just like a sign visited toe I This is all a little bit off, like nonsense, but I I just want to show you something called Dirty bugger. So on cloud nine, if you just type PDB and you see that snippet in there, you just press enter. And this is something they're gonna use a lot askew, kindof like, are trying to home for for bugs. What? What this does is it steps O. R stops the execution off the script at that point. And then after that, you can kind of, like, go forward, back forward, step by step and see what's going on. So usually you use this when you have an error and you see the line number. You put this statement maybe five lines before, or something like that, so that you can see what what what's going on Step by step on the on the application as the acid runs. So when I saved that, it already recorded this. The the change. So now when I when I load here, you're going to see something, which is the pages not loading. It says, You know, it's kind of like in, ah, in waiting for a lap to resolve. And if you see here, you'll see that now we have something strange here it says, um greater than Homo belong to. Hello, p. Y. Nine. That means that it stopped at Line a Line nine on the Hello World function and you see, there's a little arrow that's a cursor, and that's where the execution the next command is gonna happen. So it says I equals street. So if I If I can actually interact here, see you can you can do stuff in here so you can actually like Ah, you know, print I on This is gonna like not return anything because what happens is that I at that point hasn't been defined yet. But as you can see, I can interact with the program as it was like frozen in time at that at that point to advance to the next line you repressed and And what happens is now it says, you know. Hello, P Y line 10. Hello world. So that means that I equals three was already executed and this is about to be executed. So now if I do print I you'll see that I get three. So that means that this line here was was executed And, um, you know, it's assigned three toe the I. So now I click next. It says visited ical Either means that I whose I plus one we can check if I is in fact for it is that looks good. Then I press next. Now I can see what visited value is because he was already assigned to four. So, yes, you can see you can like, step by step. Look at what's happening with the application. And that way you can really hunt down any any bugs. Um, if I want to like, if I saw the air or I'm done, you compress. See which means continue and then you'll see that the, um, the program will continue even though it looks like it's executing again. So I'll do see here. And there you go. It ended the execution, and if I see here, it loaded the right page. Um, it's weird that executed two times. I'm guessing because I hate reload. Maybe, but usually that when expression, when you press, see either it goes on and executes that one time. So, uh, remember, baby, it's that really useful, really useful tool the fight on the bugger to be able to pinpoint areas that you might have . 28. Routing with Vars: Okay, let's take this line out. Um, actually, let's just think this whole thing out and we're gonna put ah, hello world again here to have it asked. We started. So now we're gonna do a little bit more off a routing and checking out how to do different routes for different things. So let's say that now, instead of having this asked the index page, I'm gonna have to use to be the hello route. So what that means is that, um, check if that reloaded, it's reloaded. Okay, Um, what this means is now that instead off off on the route, which is this, like, basically this route here now the hello worlds went up your own Hello or slash. Hello for press. Enter there. I'll see hello world there. And then we put this a little bigger. So what's happening here is that I just changed the route. This is called a route, which is the access point for that function. Um, and I can, you know, add another one here so that it responds to the home page because right now, if I go to the homepage here, it says, you know, it's a 44 not found because there's no route here in this in this in this script that responds to the to the index. So I'm gonna create another route here, AP Raut, and I'm gonna call this index, and now I'm going to return here. Index page. Okay, so that's that's that, Um, let's check out the the page. Now, let's see that control escape yet that we loaded. So if I go to the home page, I see and expect year and let's check it fellows to It works. Yeah, it works perfectly fine. One other cool thing that we can do here is we can pass virals. So the tow this routes and those rebels are gonna be passed down to the to the function. So let's see how that would work. Let's say, for example, that we have, ah, profile page off the users like, you know, Twitter does, and we're going to call that route, um slash user and then slash Let's say that that I passed likes, for example, user um Jorge, which is my my user name in this application. But I don't wanna have, you know, slash Jorge and then slash you know, Jack and all the possible routes, right? We just wanna have one route that serves the appropriate profile page for every specific user. So for that, we use this notion off virals, and we just put here slash um greater than or lower than user name greater than. And that means that now, anything that, um slash user slash something, some string or some number, it's gonna be passed down to the function. But so how does the function get that? Well, you pass it as a parameter for that. So let's say we call this show user profile, and then we put use her name here, and that user named Viable is gonna be assigned whatever. Um, number or string was passed after the the slash sign here. So and we can check that out by returning. Uh, let's put our coming here. It's good practice to always comment what the functions that also show they use her profile for Daddy User. So I'm gonna return, um, you, sir, and then plus user name. Um, we actually should put, um, string in there just in case. It's a number. Although I think that flats will make this a string event even if it's ah, it's a number, but I think it's a good practice to just wrapping string just in case. So let's see if that reloaded There it did. Um, so now I'm gonna So if I go to the index page again, it's working. Fine. So now I'm going to try you, Sir Jorge. And as you can see, it's it's spring. It's getting that viable from the euro and passing it back to the function, and it's returning it back to us. So User Hori, if I put you sir Jack here, I get user Jack. So let's read another one. But this time around, we're gonna use something, um, cold, um, casting so that we can, um, basically expect what type off virals we're gonna be passed there. So we're gonna create another one called Post, so slash post. And then instead of just putting, I'm gonna put post I d. Here, right? And that's gonna be a number. I know it's gonna be 123 a number there. So instead of just passing that, I can put I anti on front animals and it'll know that this this viable that's going to be passed here is gonna be a number. So now we're gonna put the fine show post and then passed the post I d. And now, um, we're going to return, um, post. And then what would happen if I put both Saidi here? Probably I would get I should get on air because it's ah, it's being passes an integer, but let's check it out. So now we do hear post three. That's right. So I can convert on in and tow a string. So that means that whatever we're passing here, it has to be a number and extreme, just a number here. So the way we can fix that is by putting the string around here as well. So now if I reload, I get post three. That's that's perfect. And so what would happen if I put a string in there? Let's say, Jorge, I get a 44 on this because again, Flask is expecting an integer there and not a strength so, But instead of breaking down, it's giving me a nice you know, I couldn't find that page instead off just breaking, so that works pretty well. One thing that you're gonna use, um, very often is this? Like, um, kind of like putting, adding like a string to another string and things like that, but it's not as readable. Um, And you, if you have, like many, like, like, say, if you want to put like something here else and you visited, um x times and you have to put this again X So it's is it becomes cumbersome. Right? So one thing that you can dio with by phone and exists present in other languages as well is you can do like a print four morning. So the way you do that is you basically you put where wherever that string is going to appear, you put a percent s for string. Um, and then you just ah, put up a percentage here, and then you put user name. We don't need to put the string there because this is this is going to automatically cast the whatever viable here to a string because we're saying this is going to be a strength. Um, they're saying the same thing. Um, let me put a coming here. Um, show the post with the given i d. The I d. Must be an integer. So the same thing here. But instead of putting a percent s, I can do percent d, which is digit. And, um and I can do then now percent and then post I d without anything. So let's see if that works. Um, so let's do, um you, sir or hey, Perfect. And let's put post 15 and I get that. So it's It's a much cleaner to read, and you can put like you, sir, Person s visited percent the times, right? So and then, in that case, you have to put this in parenthesis and then put, um, this this foreman So it will take like the first string is the string and the digitalis. This did it. So that works. Um um, pretty well there. Yeah. We don't want him to find Miss it. So that's that's fine. Um, so that's it for for this This Ah, this lecture. I think you've gotten a lot. You can play around, do some some custom routes and see where what you getting there. And ah, we'll talk about, um, some new stuff next 29. Url_for: Okay, so let's look at something really useful, which is called the Ural four. And euro four is is a function that allows flask to look at, um What? Ah, what the euro or what the route looks like for a specific, um, function. So what? The way that we're going to check that is the following. We're gonna let's say, let's take this one out here. And so you see how we have up up route you serves last user name here, and he returns to user, so that function is called show user profile. So what happens is flask now knows that the function show user profile is associated with this route. And there's gonna be into instances where you don't want to, like, hard coat this this route, this girl here use when I like to find the functions because you know your else can can change often. And maybe you have that girl in many, many places. Um, and and then you want to change that, that you're l You're gonna have to, like, change, especially like in templates, which we're going to see later on. So the way that we can, um, that weaken um not hard code that, but rather let flask display. That for us is through, um, dysfunction called euro for So if we do hear return euro four and then we put Ah, this basically takes at least one parameter, which is the function name. Ah, but you have to put it in in quotes. So let's say show user profile. And then, um, that would be all. But the thing is, like, this requires this specific function. Show you so far requires I use the name so we can pass that as well. So here we would pass. That's a, um, Richard. Okay, so now we saved that. Um, we have to import your for from flask. There you go. And so what happens is now when when I hit the and expect no. Is that I'm not putting you slash user slash Richard. I'm just pointing Return What? The euro is for this function with if I pass this this user name, right. So let's go to the index page here, and I'm getting an error, etc. Oh, I have to put a viable here. User name equals Yeah, So it's It's not a this. This is called a positional see here it says Takes one posters, an argument, but two were given a position. Argument means that, um, this 1st 1 is the one that that takes that position. The first position is the function. But after that, there old arguments that need to have the what the viable is for that, Um um, for that parameter that the function need. So we need to pass. User name equals rich. So let's check again. And there you go, you, sir Richard. So there's basically saying the euro for the function show user profile if the Houston in Richer is past is slash user slash richard. But what happens if we change states? Toby used her name instead of user. So imagine we had that we were referencing that in that when you have, you know, the Twitter feed has holder profiles and you want to like, have If you click on the photo, you take them to the profile. If we change user name, user to user name here in this year in this rural, we would have to change all the templates that have the that slash user to slash user name . And that would be ah, really big, painful thing. So if we used your old four, that means we just change this and it will be updated automatically. So if I press reload here, See? Now it says, use the name Richard. So remember, if you're using your elder, you're referencing a euro for anything. Don't reference, Ural. Reference the function and use you euro four. And that is going to save you a lot of headaches. And of course, you don't see that utility right now because we haven't done a lot off stuff with templates , But you're gonna see a lot off this as we continue with the course. 30. Get Method: Okay, so we're going to start looking now at, um forms and the methods that we used to interact with forms so form a form is basically something Where you feeling? Data. Um, and then you have the submit button. Um, that's a very basic functionality that any, um, web framework needs to have and and flash is no exception. So let's, um, start playing with with this a little bit. Um, So I'm gonna create, let's say, the most known or or common form is the logging for. So, um, let's see if I can create something I'm gonna like, delete this things here and let's create a brand new one. So let's say we have, um a a Prout, which is slash Logan. Okay. And here, um, we're gonna gettinto methods. So xtalpi has multiple methods to interact. Um, between the browser, the page over the former you're serving and the server that's gonna get those virals and it's ah, it's a long discussion. We're not going to get into ah, the holes expect room, but we're going to see to ones that are very important. And those are the get method and the post method for this lesson. We're going to see the get method initially. So bear with me as we go along that that route. So here I'm telling flash that I'm gonna have a logging page that expects, um, forms to submit using the get method. Um, and I explained to you what what that means. So here we have the function we're not gonna pass any viable is here in the in the actual euro, but we're gonna pass them using to get the get method. Um, so for now, I'm just gonna return a a form. Okay, I'm gonna put some uhm html here. Um, we're not going to get a lot into HTML in this course because he's more about back end, and you can read about, you know, basic HTML stuff in. Ah, another course I might do one myself. Took over all these things. So stay tuned. Um, make sure that you, you know, subscribe to the newsletter at from Zero. I o, um, or follow me on Twitter from Syria. The U to, you know, get get information about that, but ah, so we're gonna do it. Here are four. Method. Get with action, Logan. So action means where is gonna post when I press? Um, it and then my thirties get us. We were explaining earlier. So now I'm gonna I'm just gonna put all in one line. I know it looks a little bit ugly. We're gonna make it better on ah, on ah, upcoming. Um uh, lesson on templates. So I'm gonna put an import type off text and with the name user name. So that's the viable that we're gonna be passing to the, um, to the So the script. Um, I was gonna do something here, so we have done in one line. But that's what I'm just gonna do it one line, because it's it's almost done. It's going to end up toe api here to separate. And then I'm gonna do ah, button type equals submit. And then I'm going to put some meat here and button in the end form. Okay, so that's just like a long one liner that will show us user name input. And then I submit button. Um, so let's see how that looks. Let me see if I didn't break anything, So control escape, it's restarted. So that's fine. And that's to slash and log in Perfect. So now these They use the name field. So I type things here and then I can press a minute. Now what happened? See if I press omit, nothing happens. Um, like nothing that we can. We can see, however, notice on the girl. I have a question mark now and it says user name equals DF DF, which is the string that I past year. Let's put something more recognizable, like Jorge Submit. As you can see, it does something like it. It clears the field here and then it says question mark use of chemicals. Jorge. So that's what get method. Looks like meth. The get method is it's going to stuff all Divi Valls, um, on the form after a question mark and send it to that Your elder with a sign that we defined there. So basically, it's sending use of chemicals. Jorge, um told the script by putting it on the on the euro off the, um, off the form. Um, so But I don't How can I read that user name? Feel there for that. We're gonna use something called request. So we need to Let's take your all four cause we're not using it anymore and add request. So request is the way that flask handles all these requests. All this, like posts from from forms or from http actions. And what we're gonna do is, um we're gonna check basically the values that user name is gonna be within the request as a, um us on Ah, list. OK, um, we're not morally sorry as a dictionary. So I'm gonna check if there's values on the request. OK, then let's return the user name that's present in that request. So I wanted to return use her name is and then I'm gonna put request values off the use her name index. So request values is basically holding all those All those input. Ah, name with the name being the index off that off that dictionary. And then if there's no values, then we're going to return the form. So that means that the person has impressed some idiot, so we'll see if that works. Um, check that reloaded. And ah, let's try it again without anything here, Logan. Okay, so I get the form it's try with, um Ah, Sam. Some met. Awesome. So I get user name is Sam So you see the first, the first version of that page, which was this Slash Rogaine. It's checking when it hits. That are then any values on the request. And the answer is no, because we haven't sent anything. So it goes in returns that HDP um or brother html string that renders us, Ah, us A form on the browser. But as soon as I type something here and I press a mitt, it sends that back to the slash again with the question mark is the name Jorge. And then request values does have a value. So it goes into this block, which is returned user name is and request values off your surname. So that's how you interact here with, um, with that field. And we can then do stuff with that. With that, with that value. 31. Post Method: Okay, One thing that will, um, that we were gonna be talking about is, um you know, security, right? And one thing that I will tell you is that get requests are no very secure because they're sending that information on the euro itself. So basically, they the user, or even even, you know, some, um, some person that's like sniffing. Ah, in some way I went talking here like hackers and things like that, but also even, like, you know, system administrators that are checking the logs off their servers, they're gonna be able to see user name here. And if we have, like, password as well, like you, they're going to see passport equals. You know, you're very secure password in plain text. So that's not very good. That's not very secure. So we want to change that and make that a more secure Um ah, kind of like setting. But how do you pass then through the log in this use of chemicals, Jorge without displaying it there. And that's where post comes into play. So what do we need to change here? So the first thing or the only thing that we need to change in the form is that we're gonna put post for method equals supposed instead of get. That's the first change. The second changes that we need to out here. The methods to accept both get and posed. But why's that? You're thinking, like, isn't now the form ethic post here? Why do Why do we need getting there? Well, so the thing is like when you first hit the this Ural, the slash Logan, you're actually using a get request. And the reason behind that is that the most basic operation that you're doing through http is oh, get me the log in page that get me the log in page is a get request. So any time you hit a page, let's say you go to Twitter. You goto Facebook, you go to Facebook slash profile. Those are all get requests. But the moment that you then feel something out and then you post using the post method, you submit something, then the post method is being used. So, um, one way that we're gonna be able to check um, aside from the request, values is we're gonna be able to, um, check the request type on the, um underscored the function. So instead of having every quest values, we're gonna use this, which is more, um, the most often use if request method equals. Ah, post. Then you return the user name. And if you're hitting, if you're kidding it the first time, it's request method equals is gonna be get. So it's going to return this this form. So let's see if that works. Um, it's to control escape here. It looks good. So we're gonna hit the log in. We get the use her name. No, wait. There you go. And we're gonna put here. Um, let's say Robert have pressed submit use. Her name is Robert. So one thing that you'll notice here is that there's no question mark user name equals Robert. And but instead, this was fetched perfectly fine from the request values. How is that possible? Where is that reasoning equals, Robert having passed in so that it's actually done through the, um, post parameters, which is which is a header. And this is going a little bit into, you know, more technical specifications. I recommend you like if you can step, uh oh. Are set aside some time to study how it's to be works. It's a very good I learning tool. But one thing that you can do here in chrome is is you can see the the headers of things are going on. And the way you do that is, um, you go to you can view developer on then developer tools. We can also use, um, in the markets. I'll comment I orale come. And J, I usually use J because it's a jealous we're console. But if you press that, you're going to see that something like pops up underneath and here, you're gonna be able to see things like the post requests and things like that. Um, so let's try that again. Um, but this time around, we're going to see the the the information being Some made it. So let's reload here, okay? And then we're gonna put, uh, Robert here and then submit. So it seems like different types in here, um, elements, things basically like the html off the page and some other stuff that you can play around with. Um, we'll check a couple of this in here, but one thing that you'll notice is that if you click on, so if you There's a long line here of things that are being ah served over when you hit that page. But if you press if you click on logging here, which is the log in request off the page, you'll see that Now you see a header stop here and if you scroll down, you'll see there's, you know, General, that was you know, the request response Headers, which is what responded was responded back from from the page request headers, which is kind of like closer to what we're trying to get at, um, and then finally, here, at the very bottom you'll see form data. And if you see here, it says user name Robert. That's where it's it. So it's part of a It's called off the form data header. Um, the post is hitting from the your l. So that means that no person between can look at the data and still the application's gonna be able to read it. So post is the prefer method. If you're sending, you know, stuff that that needs some security and you can use get for example, if you're the Twitter user profile is slash users. That's Jorge. Um, that's a more than an okay thing to post publicly so you can use get for those requests 32. Introduction to Templates: Okay, so we're gonna look at Tempt. It's now, as you can remember, we have this, like, ugly user name submit form here, Um, and we want to make that, you know, easier to to update. And you might have a different kind of, like front and developers doing the templates versus you are they get yourself to the back and application. So templates come to rescue. And templates are part off a software, um, methodology called M V C, which is basically model view controller. Um, M v Sea separates that I base operations from presentation layer, which is like the templates from the logic or controllers. So the views in this case would be the the templates, and the and the controllers would be the the actual, you know, p Y files like this one here. So we haven't seen the models here because we don't have that I basis yet, but, um, we're going to take a look at that in the next section. So before we do anything, let's erase this. Um, and we're gonna grade at a brand new ah, routing there. So we're gonna go ahead and create a templates folder. It has to be on the root level, and it has to be called templates. Um, we can modify that through the settings, but we're not gonna do that. We're just gonna do the default, and then we're gonna create a new file. They're called hello. Html and this falls are basically ext email. But they have some viable management. They're a swell. So we're gonna save this and let's to Ah, very simple xml file again. I'm not gonna, like, describe in a lot of little what I'm doing here, because it's not a front end, Um course, But this is like, ah, very basic structure off a, um of a next email. Five, um, flat. So here, we're gonna have, uh we can do conditional in here. So, um, but let's start with something simple. So let's say just each one Hello, world. And, um, that's basically it. Um, let's say that. And now for us to render that template, we're going to create a just doing up route, um called Hello. And, um and we're gonna define hello to be a, um basically returned that template. So to do that, we use this function called render template. And, um, it's rendered render means like it's not straight up the same content. But python will actually do some some, let's say, computing on top of it or processing. So that's why it's called Render Template. But in any case, um, we're gonna pass the hello html there. Ah, and that what that is doing is gonna, like, just return this this template Ah, sa next email file. Um, one thing that we need to insert here is rendered template, which is a flask function that we need within within the script. So that's that. Let's see if the application is running. Yep. It's running fine. So let's do Hello here. And there you go. So this is actually the tempered that we have. Ah, that we have to find here. Um, and, uh, if you see the the page source, you'll see that it's the exact same content that we put on the London extremophiles. Now, we're gonna take it a little bit further, and what we're gonna do is we're gonna see how we can put viable seen there, and, um, and kind of like, modified, attempted based on ustream foot. So when you put a just gonna be able to pass. Hello. Name here. And we're gonna pass that name. Um, in here you can do. This is something that I don't think we've seen You can actually pass. The name equals none, which means that it's not required. So that was gonna allow us toe to serve? Um, hello by itself or Ah, hello. Name. And I'm going to create a new route for that first case, because otherwise it would be we would need that training slash after. Hello. So this is also something that you can have multiple routes assigned to a specific function . Um, but any case, Hello? Name equals known means that we can render hello just by itself and hello. Name. Ah, hello. Slash name. And the name is gonna be populated with whatever we pass after the hello and is ah, very important trick. You can pass here as a pair, basically the viable that we're going to get, um, assigned through the euro. So what this means is name inside the template is gonna be equal to the name that we have passed in here. Um, but just look you So for you guys, toe, see it better. That's called this name template so that you can see the difference. Um, And then what I'm gonna put in here is I'm gonna have alot and then to access that viable inside. I put too curly brackets on. I put name here. Okay, so there's a lot of stuff in there, but let's see how that how that works, we see that it's it's running fine. And so now we're gonna do hello, slash Jorge and we're not seeing the horn all I'm sorry we assigned hello. I mean, named template instead of name. So see how name is not being right here, but name template is gonna be read, So it's restarting. Let's try that again. And there you go. Hello? Or if I put Hello, Robert, I get Robert. So you see how it's not. It's not returning this exact HTML, but it's actually rendering or replacing some of the html on the fly with whatever where we're passing. So I would not really find its to name, but it's a convention that I mean, it's once you know that that that's happening, we can just use it like this. Um, and the last thing I'm gonna try here is um I'm gonna have a conditional to remember how we have Hello and hello. Name if I pass. Hello? Let's take that. He works. So, Richard, Nurse Richer. Okay, so if I pass hello alone. I see Hello. Known, which is kind of funny. It's not very nice because that's not what we want. Like when they're known in there, that makes no sense. So what we're gonna do, we're actually able to do conditional, like if statements within the template. And to do that, we do. Instead of the two brackets, curly brackets, we do curly bracket percentage. And what we're gonna do, it's like we're gonna check if name, which means if name has any value, then print this but otherwise and see how it's like it's python. If if then um then we're just going to say hello world and you need to end the ifs so we need to do. And if Here. Okay, so what's happening there is, like, if there's a named person is going to say hello name, otherwise he's gonna display Hello world. Um, so let's see if that works. Um, if I do just hello, I get Hello, world. If I do? Hello, Jorge. I get Hello, whore. So that's kind of like the very basic, but this is like, fundamentally how you do template ing on flask, and we're going to continue, um, exploring the more things that we can do with this. 33. Login Template: Okay, so now that we know how to do template ing, um, we're gonna try doing the that form that we had before on make it, uh, part of a template. So the first thing we want to do is we actually want to create a a new template. So I'm gonna just gonna delete this. Hello? Html. And I'm going to create a new template here called Logging Exstein. Well, so it is like a log in page, so let's look at that. Um, I'm gonna create, like, a boilerplate. It's the middle five page again. This is not something that you need to, um, to master for the scores. And when I said a title blogging page, and so I'm gonna do a form here. The action is gonna be, um it's a log in. Well, don't remember what we said about Europe before. This is kind of like a perfect example of where we could use euro for, and we'll get back to that in a second. So I have my form there. I'm just gonna put a, uh, use the name people type. Input type equals text, and I'm gonna call it used her name and then I'm gonna do another one here, which is fast Word. And, um, it's gonna be passed us the password viable and finally need a button to submit the form. And I'm going to call that Le Guin. Perfect. So you can see that in full screen. Um, so I have my form Action is gonna be lovin slash looking. So that means that when I hit submit it's gonna goto the slash loving page, and it's gonna pass. I use her name viable with some some string and the password viable with another string. And so let's wire that up in our application. Okay, so let's go ahead and change our hello, p. Why? Um, so I'm gonna just create a new a new function here. The upper out is gonna be, um, slash log in. And then I'm gonna allow two methods, uh, get and post and I'm going to call this function logging. So I'm gonna say like we did before eve request, uh dot method is supposed It means that the user has already pressed submit, So we're gonna just return. Uh, you, sir, percentage s, which is gonna be a string locked in. So how do we get the value off the user name? So it turns out that the, um, request has a a na object or a dictionary rather that is called request form. And in it, it will have any fields that you have sent on the request in this in this field here. So if we do request Sorry if we do request that form user name, then that will hold the value off that Houston infield. And, um otherwise, if we're not getting the post, it means that we just load in the page. Well, just to return Render template. Um, Logan HTM. So let's see what that looks like. Um, that start the application here by phone. Hello? P y. And no airs there. And let's get the application. You're all here from the share. Um, it's not found here because we, um, specified that the only of route available for this obligation is slash Logan. So we do slash looking there. There you go. So we're rendering the template as a form and we can try doing Jorge. 12345 No, again. User Jorge loaded. So it's reading properly that user name field here and So we have a pretty good initial form to work with before we move on. One thing that I want to change here is this last Logan. Remember when we talked about your l four? How we didn't That wasn't a good practice to set toe have actual in courting girls. So whenever you typed A whenever you type something that slash you know a your l off off a off kind of like your application routes, it should be a big red flag for you. So instead, what we should use here is the euro for method and rumor How we saw that earlier. So in this case, we're gonna put your four, and then we need to put the name of the function, which in this case, is the logging function. So if I put, um, if I put the oil for Le Guin here and then save, let's see if I have any heirs here. I'm getting years. So they let me reload the page and you'll see if I see the view page source that it's properly mapping that to slash Logan. So that's a big benefit off using euro four instead, off the actual slash again Because what happens is, you know, kind of like think this last. Logan is peppered through a bunch of temples like hundreds of templates. And instead we changes to, um, logging. Underscore, use or right, um, now the euro release logging user instead of log in. And if we hit that, you're all here logging user, we'll see that it loads here. But not only that, the your L four is automatically updating that. So that's the big That's a big thing. And we should always use your all four whenever you're you're referring to the Urals on your templates and even within your obligation, um, controllers as well. 34. Login Function: Okay, um, so we're gonna do a little bit more work on the application. What we want to do in this lesson is making a logging function kind of like checking of the use enemy spot on bass. Were are working fine. Although we're not gonna kind of, like, check like a real application using a database, But we're gonna kind off fake it. And the way that we're going to do that is that we're going to check if the user name and password are the same that the user can logon. It's a little bit like simple, but it'll allow us to check how how these things work. So, um, I'm gonna create a function. So in your flask app, you can define functions that are, like internal. They don't have any routes. They're just like helper functions, so to speak. And I want a great this valley log in function that, um if you pass, I use the name and password. It'll check if the user name is equal to the password. Then it returns. True to whoever called it on, otherwise it'll return falls. So it's a very simple, uh, kind of function, But imagine that this was This would be kind of like where you go to the database and fetch the use of name and passport record and check if there violet and then you return something back. So that's kind of like how how that would work. But in here we were just gonna make it that if the user name and password are the same, then then the user can love it. So, um, what we want to do is we want to check on this using the post method. If the record's method is posted, we're going to check if valid, Logan and then we have the user name. So we need to pass the request dot form, use her name as the first barometer, and then request form, uh, fast word to, um, toe take the password. So if if this two are the same because this is going to check in for their the same and it returns true, then whatever is underneath this is gonna be executed. So we're gonna say return. Welcome back. Um, and then the user, the user's name, So request that form. He used the name, but what happens if if it's not OK, So let's say we put the wrong using a passport, which means they're not going to be the same. So we have an else block, and we can set an error here, so error equals incorrect. Uh, use the name and password. Um, OK, so but what happens? Um, with this error, where is that displayed? Um, so one thing that we want to do is, um, we want to pass, and you can pass content of the templates. And you just need to assign basically the viable, like, within the template, and then what? That equals two. So in the best error equals air. Um, and then once we're on the log in page or the template, we're gonna basically check for that. However, on the first run, when you first front it, it's not gonna execute this part here. Um, and air is gonna be undefined. So we might We might get in there. So the way to avoid that is that we can set the the error from the from the get go before before we even do. Is this this part of the code? But let's check what happens if we if we do that, so Let's see if the application is running. It reloaded. That's perfect. And so we're gonna do logging user here. Uh, it's Logan. Sorry. Log in. Okay, so this is the era was talking true about so local viable error reference before assignment . So basically, by then he saying, you know, you're telling me that air equals error. I don't know what that error is, so in order to avoid that, we put error equals none at the beginning, so that, you know, the beginning is just said to none. And then if the air happens, then we will pass that error incurring isn't even passport back to the template. And you can imagine that there could be more errors. Like, you know, you didn't feel the user name or older use cases, but for now, we're gonna stick to that. So let's see that reloaded, uh, and going a reload that. So now we have the proper templates, and that's that's all fine and good. So let's try to put the like, the user name and password. They're saying letter so that that's gonna work out the same. You see, your passport means that we're going to get the valid logging returned true. And it's gonna say return, I mean, is going to say, Welcome back, uh, Jorge in this case. So let's try that yet. That works perfectly fine. If I go back and put Jorge and then put John here, then I should get in there. Oh, but where's the air that we just define in here? Um, well, the thing is that we're passing that error here, but we're not, um, putting anywhere on the template that we should explain that back to the user. So how do we do that? Um, it's pretty simple. Um, I'm gonna put a if block here if error. That means that if there's anything that viable holds anything and just so that we don't remember to close it will close it on here. And then we're gonna say, um I'm just gonna put a red color text in here, and I'm going to say, Just print out the air that the template is a setting. Um, one other little thing that I want to change here is I'm gonna set this to type password so that you cannot see what what's being tight. And it's just like a built in thing that HTML has. Um So if there's an error passed through the template, then we displayed that there. That's bean defined by the controller. The controller being Hello, P Y. So let's see. That looks OK. Um, no heirs, so Well, try one more time. So we're gonna put Jorge and then Jorge. Look at that. You find a little like dot or asterisks? Um, that's the password type. Well, again. And I was That was perfect. Um, So what happens if we put the wrong so Jorge and Ana put test Perfect. So I'm getting the air in red letters. Um, very, you know, calling my attention. And it's displaying that error that we defined in here. So we're getting closer to a good working application. Um, let's see. What what comes next. 35. Redirect After Post: Okay, The next thing we're going to see is, um, redirecting users after the logging and password have are correct. The reason for that is because when we enter our submit a form and it gets to the to the page or to the server rather that, ah, post information is still hanging around. And this is something that you'll see happen if you if you don't do a redirect. So they say, I'm I'm doing here, Jorge Jorge, which is the right Logan on a press log in I get to the to the log in page with Welcome back, Jorge. Right. But what happens if I hit? Reload? See, You see this ugly kind of like dialogue that says, you know, confirm for re submission, which is doesn't look very professional. Plus, it's, ah, it's a security issue because, you know, you could potentially have data lying around. So what we normally do after a successful Logan is we send the user to another at another page that, um, that kind of like can can, ah clear that that post information and and send you on your way. So we're gonna do that in this lesson, and that's how we do that. So the first thing we're gonna do here is we're gonna try to add, um redirect, which is a a flask utility, and that redirect will send us to any any oral that you specify. Um, so here, instead of returning, welcome back, Jorge or whatever, the Houston waas we're gonna have a redirect people and and it's it's a little bit funny because you've returned the redirect. And, um, I think part of the reason that that's the way work is because it's a there's a a an http code called three or four, which is a redirect. And I think that's what Flask does in there, but indicates you have to use return, redirect and, um, But before we do that, we're gonna, um we're gonna create a new AP route, which is gonna be a welcome landing page. So let's create that. We're gonna do up route on. We're going to call it welcome. And then we're gonna put a user naming there so that we can pass the user name on that on the euro, and then we'll do well defined this as a a function called welcome and will return that template. Render template. Um, welcome door, ext. Email. Which we haven't created yet and will pass the user name. Toe that to that. So now we can Now, um, put that return. Redirect. So we're gonna return. Redirect to that you are l four, which, as you know, during the euro for a specific function, Um, and we're gonna pass. User name equals the request form. Ah, get used. Okay. So what's happening here is that we're redirecting toe the euro. Um, four off this function. Welcome. Which is this one here. And we're passing the user name, which is the, um, this first method. It's this this user name here, which ah, turns out to be the user name. Ah, parameter for that method. So it's gonna read that in there. Um, see, we haven't air here euro 40 so we don't have euro four defined here yet. So there you go. Um, so now we have set up our return redirect, And what would happen is when the user name and password are correct, it'll return, or you'll redirect us to this page. Welcome slash. You know, user name Jorge or Jack, or whatever the use enemies and then return it a template. Um, that that will define now. So let's see what that template looks like. We're gonna do something rather simple. So we're great new file here, and we're going to call it Welcome, HTML, and we're gonna open that, and we just great a very simple HTML file. Um, will have, uh, title East. Welcome page. And we're gonna have last string that says welcome and use her name, which is the viable that were passing toe the to the temple. So that's it. Let's see if we have any errors. Looks good. So now we'll go back to logging and we're gonna try Jorge. Jorge, Uh, there you go. And now we're past two or redirected to welcome slash Jorge and you see welcome Jorge there . Now what happens if I hit reload? Nothing. But I don't see anything because it's a static page. It's already kind of like there's no post data there. So this is something that you're always gonna be dio doing in in when you do form submission is redirecting users to to let's say that the actual page where, um, where you're going to start doing any kind off like operations and stuff. So remember that it's a it's a good rule toe 36. Flash Messages: one come operation that we want to be able to do is to communicate to the user. Um, when something kind of like happened, For example, if you locked in successfully, But you want to do it on the on the next page, for example, in this case, we want to be able to when when people hit this. Welcome, girl. Imagine that. That's the home page off your application. You want to be able to pass messages and different messages depending on what the last Operation Waas and because, um, x t p is doesn't keep any kind of, like state between different pages. You need to be able to store that somewhere. Those those notification messages and flask offers a, uh, kind of like a subsystem called flash messages that allow you to do exactly that. So we're gonna do a flash measured message here so that you guys can see how how that works . So in order for us to use flash messages, we have to add flash. Here are as one of the libraries that we're gonna be importing and ah, using flash is very, very simple. Um, what we're gonna do here is that after the valid Logan method, Um, or or if conditional, we're gonna just say flash, and then we put successfully logged in. Now, what's what that is going to do is it's going at, um, store that flash messages in a on a session cookie. On the next time a page is loaded that retrieves or wants to get those messages, it will get any message, any messages that you have flashed in previous pages and it'll will return back to zero, like, reset it and you can have more than one. So if you put flash here successfully logged in, But you had a previous flash messages saying so successfully registered and you haven't landed in a page where those flash messages are retrieved, then you'll see all of them. Um, but we need to do that retrieval, um, kind of like code. And we're gonna do that here on the on the welcome page. So what I'm going to do is I'm wanted to a generic, um um, group, forgetting all the flash messages and basically put in putting them on the on the page. So we're gonna use this. Um, this method called with messages equal get flash messages and get flash messages is like the built in, um, method available in the template to get all the messages. So basically, I'm saying set messages as a viable that contains all the flash messages that that we have until here, Um and then when end with here. So that's Ah, that's basically a loop. And now I'm going to check if messages so that if there's no messages, we just keep this snippet. Um, and now I'm going to Is that an order on order list in next email? And you can look that up. Um, And then when I defined this as a class called Flashes, um, and then for each message in that message is, um, basically it's a list or a kind of like all the messages are are dumping that in the message. So for message in messages, we're going to get each message. That's that's where that list, um, and let's to an end for here so that we can not loose the structure and we're gonna dent toe on u l two and that you will on the top. And now we have our message. So we just, um, insert a new um, item in that list list item, and we print the message there. Perfect. So that should be all, um So again here, we're set. We're setting the messages viable to get all the flash messages as a list. If we have messages way have that list, then I'm going to create a new well or on order list, and then I'm gonna go for each messaging. That message is printed out. Assad, That's an individualized. So let's see if we have any errors here. Doesn't look like it. And no, we haven't saved this. Right? So now stick. Okay. Perfect. So now let's try that again. So we're gonna go to log in and when it to Jorge, Jorge and look it. But what happened here? The session is unavailable because no secret key was set. Okay, so this is something that you're gonna have to set. And I, uh I did this on purpose so that you couldn't, um, kind of like running toe this cause it's It's very common. Um, I always like, forget us. Well, so what we need to do is we need to set on the, um here on the main main. Um, the method, we're gonna have to pass a new, um viable called app secret. So let's say do this so up. Secret Key basically will encode any sessions that do you have so that users can not be able to, um, to retrieve those the the information on those sessions, and I'll show you later how to generate good secret keys. But for now, we're just gonna put some very simple, super secret key string here, and that's gonna be our super secret key. Of course, it's not very good. We should have a combination off like characters and numbers, things like that. But for now, we'll say, that's that's a secret key. So now we go back and let's try that again. Or here or here and now it's working perfectly fine. Nancy, it says, successfully logged in. Yes, a little like darting there because it's it's a list off off items. But as you can see that that message was, um, was safely retrieved from the previous logging page. Now what do you think happens if I refresh the page? Will I get that message again or not? Well, let's see if I reload. Oh, that disappeared. Well, let me reload again. Oh, I don't see it anymore. So see, that's how messages work. It's kind of like you fetch them. You're resetting that list, and now you're wrong. You don't have that message anymore, which is perfectly, you know, that's the way that it should work. Um, we should have that, um, that functionally in place. So that way you're sure that we're gonna, um we're gonna pass a message is going to be consumed by the other page. And then after that, it's Ah, it's a reset, and we don't We don't display back to the user. 37. Better HTML: Okay, We're gonna have to, um, start putting a little bit a better structure in our HTML. So if you want to skip this, um, this lesson, it's fine. Ah, I'm just gonna put a little bit for better. It's female here on our logging page, Um, so that it's more standard compliant. And, you know, it's always important to be standard compliant, um, in any code that we do. So we're gonna add when he tried it. Html, um, designator here at the beginning, and then we close that down, then usually you have a head, which is where you define the title and other, um, other kind of attributes for the page. And then we have to do body, which is where the actual a body of the page is in. And we close that, uh, and, um, and finally, I want to have a little like a title or a hair er, and I'm going to call this, you know, the act may company, um, in honor off those of those famous ah, cartoons. You know, like the road runner. Um, and I'm gonna put here logging page. The Acme company asked the US a title so OK, now we have Ah better html page. We have our HTML declaration of the beginning of the end. We have a head with the title, and then we have the body and on each one, Um, so we're gonna have we're gonna have that. Let's see if that looks good. And if we hit log in, we'll see that it looks a little bit better. But especially, it's now kind of like standard compliant. Um, but the point I'm gonna make here you still don't see is that, um you know, we would need to do this also with the welcome page, because you know, that page is exactly the same. So, you know, we need to, like, maybe copy all this and reported here. Well, the title in this case is the welcome page. So I would need to do is here. But you can see how this can be become really repetitious and not very scalable. So, for example, what happens if you want to change? You know this part here that says the Acme company for all the titles, for example, or you don't want you want to have these h one title. Maybe with the logo of the company in every single page. Like it quickly will become a mess to be able to, um, to sustain this. So what we're going to see is something that, um, that is gonna help us, um, deal with this. And that is called, um template Inheritance. So let's see what? That what that looks like. 38. Block Super: one final trigger wanna show you guys is and you're gonna see this. You're gonna find yourself, um, in this situation, I think, rather often, especially when you start doing javascript libraries and things like that. Um, remember how we said that if I had the title the block title here, without the finding the head, it would intelligently kind of like inserted here. But what happens if I want to define some other stuff that I want to insert in this in this title underneath, like, let's say, you know, javascript library or something, but from the from the template itself, um, well, one way that we could do it is we could have a you know, blockhead here. Right? And let's say we have, uh um and block, right? And then we have I'm gonna just like to a script javascript tag, but it's not going to do anything fancy. Just like just to have something that's a placeholder of our X equals one and script. Right? Um, So what happens if I do that? Well, the problem that we're going to seize that this is gonna replace their whole head, and it's not gonna leave. Um, anything for the title. Like Imagine it's doing this like it's replacing all this with this. This Ah, this jobs were likely that we're that we're putting here, so and you can see it here. Like, if I do refresh, um si, si, like hydro. I don't have the title there anymore. Um, it's it's replacing the whole head with it, So that's not very good and see, like we now have this ugly title, which is the euro. So I want to be able to insert this JavaScript thinking they're in here, but at the same time have my block title, um, in their And, um so the way you do that is you call a, uh a method called super. So if we do, um, it's kind of like it's like a viable super. Then what's gonna happen? These It's going to replace this the head with this with this stuff, But then it's gonna leave the title the title tag, um, to be a Toby kind of like, um, allowed to be modified by subsequent call. So let's see that in action received the title now now looks good. And there you go. See, Now have my title if I see the source be paid source, I have my screwed type here and then my title here. We could also do it the other way around. Um, if you wanted to title first for some reason, you can do this, right? So first we're saying, OK, the title block, replace it with this and then for the head do this insertion on the head part, but called the super, which is going to, like, insert the title back. And if we reload now here we'll see the same result. We have our right the right title. But if we see the the View page source, you'll see that Oh, the title still appears underneath. That's that's interesting. I was expecting the title Toby Toby first, but no, actually, what's what? The way that we would do the order change against would be this exactly because then supers is calling like the title piece and then its inserting the thing afterwards. So let me reload the view sores yet, and there you go, Um, and that's put a inventing there so that it looks a little a little nicer. Perfect. So see, that's how you kind of like your ableto, um, to modify attack inside a block, but also kind of like call the super method so that you can also do more stuff with it. 39. Template Inheritance: So as we saw in deep on the breeze lesson, We, um we're trying to fix, um, the pages, the log in page to be more stunning, complying than we had it. It's like required, um, tags for proper It's the male. But as we saw, it's very repetitively or on unskilled able to be doing this for every single page that we do. So the way that we, um, that we solve this problem is that we use something called Template Inheritance and Tim Blizter inheritance just means that we're gonna have a basically a we're based template that has the common things and then each each additional page Well, just like extend or add to the toe that existing kind of like base page. Still, the best way to to see or understand this is to see in action. So let's create our base html page and see what that looks like. So let's create a base html template and so new file and we're gonna we're gonna call this base html and we're going to copy and paste a bunch of the stuff that we that we did hear earlier. Um, so we're gonna have doc type we have ahead, and then I'm gonna have a body, and we're gonna have end HD. So that's like the basic, very, very basic structure. But, um, let's start doing some other more interesting stuffing here. So, for example, on the head, um, I can do something like title, right? And then I would put something like, you know, the Acme company. However, that means that the same titles going appear on every single page. But I want to be able to do this kind of things, like blogging Paige Dash and in the welcome page. Welcome page dash. Right. That and then print out the Acme company. So the way we do this with the base template is that we define areas where things can be substituted by the by the Children or by the inheriting templates and modify the temporary in those places. So the way you do that is we use this, um, this command called block, and then you put a like a name for that block. So if I do block title and then you define the end of it and block, what's gonna happen is that any time let's put a space and then the dash here. So any time now that a a page like the log in page inherits that we're gonna have replace, Um, basically, this this part that changes with that with that block tile and the rest is gonna be, um it's gonna be printed us as us is on the on the base on the base template. So, um, I'm gonna go ahead and define this whole block, um, for the head, because I I want to be able to also maybe at older attributes in here, maybe injecting JavaScript libraries, things like that. And then I'm gonna end. Look here, Um, so now on the body, I want to have that h one, which is, you know, the header for all the pages. I wanted to be the Acme company across the boards or no changes there. And I always want to have if there's any flash messages toe display them here. So I'm just going to copy and paste this part because it's it's gonna work just like it is . And I'm actually cutting it off from the welcome pigs, and I'm gonna put it here. It's, uh, invent that. Okay. And, um did I want to find an area even though it's not, you know, required. It's It's a very common pattern again, to have a Dave that holds all the kind of, like, the main content. And I'm gonna call this block uh, content. Okay. And, um, last thing I'm gonna do is I'm gonna at a food er which is a newish html, um, attributes. And I'm going to call this also blocked food or because I want to be able to maybe override it, uh, not printed at all. Or, um, or add stuff to it. But here, I'm gonna have a copyright sign and copyright 2014 by on a little hyper link there. Uh uh, you know, back May Co and, uh, the act me come perfect. So now I have my base. Html looks really good. Um, I have ahead that I can override with a title. I have a flash messages for all the pages already built in Have a Contin, airy and a footer. So let's start with the log in page. So So how do I get this basic seem ill in here? So we use a keyword cold extends, and so I'm gonna be able to basically Ah, take this all out and I'm going to say, uh, extends base html and you put it lower up. I need to put quotes. Sorry. And Flats is gonna find whatever template you have with that name Basij female. And it's gonna like, make it the apparent for this for this attempt. So for the title, um, going to to, um, block, oops, block, um, title. And then I'm gonna put, um, let's just call it log in and then end luck and look, and what's what that's gonna gonna do is like, you see how I have, um, block title logging here. It's going to go in here, but notice that I didn't have to include a blockhead and block at the beginning because that means that if I have some, some internal, um, tag like title flats will be intelligent enough to know that it's not. We don't want to replace the whole thing. We just want to replace this line here, and that's a pretty pretty useful thing. Um, for us, we don't need to head anymore because the head is being defined, you know, here. And, um, we don't need any of this And what we want to do is we're gonna have a defined this as the content, so block content who and that's gonna be printed or inserted in in this area. Here. Block content. I have the form action form action. Um, I don't need the this body or HTML anymore, and I'm gonna end a block here. Cool. So we'll deal with the welcome page a little later. We just want to see that the there were complaints is working fine. So let's see if we have any errors. Looks good. So let's reload the log in page and see what happens. Wow. Perfect. So you now have the Kobe writing there, which is coming from the from the template. I have logging dashed. The Acme company, which is the title that that I wanted to have. And, um and everything else should work. Uh, the same. So if I do Jorge Jorge, I can I can log in and I get the message. Although that message is coming from the welcome page. So that doesn't count. But let's finish it off and see how we do the work on page. Okay, so let's do now. The welcome page um, so here. I don't need this, but I need to have the extends. I'm just gonna copy that from here, and then I'm going to have a title. And when I put this us the welcome page, um and, um see, And I need I need this to be the content, right? Block content. Um, and that's it. See how simple it looks now? It's, ah, pretty much reduced. And now we have, um we have a very easy way to kind of, like, extend this. Basically, Melanie's gonna look exactly the same across different pages. So, um, let's see if we have an error and we'll do the logging again. Um, so let's to Jorge Jorge Logan. See how I always now have the H one here. The header. I have the welcome Jorge and I have my food. Or which appears in every page. If I had reload, I should get rid off the flash message. There you go. He looks it looks good. 40. Cookies: Okay, so now we're gonna try something new, which is, um, using cookies to be able to track the logging off the user. And cookies are basically small. Files are saved, Um, locally in your computer that only respond to the domain that they're associated with. So in this case, you would be with our I d or rather hello app from Syria, the u C nine dot io domain. So, um, we're gonna learn how to use cookies. Although, um, it's more secure or safe to use sessions. But it's ah, it's a good thing to learn how to use cookies anyways, because sessions are based in in on cookies. And and it's something that you should definitely have. Ah, and all its own on how to do so In order for us to use cookies with flask, we need to ah, use a A library or a function called make response. And make response allows you to modify the response, um, so that you can set, um, you know, Rex. It's well as, um, as cookies with the same kind of like on the same. It's a round trip, um, or on the same request. So let's at make response to the flask libraries here on the tops. And, um so we're gonna replace this return Resurrect? Ah, you're a four with a basically, we're gonna create a response object. So we're gonna do response equals, make response, and then the same thing Europe for welcome. Um, that that whole, that whole thing. And basically, we're doing I'm a response for the redirect part of the thing. And now we're also now we're gonna also set a cookie, and the way we're going to do that is we're gonna do response. That said cookie. And then we put the name off the cookie and a value. So in this case is request form, uh, get user name, and we close that, and then we return response. So that's the way we do. So, as you can see, you know, responsibly. First, create a make response object that has the euro four. Welcome. Which is gonna be kind of like a redirect. I know the same response. We also tag on this site. Cookie, Use the name. Um, it said so. We are also going to create a look out function that will allow us to, like, basically log out And a log out in this case would be to delete the cookie, that user name cookie in, um, in the application so discreet, that proud slash Look out and, uh, to another findings as to log out method. And we were more or less the same thing we're going to Ah, responds, um equals make response and then every direct Ah, you're old for, uh, Logan. So in this case, we're gonna send the person toe the log in page when they when they look out, and then we're gonna responds, said Cookie. And it's funny how ah, the way there you the leader cookies. You said it's, um, expiry to zero. And, um and you put the value is black. This third parameter expires allows you to set the cookies lifetime. Um and you know, you can You can basically put a cookie for, like, an hour or a day, etcetera. I believe that's ah, that's seconds. And then you return the response. Okay, um, we also need to change the, um the welcome. Um, so we're gonna be doing welcome kind of like becoming the route off the or the index off the application. We're going to find us a slash. And what we're gonna do here is we're going to get the user name from the cookie and the way to get a cookies value. You do request that. Cookie start get use the name. And if that's empty, then that means that they use her name has not been set. So we're gonna set this to if there's a user name in that cookie than we do this else. We return a redirect to the gi