Hey friends, Azure Functions has a new
programing model for Python developers. And if that's you, you'll find the development experiences
that much more familiar. Shreya and Gavin are here to show me
how much has changed with Python and Azure Functions with this
new programing model. Today on Azure Friday. Hey friends, I'm Scott Hanselman
and it's Azure Friday. I'm here with Shreya
and we're going to learn about the new programing model for
Python in Azure Functions. How are you? I'm good, Scott Thank you so much for having us here
today to talk about the new model. Yeah, so is this new model,
we call this a V2, like we've changed something
fundamentally? Yes, definitely. So our ultimate goal with this new
programing model, the V2 programing model is for developers to use Azure Functions
in Python to have a more intuitive, more familiar experience
when it comes to developing functions. Okay, so more familiar
not from a Function's perspective, but more familiar because
they're Python developers and they expect a certain thing when
they're using Python in the cloud. Exactly. Yep. So that means fewer files and then
using decorators in Python, which I assume is going to make things more intuitive
for them as Python developers. Yup. That's the hope. That's the feedback
that we've been getting and we're really excited to get more feedback
from customers as they try out the model. Very cool. So, this sounds like it's
going to be a lot more idiomatic. Can you help people who may
not be familiar understand like why I would use an Azure Function
over something else in Azure? Maybe you have some slides to show me. Yeah, definitely. So, if you haven't used functions before,
Azure Functions is an event-driven service and it provides compute on demand,
and when you're using functions, code and logic that you have,
it can run when triggered by events and it really allows you to focus
just on your code itself and everything else
we want to take care of for you. So, really have that focus and attention
on the code that you're writing in. The logic that you want to create
functions also has a lot of out-of-the-box technologies as well, such as extensive
monitoring and observability. So all this really sells the point
on why you should be using functions to configure your code in the cloud. And I do want to go over a few different
common examples and use cases
when it comes to using functions. So this first slide I have over here, this represents
the automation of some scheduled tasks. So we start with a database
that needs deduplication and cleaning and we have a function
that runs on a timer schedule every 15 minutes
that goes ahead and cleanses the database and duplicates the entries
every 15 minutes. So this is like a common example
and why someone would use a function when it comes to their data. And then after this,
I have another example which includes a real time
stream processing. So we start off with this app or device
that's producing data and we want the end result
to be a data visualization dashboard so we can make analysis
on the data itself. And as you can see in this diagram,
we use a function to, to process the data from the Event Hub
and then go ahead and send it to a Cosmos database where it's ready
to use be used for visualizations. And then before
we kind of get into more about this V2 programing model, I do want to go over
the core of the functions programing models in general,
and this includes three main components. The first one is the required trigger
and a trigger. It causes the function to run. So this could be on a schedule. So a timer, it could be an issue type request,
it could be when an event is processed. But the trigger is really what started this function and continue
to make sure it can run. And then next,
we have two optional things as well, which are the input and output binding. So as you can see, the input binding,
it can be some additional data that you might want to use
during your function. And then the output binding
could be maybe somewhere you want to place data
after the function runs and the central lightning strike
that represents the Azure Function. And that's going to be your code. The logic that actually processes
the information being given. And then also what is an output as well. Interesting. Okay. So, those bindings are kind of like parameters and return values,
except they're coming from cloud context rather than being legitimately passed
in as parameters. But from the perspective of the developer,
it feels like it got passed in as a parameter and
I can just call a function in the cloud. Yes, exactly. That's very cool.
I'd love to see this, like for real? Yeah. So we do have Gavin who's
going to give a demo and right before he does,
I do want to go into more detail about what does it mean
that we have this V2 model? So to be more concise,
I do want to make sure that it's known that the, the integrated programing model,
the thing I just described that's going to remain the same,
that's not going to change at all. That's going to be
the core of the programing model, whether using v1 or v2. But what will be different is the files that you interact with
and the code that is developed that outside wrapping stuff,
that's going to be where the change is. And that really brings me
to why we decided to create this new model in the first place. And that's because we've been talking
to customers and getting some feedback that there is a little bit
of a learning curve when it comes to being a native Python developer and then going on
to create a function app. And we want to do as much as we can
to make it easy for our developers to get started as easily as possible,
to make sure that they can have an intuitive experience
when it comes to developing functions. So, before I hand it over to Gavin, who's
going to take us through the demo, I just want to highlight a few of the
features that you already mentioned as well that we're going to be changing
with this new programing model. So first we have triggers and bindings
represented as decorators. This is going to make it
so it's similar to other well-known Python frameworks that currently Python
developers are using. We're also going to introduce
a simplified file structure. So, previously we had a folder
and multiple files required per function, but now we're going to
just have one function that captures - one file that captures all the functions. And so we're hoping that's going to drive simplicity
and ease into development as well. And then the last one, which is my favorite to talk about,
is the easier access to documentation. We're adding more avenues
where developers can get documentation, and this includes IntelliSense,
which is going to give you hints as you type, as well as
a new functionality allowing you to preview the template code
within VS Code that's going to allow you
to get comprehensive documentation without ever leaving the IDE. And with that, I want to bring Gavin on
because I know he's going to be walking us through and showing us
examples of what I've just talked about. Very cool. Welcome, Gavin. Yeah, Shreya has gotten me up to speed
on a ton of changes here. Let's see them in action. Awesome. Thanks, Scott. Thank you, Shreya. So let me start by actually,
how do, how does a developer actually use that and create a function
using the V2 programing model? So and let's try running this
within a few minutes and I'll show you how quick and easy it is
to get it up and running. So as you can see, I have my Studio open
with this, an empty folder and it said to create a function right now
I'm going to do is open my command pallete
and that's to create function and yes, I want to create any project. And once you see we have a Python new programing model
also along with the regular Python. So, once we select this, it's
going to select the new programing model and, and we set up the interpreter. And right here, as you see,
this actually gives you a list of files markdown file explaining. What's the new programing model,
how to use and and also a description of every file available once it's useful code snippets to get you started with it. Actually, as I pointed out before all the functions right now will not
you won't see a different folder for each function, but every function
right now is going to be in this one file. So it gives a very concise way. And very easy start with functions. And let me start by actually creating a function for you and running it locally. So let me just delete the comments
right here and now to create a function. What I do is again, I open the command
palette and create a function. I yes, I give it a moment and it's loading. Looks like we're creating a virtual
environment, a virtual Python environment. Awesome yes, yes. That it creates
all that for you on the fly. So let's try creating an HTTP
trigger. And let's add this to go to the function
underscore app file and here you go. So right here
it just gives you a quick API to call and HTTP trigger and this is the sample code
which comes along with it. So the user can actually just run this
and just see that the function is working. And yeah, and so this is an easier way to actually create a function using
come on the command palette. But let me show you how we can actually
create this on the fly also. So let's try to create
a different type of vehicle right now. So that's
let me try to create like a timer trigger and as you see for a new developer,
if the IntelliSense was just don't you all that you need to know
about creating a new function. So for the timer trigger,
I do a schedule. Now what are the options? What are the arguments
that I need to pass in this in this time? Think I will also show up in the sense. So right now it shows me,
Oh, I need an arg name. So let me just put an arg name and let me just name it mytimer. What else do I need? I need a schedule. So let me do scale Now, what is schedule?
as you see it, set the schedule. It's like a CRON expression. I said, okay,
so let me create a go on expression. I just want this to run
maybe every 3 seconds. So this and these expressions are familiar
for people who've done CRON, but I personally use the different CRON
expressions websites that will help you write those out. Exactly. This is this is an account that you can draw an expression
which you can use in this trigger itself. Mm hmm. So let me just stop anticipating
a function. I create, you know, creating a timer
because I'm passing in my timer right now. So the best part of this is, if you listen to my I,
you don't have to know what is one of the one of the methods
experts to just easily shows me everything what I need to know
instead with IntelliSense. Now all I want to do with
this is just probably just give a log every 5 seconds. The thing only time. Oh, so we have to function pretty,
but let's try running this. So I open my terminal. Okay. My environment
that's already activated. So all we need is to a function or stop. This is very similar to how even programing model function. Now let's see what we get. Oh, so one of the thing is
it shows me that I don't have a storage account, so
let me try to create since this is running locally, let me just use a storage
emulator to run this account. So let me just initialize
my storage emulator let me start my storage emulator right now. So I have external emulator ready
and it's already started. So let's run the command again. Let's see what we get. All right, we're bringing up that runtime. So you're running an Azure Function
locally, not in the cloud, but locally with the Azure Function runtime, right? So, as you see, we have two functions
which got exposed. One is the HTTP trigger that we have
and the time data and from the logs you can see that all the time to go
was already started working. So every 5 seconds you get this. Now let's try invoking
the frame of itself. So let me just use my local client,
which I have finished. Since we're running the function on localhost 7071
and we're going to call that's to the default port. All right. To show trainings. And we got this one from the state,
which I got. So. So as you see,
we have two functions which actually ran and it took just a few minutes
to create a function, create to function actually, and get it
running for the functions itself. And if you return that source code,
if you don't mind real quick show in the function
app P and it scroll up to the top there. You've got on at the beginning of your function,
you've got that on lines nine and ten, you've got that decorator,
that app decorator giving Azure. I assume some context
about how this function is going to be like because line 11 is just
a regular Azure Function, originally a regular Python function rather
it's been decorators on nine and ten. That adds a magic thing. Exactly. So if you see line up at end, this line number ten
is replacing the entire function or JSON file, which we had in the view
and programing model. So now you can just skip the whole file
and just add this one line. It'll tell the Python that this is
the end, it's a Python, a function. Okay. And then the function name HTP Trigger one is how this will identify itself to Azure, even though the functions called test
function. Exactly. Yeah. Very cool. That's a great use of decorators
in Python. Right. I also wanted to bring up that
this is just a small demo example, but a lot of developers are going
to create on more complex examples and I wanted to show how we can actually
use this in a much more complex example on how we can navigate
leading documentation and actually coding this function together. So let me show my other screen. So for this example, what I want to do is I want to create a back end service
for a day of the app. So I know that for the funeral
we have quite a few APIs, like calls,
like I want to create a new order. I want to maybe get an order,
maybe I want to do it. And so I know I need three
at least three API goals. And also once I have the API,
I want to do something with the order. So I want to create like an event
once the order is created. So that event is going to once I have that
event, I can do some processing with it. And once I have, once I process it,
I ultimately want to save it somewhere for later use. So I know that I from my previous example
I'm going to use and instead of being out trying out my stupid thing,
but also I know I want to use event ops and once I have event ups to store it,
I also want to use as a storage. So now how do I actually use these things
in a function? So let me show you how my function app is going to look like
with these functions. So I have created a few APIs
which we needed. So the first API you
see is just getting orders. It's just a regular function, which I got,
which just it on the orders. But we have my next
function is to create order. Now if you see this card or it does have, what do you see? And instead of being single,
but also you see there's also an output binding associated with it. Now what this will do is once
we get the take of they're going to move the order to the event hub. And once the event hub is triggered,
we want it to save later on. So that's why you see that
we are using an event of output binding. So this is what we're going to do at line
number 25. It's basically going to set the event,
create an event and send it to enter, which I have right here
and make love connection. And lastly, we have a delete API,
which will just delete the API of the records of any database and you see all these function definitions. It's in drought, digital, and also
it has the method which it's going to use. So the delete function is
money is a delete method to create is going to use
like what and get is going to use that
to get order on to use object. So once we run this will actually see
all these API is being exposed. But yeah, lastly, we wanted another API
which will actually another function, so that will actually store these
API stored orders in a storage account. So we have two options right now. I think we could create a new function
right here in this file, but as you see, creating more functions
within the same file can get this file extensive
and a little difficult to maintain. So for this reason,
the new programing model exposes or has a concept called blueprints. Now, blueprints are very similar
to what blueprints is in flask, what many users are familiar with. So even in functions,
the blueprint is a container of it have it
has a bunch of different functions, and this functions can be referenced
in a main function that. So you don't have to have only a function
that once you can update it as you wish for submission of contents and you can just reference
at the main function. So as you see,
I have a new file here called Blueprint Step UI and initialize file. Okay, I want to define to be a blueprint. So I just want people to find a blueprint and the rest is the same. It's just the same function. How I create a hidden
event up to here in this case. Because you know from my previous create method, it's
going to send it to the event hub. And this event hub is going to get bigger
when a new event comes in. And once this event gets bigger,
I'm going to do some processing to it and then ultimately I'm going to save it
to a block stories account. This amount is actually going to run
my function underscore app. Once it gets all the methods in this, it's
also going to show you the blueprint method itself. So we expect to see at least of we expect
to see four functions being created. And like you see, we have the three
functions which we have in the function and the function in the blueprint
also showing up of you. And we can quickly and I set out,
if it's working correctly, let's say I have eight orders, like now it's empty,
so it's going to give me an empty list. Let's create something in it. I created the order, so if you see the logs,
it creates the order it executes to create a cycle. It also executes the event happening and
then it saves it to the storage account. So, you know, a new order being created
or a 951 let's run this and you see the orders
showing up in the list of orders. So let's try deleting this auto and just if you send it and if you're doing an order,
it should be empty right now. So this is a small example of how the new programing model
can be used in a real world example. And yet this feature is actually helps us to get
and get a bit of new function really quickly
and very intuitive to Python users. And right now this is in preview. So we have the tool we have. We do have a lot of people
actually using it right now, but we want a lot of more people to use it
so we can get the feedback, what we need and how do we improve this function up
and make it better for the other users. One question I have is blueprints. Traditionally, in Flask, you know,
our way to organize related views in code is this a flask blueprint
or is this a conceptual blueprint? In the context of Azure Functions? It's a conceptual blueprints
often invented for functions itself. So it's the same concept, but it's it's monitored for functions right now. Okay. So it's meant to make people feel
comfortable and say, Oh, I know. Flask So blueprints and Azure
functions work the same way. Exactly right. That's very cool. All right, let's bring Shreya back in. This has been pretty cool. This this really is going to make things
a lot nicer for folks
that are familiar with Python. It feels like you say idiomatic,
it feels comfortable. It uses decorators, it uses,
you know, blueprint style structures. It sounds like Shreya, this is going to be a big win
for anyone using Python who wants to put the serverless things
in the cloud. Yes, definitely. We're really hoping that this can like
these aspects can really make customers more productive
as they're developing functions. Hmm. Now, do
I just have to go and update my extension? Do I already have this feature?
And I don't know what you might
you do have to update your extension and all the details on like the number
and how you can get started. They can be found on
aka.ms/PythonProgrammingModel Very cool. We're going to go ahead and put links
in the show notes and folks and learn more about the new Python
programing model v2 for Azure Functions. Thank you both for your time
and for hanging out with us today. Thank you so much. All right. I'm learning all about Azure Functions
using the V2 Python programing model today on Azure Friday. Hey, thanks for watching
this episode of Azure Friday. Now I need you to like it. Comment on it,
tell your friends, retweet it. Watch more Azure Friday.