Detailed Notes
Python is a powerful and general purpose language. You can add the Flask library to quickly do a large number of tasks. That include spinning up an API with a few lines of code. This episode walks through exactly that.
You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.
Transcript Text
foreign [Music] well hello and welcome back we are continuing in a sense although this is we're going to focus on one specific question we're going to talk about basically sort of what is flask and then how do I create an API using flask in Python and we're going to look at these two but this is part of a larger project that we're working on we're working on our way through building what we call Shorty which is just a not terribly unique or creative way name I guess for a URL shortener much like you would see bit.ly or owl.ly or one of those and we've worked our way through a couple of pieces but now we're going to work on the API side of it because we're going to have to be able to take a request and return back a short a shortened one a shortened URL and of course we need to do that through an API the way we're going to do that I guess it says of course but that's how we're going to do it so we're going to use this thing called flask and if you haven't used it check out flask if you just go to flash.palletsprojects.com or if you go to Google and you just do something along the lines of uh let's see python flask search for that and you're going to get some stuff here you can jump right into it it's very useful and we're just going to touch on it actually if you look down the side here there's a lot of things that are available but we're going to do the most the most simple which actually we're going to start with their hello colorful world and we're gonna come in here we're going to create a little file a new file and this will be we're going to call this API py and we're just going to start with something very simple so what we do need to do is we need to make sure that we've installed flask if I have oh apparently I hadn't so here we go so now I should be able to do here now what you would normally do is you do Python 3 API let's see what that does so it doesn't really do anything oh because we don't have it actually doing anything but what you do need to do is if we do a flask run then it's going to tell us we can't locate a flask application and so what we're going to see here is okay we've got this little thing and there we go so we're going to have to tell it what our app is now our app in this case so if we do theirs if we say Flash the dashes dashes app hello run guess what doesn't exist but if we do API uh oh sorry not run if we change that to API because that's going to be the name of this boom python thing so now we've got this running at 127.001 5000. so what we want to do let's background that and then if we do curl that guy HTTP because this is going to be a very simple way to test our call uh one on five thousand well I want to do a dash get I think that's how I do it okay actually what I had to do is because I had my app actually paused what I do want to do is then if I do curl HTTP colon backslash whoops with one of these uh 127.001 5000. there we go so I get hello world and you notice that I've even got it wrapped in this thing so I get a very straightforward result coming from it which means if I come back over here I can go and actually I guess I ran where it was there we go oh there I can actually here I can come in and I can tell it what port to run on which it's going to tell us like your dash host could do dash dash host and I think it's just a equals so I'm gonna do 127.001 then I'm going to run it on let's run it on Port 6500. boom and then if I come over here oh we'll come back around to that because one of the first things I want to do is let's go ahead and we're going to pull in this probably because we're going to need it yeah we'll go ahead and do it because we're gonna end up using this um so I'm going to escape and we can always do so what we're going to do here so we're going to create a different endpoint so this will be we're just going to call this shorten we're going to start with this and call shorten and then he's going to return so let's go look at the first thing we've got here is what we want to be able to do is we want to be able to give it a parameter and so let's do here let's call this URL which I believe is how it's done oh actually I think it's just here so I'm going to do it shortened URL because I think that's how it looks and if we come in oh yeah it is so if we do shorten so now what we can do is we take this whoops uh we'll do slash so if we're going to do this we're going to do that we're going to sorry if we if we call short and Slash and then the name of a URL and then we send the URL we're going to send that URL in and then we're going to return let's say um we're just going to say this is what you sent and we're going to do uh Escape just to be safe URL and so if you look here what we're going to do is Escape that name out and then that means if we do something like weird like this it's still going to come through so if we send it tags and stuff like that uh did I get it all right I did so now let's just go back to run it let's run five thousand if I do my initial one oh a port and then what I get is hello world but if I do shorten now I'm going to get that it's not found because I can't I did not provide a name if I do this and I do um to be colon backslash some long url.com slash a equals one uh let's see I'm sorry that's not a real one but we're gonna do it anyways b equals two so now we've got this big old thing if we do that because of the slashes it's going to give us some issues so we could get rid of this oh it's still not going to give it to us let's go back and look at our over here so we have shortening URL so let's go look at what our example was here they said that if we do slash name and we give it a name then that will pick that up so if we just do a simple URL then that should be let's just do this there we go and so when we do oh it's because it has ah because we have parameters there okay so right now we've already found in we're running a little bit of an issue with our apis we've got to be able to figure out a way to send an actual an actual URL as opposed to something just very simple so here if we did something url.com and then we did let's do just to show you what the tags are I don't really we'll need it but we're going to do it anyways um there's a parse error uh well it doesn't like that very much anyways okay so I'm not sure that our Escape is going to help us very much in this one let's go back and look at that just in case because that should be escaping because we're entered as text rather than that so if I did this is what they're saying oh because it's formatting it okay let's see let's just do exactly their example yeah so we get an issue with that is it doesn't like what we're sending but if we do this because we're doing a format uh come here so let's just do this let's do what they said so I can show you this and really it's just to do it you set this now we need to reload it I'm assuming and so now if we do it nope it still doesn't sound like it so we've got something in our oh it's because of our slashes Ah that's our problem yeah it doesn't like that at all in here because we would need to probably let's go back to this if we do this and this will this give us what I need nope then it gives us a we're not found because we didn't actually get the thing so we're going to play around with this first is within our API what we want to be able to do is let's just do this we don't need to escape it right now what we want to be able to do is do is send a URL in of some sort and then do something with it so so let's do this because we're going to actually step up a little bit because that was just a straight out we're going to get something but here we're going to get some and such because this is going to be important when we get into those so we get into the API because what we're going to end up doing is we're going to be able to we're going to want to be able to do a get and we're going to be able to do a post so actually we're going to do is we're going to post to shorten and we'll deal with this a little later and then there's going to be a different one that is going to be the get I'm putting these in place these are placeholders we'll come back to them uh here let's see so it's like that so now if we do a get uh oh I need it to be here so if I do this I should get the same thing because I'm still doing a get when I do the this by default when I do this curl that's going to be a get and let me just go back to the one that was simpler there we go some long URL but this is what you sent okay so we're doing working on our git first this is going to be essentially what we're going to do is this going to be instead of shorten this is going to be well we can call it shortened we're just gonna it's the same name here but here we're going when we do a get then the URL we send is we're going to send something back this so it'll be uh actually let's do turn value and the first thing is going to be this is what you sent and then we're going to do return value Plus equals and we're going to return return value and then it will be something that's going to be is the actual URL and we're just going to call this result and we're just going to say for right now result equals do it like this way assume we did something so what you're probably going to end up here would be our short URL which we can we can either do it with an HTTP or not but let's see so if we do here so we'll assume that we don't have the HTTP on it because it which would really make sense so now it's going to be like my short dot com but then we're still going to have that slash so that's something we're going to have to worry about so I think we did it that way how did we do it oh we haven't really looked at that yet so it's probably going to end up being this it's going to be four five something like this is what we're going to want to send but we're going to need to be able to wrap that and so we're going to have to be able to get either we're going to end this is two things we can either send straight code in which is this which is probably where we're gonna we're going to want to do is say although we're probably gonna have to clean it too so we'll start with this so we're going to start with is we're going to send them some code and then it's going to get back the you know the shortened URL and let's just do this uh uh we'll put a little Line Feed in there just because not that one that one run it go over here boom okay so now we got something a little easier but now what we're going to do and you can see all this stuff as I mentioned in here so if you want to build something when we're complicated then you can see things like hey you know what is the what's the method uh which problem that is probably what we're going to want to do since it's the same one I'm going to do this whoop actually let's just do get comma [Music] any post and here we're going to say let's do this if it's post else because it's going to be a get so that's a get here return value equals not implemented yet oh which we need to get the request is going to have to come in oh I think it's automatically gonna do that oh we need to I'm sorry we have to import that's right so that means we also have to import the request from flask now that will come through let's go ahead and show you what that looks like so believe it's as simple as I think it's Dash get okay so just to save you a little bit of time you may have noticed a little jump there and what I did is I want to do X and I put its post there we go uh and then I've got my little percent after that I'm not sure why I get that but anyways so we get that so that allows me to post if I do a get and then we're here and now let's go back and look over one of the things we can do is we can send parameters and so that's what I want to take a look at and again if you want to look in here running Direction URL building probably what we want here we go request data maybe nope that's not what we want so we go here oh here we go foreign so here let's see exchange blah blah blah that's how I want to do oh that's if I want to rebuild something I'm going to do a template I can render a template I don't really need that right now here we go so I can do that's in a post and let's do it this way we're going to play with it for a second because we can so let's come in here I do login nope I don't need longer that's their thing that's totally useless to me instead of this I do shorten and then I'm going to do print request let's try that um and so let's save that I may have time jumped you a little bit but it's because I needed to wrap my URL in quotes as far as using curl so now if I do it uh and I want to just let me clean this up a tad because I want to give something whoops um let me run it unexpected event where did I oh shoot got a space in there somewhere there we go so now if I run it I get success because that's what I kick back but you can see here where I pulled those suckers off so now if I go back to shorten then that should have been fine so let's get rid of that so now if I go back to shorten let's go all the way back uh if we go back to here the one we did all the way here we go big long URL that's we're going to do URL equals uh we have to just wrap this and then we come all the way back here there we go and now this is what you sent so we did something but notice that it actually let's see what did it get oh because I see this is where we're going to run into some issues this will be a little bit of a challenge we're going to want to put we're going to want to post this because if we wrap it into the string then we're going to end up with some issues so we're going to be having to either oh which we won't have to do this would actually never exist I'm sorry because what's going to happen is what we would normally do is for this for the shortened we're going to post uh so we hit a colon backlash slash short name some number there okay so it's going to come through and so now we have a an API and I think I'm going to hold here because what we've done is we do have creating an API using python because we can use this using our flask run and we have our uh we're using it in flask as well so we do have a little bit of testing an API call but I want to talk next time we're going to come back and we'll go a little more deeper into this now that we have this out here and find some easier ways than what I just went through with curl as you can see is a little bit of a pain and we're going to check this thing out called Postman that being said get back to your day hope you have a good one and we'll talk to you next time around hello this is Rob with developmentor also known as building better developers wanted to announce that we have school.developmentor.com feel free to check it out if you like any of this information any of the content that we've sent and you would like to see more you can come out you can enroll for free we have free courses we've got places for you to get better at just learning a technology our how to's you can work on your business skills we can help you with becoming a better developer as encoding and things like that a lot of the stuff you've seen on YouTube we also have out at school.development or we just have it a little more of a educational format and a way for you to track your progress as you move forward becoming a better developer foreign
Transcript Segments
foreign
[Music]
well hello and welcome back we are
continuing in a sense although this is
we're going to focus on one specific
question we're going to talk about
basically sort of what is flask and then
how do I create an API using flask in
Python
and we're going to look at these two but
this is part of a larger project that
we're working on
we're working on our way through
building what we call Shorty which is
just a not terribly unique or creative
way name I guess for a URL shortener
much like you would see bit.ly or owl.ly
or one of those and we've worked our way
through a couple of pieces but now we're
going to work on the API side of it
because we're going to have to be able
to take a request and return back a
short a shortened one a shortened URL
and of course we need to do that through
an API the way we're going to do that
I guess it says of course but that's how
we're going to do it so we're going to
use this thing called flask and if you
haven't used it check out flask if you
just go to
flash.palletsprojects.com or if you go
to Google and you just do something
along the lines of uh let's see python
flask
search for that and you're going to get
some stuff here you can jump right into
it it's very useful
and we're just going to touch on it
actually if you look down the side here
there's a lot of things that are
available but we're going to do the most
the most simple which actually we're
going to start with their hello colorful
world
and we're gonna come in here we're going
to create a little file
a new file and this will be we're going
to call this API py
and we're just going to start with
something very simple so what we do need
to do is we need to make sure that we've
installed flask
if I have oh apparently I hadn't so here
we go so now I should be able to do here
now what you would normally do is you do
Python 3 API let's see what that does
so it doesn't really do anything oh
because we don't have it actually doing
anything
but what you do need to do is if we do a
flask run
then it's going to tell us
we can't locate a flask application and
so what we're going to see here is okay
we've got this little thing
and there we go so we're going to have
to tell it what our app is
now our app in this case so if we do
theirs if we say Flash the dashes dashes
app hello run guess what
doesn't exist but if we do API
uh oh sorry
not run if we change that to API because
that's going to be the name of this
boom python thing so now we've got this
running at
127.001 5000.
so what we want to do let's background
that
and then if we do curl
that guy HTTP because this is going to
be a very simple way to test our call
uh one on five thousand
well
I want to do a dash get
I think that's how I do it
okay actually what I had to do is
because I had my app actually paused
what I do want to do is then if I do
curl HTTP colon backslash whoops with
one of these
uh
127.001 5000.
there we go
so I get hello world and you notice that
I've even got it wrapped in this thing
so I get a very straightforward result
coming from it which means
if I come back over here I can go and
actually I guess I ran where it was
there we go oh
there I can actually
here
I can come in and I can tell it what
port to run on
which it's going to tell us like your
dash host
could do dash dash host and I think it's
just a equals
so I'm gonna do
127.001 then I'm going to run it on
let's run it on Port 6500.
boom and then if I come over here
oh we'll come back around to that
because one of the first things I want
to do is let's go ahead and we're going
to pull in
this
probably because we're going to need it
yeah we'll go ahead and do it because
we're gonna end up using this
um so I'm going to escape
and we can always do so what we're going
to do here
so we're going to create a different
endpoint so this will be
we're just going to call this shorten
we're going to start with this
and call shorten
and then he's going to return
so let's go look at the first thing
we've got here
is what we want to be able to do
is we want to be able to give it a
parameter
and so let's do here let's call this URL
which I believe is how it's done oh
actually I think it's just here so I'm
going to do it shortened URL
because I think that's how it looks
and if we come in
oh yeah it is so if we do shorten
so now what we can do
is we take this whoops uh we'll do slash
so if we're going to do this we're going
to do that we're going to sorry if we if
we call short and Slash and then the
name of a URL
and then we send the URL we're going to
send that URL in and then we're going to
return let's say
um
we're just going to say this is
what you sent
and we're going to do
uh Escape
just to be safe
URL
and so if you look here
what we're going to do is
Escape that name out and then that means
if we do something like weird like this
it's still going to come through so if
we send it tags and stuff like that uh
did I get it all right I did so now
let's just go back to run it
let's run five thousand if I do my
initial one oh
a port
and then what I get is hello world but
if I do shorten
now I'm going to get that it's not found
because I can't
I did not provide a name if I do this
and I do
um
to be colon backslash
some long
url.com
slash a equals one
uh let's see I'm sorry that's not a real
one but we're gonna do it anyways
b equals two so now we've got this big
old thing
if we do that
because of the slashes it's going to
give us some issues
so
we could get rid of this
oh it's still not going to give it to us
let's go back and look at our over here
so we have shortening URL so let's go
look at what our example was here
they said
that if we do slash name and we give it
a name then that will pick that up so if
we just do a simple URL then that should
be
let's just do this
there we go
and so when we do oh it's because it has
ah
because we have parameters there okay
so right now we've already found in
we're running a little bit of an issue
with our apis we've got to be able to
figure out a way to send an actual
an actual URL as opposed to something
just very simple so here if we did
something url.com and then we did let's
do
just to show you what the tags are
I don't really we'll need it but we're
going to do it anyways
um
there's a parse error
uh
well it doesn't like that very much
anyways okay
so I'm not sure that our Escape is going
to help us very much in this one let's
go back and look at that just in case
because that should be
escaping because we're entered as text
rather than that so if I did this
is what they're saying
oh because it's formatting it okay
let's see
let's just do exactly their example
yeah so we get an issue with that is it
doesn't like what we're sending
but if we do this
because we're doing a format uh come
here
so let's just do this
let's do what they said
so I can show you this and really it's
just to do it
you set this
now we need to reload it
I'm assuming
and so now if we do it nope it still
doesn't sound like it so we've got
something in our
oh it's because of our slashes Ah that's
our problem
yeah it doesn't like that
at all in here because we would need to
probably let's go back to this if we do
this
and this will this give us what I need
nope then it gives us a we're not found
because we didn't actually get the thing
so
we're going to play around with this
first
is within our API
what we want to be able to do
is let's just do this we don't need to
escape it right now
what we want to be able to do is do is
send a URL in of some sort and then do
something with it so
so let's do
this because we're going to actually
step up a little bit because that was
just a straight out we're going to get
something but here we're going to get
some and such because this is going to
be important when we get into those so
we get into the API because what we're
going to end up doing
is we're going to be able to we're going
to want to be able to do a get
and we're going to be able to do a post
so actually we're going to do is we're
going to post to shorten and we'll deal
with this a little later
and then there's going to be a different
one that is going to be the get
I'm putting these in place these are
placeholders we'll come back to them uh
here let's see so it's like that so now
if we do a get
uh oh I need it to be here
so if I do this I should get the same
thing because I'm still doing a get when
I do the this by default when I do this
curl that's going to be a get
and let me just go back to the one that
was simpler there we go some long URL
but this is what you sent okay
so we're doing working on our git first
this is going to be essentially what
we're going to do is this going to be
instead of shorten this is going to be
well we can call it shortened we're just
gonna it's the same name here but here
we're going when we do a get then the
URL we send is we're going to send
something back this so it'll be uh
actually let's do
turn value
and the first thing is going to be this
is what you sent
and then we're going to do return value
Plus
equals and we're going to return return
value
and then it will be something that's
going to be
is
the actual
URL
and we're just going to call this result
and we're just going to say for right
now result
equals
do it like this way assume we did
something
so what you're probably going to end up
here
would be our short URL
which we can we can either do it with an
HTTP or not but let's see so if we do
here
so we'll assume that we don't have the
HTTP on it because it which would really
make sense so now it's going to be like
my short
dot com but then we're still going to
have that slash so that's something
we're going to have to worry about
so I think we did it that way how did we
do it oh we haven't really
looked at that yet so
it's probably going to end up being this
it's going to be four five something
like this is what we're going to want to
send but we're going to need to be able
to wrap that and so we're going to have
to be able to get either
we're going to end this is two things we
can either send straight code in
which is this which is probably where
we're gonna we're going to want to do
is say
although we're probably gonna have to
clean it too so we'll start with this
so we're going to start with is we're
going to send them some code
and then it's going to get back the you
know the shortened URL
and let's just do this uh
uh we'll put a little Line Feed in there
just because
not that one that one
run it
go over here boom okay so now we got
something a little easier but now what
we're going to do and you can see all
this stuff as I mentioned in here so if
you want to build something when we're
complicated then you can see things like
hey you know what is the what's the
method uh which problem
that is probably what we're going to
want to do
since it's the same one
I'm going to do this whoop
actually let's just do get
comma
[Music]
any post
and here we're going to say let's do
this
if it's post
else because it's going to be a get so
that's a get here
return value equals
not implemented yet
oh which we need to get the request is
going to have to come in
oh I think it's automatically gonna do
that oh we need to I'm sorry we have to
import that's right
so that means we also have to import the
request from flask
now that will come through
let's go ahead and show you what that
looks like so
believe
it's as simple as I think it's Dash get
okay so just to save you a little bit of
time you may have noticed a little jump
there
and what I did is I want to do X and I
put its post
there we go
uh and then I've got my little percent
after that I'm not sure why I get that
but anyways
so we get that so that allows me to post
if I do a get
and then
we're here
and now let's go back and look over one
of the things we can do
is we can send parameters
and so that's what I want to take a look
at
and again
if you want to look in here running
Direction URL building
probably what we want
here we go request data
maybe nope that's not what we want
so we go here
oh here we go
foreign
so here
let's see exchange blah blah blah that's
how I want to do oh that's if I want to
rebuild something
I'm going to do a template I can render
a template I don't really need that
right now
here we go
so I can do
that's in a post
and
let's do it this way we're going to play
with it for a second because we can so
let's come in here
I do login
nope I don't need longer that's their
thing that's totally useless to me
instead of this
I do shorten
and then I'm going to do print
request
let's try that
um
and so let's save that
I may have time jumped you a little bit
but it's because I needed to wrap my URL
in quotes as far as using curl so now if
I do it uh and I want to just let me
clean this up a tad because I want to
give something whoops
um
let me run it
unexpected event where did I oh shoot
got a space in there somewhere
there we go so now if I run it
I get success because that's what I kick
back but you can see here where I pulled
those suckers off so now
if I go back to shorten then that should
have been fine so let's get rid of that
so now
if I go back to shorten
let's go all the way back
uh
if we go back to here the one we did all
the way here we go big long URL
that's we're going to do URL equals
uh we have to just wrap this
and then we come all the way back here
there we go and now this is what you
sent so we did something but notice that
it actually let's see what did it get
oh because I see this is where we're
going to run into some issues this will
be a little bit of a challenge we're
going to want to put we're going to want
to post this
because if we wrap it into the string
then we're going to end up with some
issues so we're going to be having to
either
oh which we won't have to do this would
actually never exist I'm sorry because
what's going to happen is what we would
normally do is for this for the
shortened
we're going to post
uh so we hit a colon backlash slash
short name
some number there okay so it's going to
come through
and so now
we have a an API and I think I'm going
to hold here
because what we've done is we do have
creating an API using python because we
can use this using our flask run
and we have our uh we're using it in
flask as well
so we do have a little bit of testing an
API call but I want to talk next time
we're going to come back and we'll go a
little more deeper into this now that we
have this out here and find some easier
ways than what I just went through with
curl as you can see is a little bit of a
pain and we're going to check this thing
out called Postman
that being said get back to your day
hope you have a good one and we'll talk
to you next time around
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.development or we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
foreign