Now, one of the first things I want to tackle is getting it out.
Hold on the user typed in too, the URL,
or rather known as parsing a URL in this case,
if the user typed in ww.mysite.com and then they had a forward slash with a
path, for example,
forward slash Angela or forward slash mail forward slash messages forward slash
pictures, then we want to be able to get hold of the information in that path,
because we might be able to do something different with it and render something
specific. Based on that URL previously,
we already saw that we could change the route of our website by serving up
different information. So if the user went to the home route,
which is just the URL and then a forward slash,
then we would show them something. But if they went to a different route,
for example, forward slash hello, then we could show them something else.
This is where we left off in our previous lessons.
If I wanted to create a BI function that just simply returns by
then I could set up the route decorator.
So this is the decorator function to look for when the user goes to a specific
path, for example, for what slash buy,
remember that this is a decorator function,
which lives inside a App Object,
which is declared in the flask class. Now,
if all of that sounded like it didn't make any sense,
then be sure to review the lessons that we did in previous days on classes and
methods.
I'm going to assume that all of this is pretty straightforward to you by now.
Now when we run this website,
we can now go to the URL and we can go to the homepage,
which says, hello world. And we could go to the buy route,
which just says, buy with an exclamation. Okay. What if we want,
I wanted to have a variable in that route.
What if we want to get hold of what the user types in him, for example,
let's say that instead of just catching by and forward slash what if we wanted
to greet our user,
let's say I create a greet function that takes their name and it returns
something like hello, but then it adds the name.
Let's turn this into an F string to make that work.
The question is how would we create the route?
Would it be forward slash name or would it be something else?
Well, this is what is described in this section of variable rules.
We can add variable sections to a URL.
So basically it's the equivalent of creating a variable that URL by marking it
with this syntax.
So putting some angle brackets around the variable name,
then all function is going to receive the variable name.
Once the decorator is done with it,
and then we can use it inside our function.
So for example,
let's say that we had a route which is forward slash username,
and then forward slash the actual name of the user,
which we'll put inside some angle brackets.
This means a flask is now going to turn, whatever it is that comes after.
Use a name forward slash into a variable for us to tap into.
And that variable has the name of name.
So now when our greet function goes through that decorator,
it's going to receive that value and we can use it in this greet function.
Now, if I go ahead and rerun my code
and go back to that URL and I go to user name forward slash,
and then I'll put in a user name, for example, Angela,
then that Angela is going to be taken and it's going to be rendered dynamically
into my webpage. So it doesn't matter what it is I put here.
It's always going to respond accordingly and treat this as if it was the value
to a variable.
Now notice how every single time we update our code. So for example,
if I didn't want this to be under the route forward slash username fall in slash
name,
instead if I just wanted to put it at forward slash name,
this is not going to work as it is. It's going to give us a four Oh four,
which is not found.
And the reason for this is because we have to stop and refresh all server for
these changes to be reflected.
So I have to stop and then rerun my server.
And now if I go to here and I start typing something like John,
then this update to the route is now live.
If we are testing and developing our website, this is really painful.
Having to stop and rerun the server every single time we make a change in our
server file.
So one of the things that you might notice here is that there is something
called a debug mode, and we can set this to be on or off.
Now there's a couple advantages of having the debug mode on and it's described
in the Quickstart. It allows us to activate the debugger,
which helps us narrow down on any issues.
It activates the automatic reloader so that whenever we edit off a file and hit
save, it's going to reload our server. And finally,
it also enables debug mode on the flask application.
Let's give that a go in order to change our app to run on debug mode.
All we have to do is to change right,
the debug property to true, right?
And now if I go ahead and rerun this application,
then you can see my debug mode is now on.
And I've got this debugger is active and also a debug, a pin,
which we'll come back to in just a minute. If I go to my website,
you can see that I can go there two, a route like this,
and it still works.
But if I was to change something in my code,
for example, just adding a new word into the return.
If I go ahead and hit command S on Mac or control us on windows to save this
file, then it will right.
Reload the server because it's tech did a change in that file,
and it's going to reload our server automatically so that we don't have to stop
and start all over again. So now,
if we go back over here and we refresh this page,
then you can see that update in the code is now reflected.
This is pretty neat. But in addition, we could also do other things.
For example, if we actually need to debug an issue, for example,
let's say that here in my variable path,
instead of saying name, I quote it username.
But then in my greed function,
I'm still trying to tap into this variable code name.
So now if I hit save,
it's going to reload my server and you can see that there's actually nothing
wrong with what I've done. This is all syntactically valid.
But if I go to my website here and I hit enter,
I actually get the flight task debug view.
So it's it actually telling me in this nicely formatted way?
What is the issue? So the main issue, right, is that I got a type II error.
Yeah, it happens.
If we scroll through the bottom is that the green function got an unexpected
keyword argument called username.
So that debugger might help us to figure out that, Oh, actually,
I've named this wrong.
Now this is an issue with the path here. But alternatively,
I could have made an error in my code as well. For example,
let's say that we want to take the name that's being passed in him.
We decided that we were going to add 12, two, it for some reason or other. Now,
if I hit save so that my server reloads and I tried to refresh this website,
you can see again, I've got a type II error. And this time though,
it's telling me that I can only concatenate string,
not an integer to a string. And if we go through these files,
you can see one of these lines, the first to a file that we wrote, hello, dot.
And it tells us that online 18 in the greet function,
this is the line. That's tripping it up. Now,
one of the things I can do at this point is I can actually open up the flask,
a debugger. And I do that by clicking on this button right here.
And what this is going to ask is for that pin that I showed you earlier on,
which is in our console.
This means that if somebody else is accessing your website,
live on the internet and you happen to have it in debug mode,
they can't just go and mess around with your code.
Let's go and scroll to the bottom and copy our debugging code and then paste it
in here as the pin, and then confirm to gain access to the debugger.
Now,
what it's done is basically its open up a console that is live on this line.
So now what I can do is I can print out for example,
the variables that it could access on that line, for example, name,
and it tells me that name is Angela. I can start diagnosing, well,
why is this wrong? So if I wanted you to add name to 12,
well I can narrow it down.
And it shows me that you can only add a string to a string or an integer to an
integer. You can't mix up the types.
So that might make me realize that actually I need to change this also to a
strength. Now,
if I go ahead and refresh,
you can see there's no problems there and I've managed to fix the problem with
the help of the flask debugger.
Now just a few more things on this URL.
One of the things that we could do is we could add some texts to the path
before. So you remember that I said username slash name. So that works.
We can also add paths afterwards.
So it could be one or two, whatever it may be.
And we can now access this route by going to our URL and then going to the first
part, which was username.
And then it was the variable that we could put in.
And then finally we can add even more to the path afterwards.
So we're in our case, the path is one. So now if I hit enter,
you can see it still goes to the same page,
but it's only this middle pot that's being taken as the value to this name
variable. Now,
if we go back to the flask Quickstart,
you can see that the final thing you mentioned is there is something called a
converter on the converter,
basically converts the URL into any data type that you specify by default,
it converts that variable value into a string,
which means it's going to accept any text without a slash. Now,
if you want to keep the slash, because if you are using a path,
then that might be quite important. So for example,
you wanted to get this entire part as your variable slash Juan.
Well then in that case, instead of using this, you would have to specify, well,
the data type is going to be a path and then I'm gonna save that as the name.
So now if I hit save and I use this same URL,
you can see the rest of that path is now being rendered in my variable and it's
being pulled in for whatever it is that I may need it for.
Now, alternatively, you can also have more than one variable, of course.
So maybe this first part, I'm going to keep it as a name,
which is a string. And then the second part, I want a number.
So maybe I'll keep it as the datatype integer and I'll give the variable a name,
a code number.
So now I can tap into the name and then I can also tap into a number like,
so now coming back here,
if I change this URL with a bit and I hit enter,
you can see it's taken this and placed it here.
It's taken this and placed it here.
So that's a quick little bit on how we can extract the parts that we want from a
URL by using these variable paths in the next lesson,
we're going to see how we can control the HTML that gets rendered instead of
just letting flask render what is the default.
So for all of that and more, I'll see you on the next lesson.