>> In this last episode of
our four-part series on Python and Django on Azure,
Carlton Gibson is here to show us how you can run Python Apps with
Django in a Serverless way using
Visual Studio Code and Azure functions,
today on Azure Friday.
[MUSIC]
>> Friends and Pythonistas welcome to Azure Friday.
I'm Nina Zakharenko, a
Senior Cloud Developer Advocate at Microsoft.
Here with me today as Carlton Gibson,
he's a Django fellow as well as a maintainer of Django,
Django Rest Frameworks and a lot of
other Packages that you've probably heard of.
In the last few episodes,
we've been talking about how to develop
a Django application with
the X code and deploy it to Azure App Service.
Today, we're going to talk about
a very popular topic Serverless. Carlton.
>> Okay, brilliant. Thank you Nina.
So the Microsoft Azure Service offerings cool functions, okay?
So the first thing we're going
do is go back to VS code and we're going to
have the functions extension and this
enables us to create new Functions Apps.
If we pull up the commands,
we can type in functions,
we can create a new function,
create a new project, functions projects,
we can do all the things.
I've already got a function's project created here.
I've already initialized it but you can initialize
your project in VS code and it will run through all that for you.
>> Excellent.
>> That's good. What I'm going to do though,
let's create a function.
Okay? Select the folder and select function template.
Well, let's use the HTTP trigger because, with Django notes.
Provide a function and well let's just call it HTTP trigger.
Fine. Let's go anonymous.
Brilliant. Okay. Wow, it's created this code for us.
>> What is a HTTP trigger function?
>> Okay, so that, basically what
it does is it creates a web endpoint
which will take a web request and it will return a web response.
>> Great. Pretty simple.
>> Yeah, pretty simple. So we can see that going on here.
The function, the bit here is
this main function and you can see here it takes in
a function HTTP request and it returns a function HTTP response.
Okay? So the request here has
all the things we like
parameters which you will get from the query string.
It says the name, we don't enter a name,
it will say please pass a name.
So, it's a standard thing.
Let's just have a look at it.
So we can open it up in the shell and let's just run it locally.
>> So there you just typed in "func host"?
>> Yeah, which is the command line tool and
if you wanted to you could do this;
you could run a task
and you can run your functions host using the tasks command,
which you can also get to from the terminal.
>> Great. It's always great having options.
>> Yeah. So this is up and running and
this is what we just created.
I'm sorry, there's things in here. Let's have a look.
Oh, pass a name,
in the query string or in the request body.
So let's put a name in, name equals Nina.
It says "Hello, Nina".
>> Hello Azure function.
>> Well, there you are. All right. Okay, so that's the basics.
Let's do something more interesting.
So let me quit this.
Stop the interpreter there.
Then let me just go to the debug activity panel here.
We select this attached to Python
functions option and we start that up.
While that's starting up,
I'll talk you through what we've got.
Let's hide that. Okay, so
if we come to the code and if we close
this one and our function is in Serverless.
This one that was created is very simple,
ours is a little bit different.
So let's look down here at our function.
Again, it's the main function there.
It takes in a function HTTP request
and it returns an HTTP response.
Okay? That response we get
from this Azure Application Object here,
which is an AzureWSGIHandler.
Now, what's WSGI?
WSGI is the Python standard for web server application frameworks.
So it stands for Web Server Gateway Interfaces,
like Common Gateway Interface, CGI evolved.
And the great thing about WSGI
is all the Python web frameworks use it.
So Django serves WSGI is a WSGI application.
Flask, Zones, they are all WSGI.
So if we can use
the WSGI application in
functions then we can use any Python web framework in function.
That's the goal here. Now, this is very early prototype
but what we're going to do is wrap up
the REST framework tutorial project using
this this wrapper that I wrote
and then we're going to serve it in functions. Okay.
>> That's incredible.
>> So, the debugger is up and running.
API Serverless. I save it.
Oh, and that is now up and running.
Let's have a look and where I need to look.
Okay. Localhost not one two.
There you go. Always get the wrong URL, there you are.
>> It happens to the most of us.
>> It happens. Right. Now, this is hit my breakpoint.
>> That's awesome.
>> We can have a look. We're in here so let's step over
this and let's step into the Azure application.
Okay? Let's get rid of this for a second.
What we have here is AzureRequestAdapter.
Because Django doesn't take Azure requests.
>> Right.
>> It takes WSGI requests.
So what we have to do in the adapter is we have to map
the Azure request into something that Django
understands which is a WSGI request.
So this gives us the environment which
is a dictionary of the parameters in the environment.
Then the other thing that WSGI application needs
is a callable that takes the response.
It takes the request headers,
so when the application is ready to return its data,
it causes StartResponse callable with the headers basically.
So, we get out of this a Django response.
Now, if we pull up the debug console, we can look at this.
>> Not quite yet. I think you have to step over one.
>> You're right. Well done.
Thank you. I always make that mistake.
I hit the breakpoint on the line before it's evaluated.
There you are. All right, now I get
the autocomplete. It looks brilliant.
Okay. It's a Django response and it's got
all the properties of a Django response.
It's got the content; what's the content?
Well, it's a big long HTML string which is the one that's
returned by the Django framework application to re-run.
So let's just put a breakpoint in here.
Let's continue to hit our next stop.
So we advance here and so what we
do just before we return the response
we have to map it back to an Azure response.
So the Django response is a WSGI response and what we
want is a function HTTP response,
so here we just map it back and we get
the Azure response and we map the various properties,
the attributes it has,
from features of the Django response and then we return it.
>> So here you've implemented this mapper for Django
but it would also be straightforward to
implement it for any other Python web framework?
>> Yeah. So the wrapper is a work-in-progress.
>> Yes.
>> It's a prototype to can we get Django serving in functions.
Can we get any WSGI application serving functions? Well, yes.
Here I'm using the Django response
but yeah we could make it Flask and then we could
make it generic WSGI and that's the goal as time goes on.
So anyway, that's the prototype.
We return the response and then if we just step up,
that's the response that gets mapped back into
a function response is what we return and so if we just continue.
>> Yeah, let's check out what that looks like.
>> There's our same application.
It's the same application we've been using
the entire time but just now it's serving functions.
Now, we can deploy that,
we can just figure it out,
and we can pick deploy functions out
but we've already got that up there.
There you are. Oh, let's just get the URL right.
>> Yes.
>> They you are. Again, the same application
we've been using the whole time but
this time deployed in functions.
>> That's incredible. Today, on Azure Friday,
we learned about Serverless Django Applications
on Azure functions.
Carlton, thank you for coming on
Azure Friday and teaching us about Django on Azure.