Azure Function Deep Dive (Python) | Work with Modules & Packages | Change Function Properties

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone, in this video we will do a deep dive in Azure functions for Python. You will learn how to make changes to Azure function properties, work with multiple Python files or modules, import from packages, and create and use multiple functions in the same Python file. Alright, let's go ahead and quickly create an Azure function for us. Today I will do this process a bit faster because we already have a detailed video for that on our channel. Let's go here. Create a new blank folder and name it Testing Azure function. In this folder I will create an Azure function for me. So within this. Let's launch our terminal and launch VS Code. After launching VS Code, you need to go to this Azure. Click on this Create Function. Select the folder. Select the language. We don't want a virtual environment. We want an HTTP trigger. func-multiplefiles. Let's name our function authorization level. We'll call it function. Right. Alright, well, these steps are completed. You can come to the Explorer and see that OK. VS Code has created for you a basic boilerplate code for your Azure function. Now the creation of function step is completed. Let's quickly create an app also on which we will deploy our function. Go to Azure, expand the subscription, right click on Function App and click on Create Function App in Azure (Advanced). Enter a globally unique name app-testingofmultiplefiles. This should be unique enough. Right. This is unique. Press enter. Select the Python version from this dropdown. It is loading. OK, let's select Python 3.9. Let's select existing resource group on Azure. The plan should be consumption. Let's select existing storage account on Azure. And let's select an existing App Insights Resource also. Once this process is completed, it will take a few minutes or seconds to deploy the app. All right. So our app deployment process is also completed as the app is also created. Let's deploy our function to the app. Click on this. Deploy the function app and select the app name that we created. app-testingofmultiplefiles. After a few seconds. You will see a pop up right on this pop up. Click on deploy. It will take a few minutes to complete the deployment process. The deployment process is complete. Let's quickly test our deployment. Expand the function app, expand our app that we created, expand the functions, right click on the function and copy function URL. Go to the browser and paste the URL here. Now, if you run this URL without any query parameter, it should return this line. This HTTP triggered function executed successfully and you can compare it with this line. This HTTP triggered function executed successfully. This way we can see that our deployment process is successfully completed. Let's go to our folder structure once now and see what all files we have inside our function. So inside our function, we have __init__.py, function.json and sample.dat. So __init__.py is the only Python file we have. You may wonder, once we deploy this function on Azure, how does Azure know that it is supposed to run this very specific file __init__.py? And how does it know that inside this file it is supposed to run this function, the def main? When you are looking at this code right now, you will say that, ok Mayank, there's only one Python file that is __init__.py. So Azure function just can go into this file and inside this file, there's just one function main. So Azure function will automatically go there. But that's not what happens. All these properties are defined in this function.json file. If you go to this file, you will see that we have a property called scriptFile. And inside this property, we have the name of the Python file __init__.py. So this property tells Azure function that which is the Python file that Azure function should run. Now, let me show you an example. What I mean by this, let me rename this __init__.py file. I will select this, press F2 and let's rename it to test.py. Right. I'm not changing anything else and I'm not updating the name in function.json. And you know, already know that whenever you make any change in the folder or any file, you need to deploy those changes. So let's go to Azure. Again, deploy changes, deploy to function app, app-testingofmultiplefiles. It will show the pop up. Let's wait. On this pop up, let's click on Deploy. Let's wait for the deployment to complete. All right. Our deployment is complete. Now, let's again go to our app here, expand our app and let's try to expand our function. Now, you will see that we do not have any function here. You can ask me why. The reason is we updated our py file name. We changed it from __init__.py to test.py. But we did not update it in function.json. This is still __init__.py. This is what happens if you have a file in this property which does not exist in your folder. Let's now try this again. Now we have changed our file name to test.py. Let's update this name here also. Let's change it to test.py. And we know whenever you make any change to any file, you need to deploy those changes. So let's come here, deploy the function app, app-testingofmultiplefiles. And wait for the pop up. Yes, let's click on Deploy. Our deployment is complete. Let's now try again to expand the functions. And this time you have your function name on func-multiplefiles. Copy the function URL. Try to run it. And you will see that function ran successfully. Now you know that whenever you change any Python file name here, you can change it in function.json. And it will work perfectly fine. You need not, you know, only work with that __init__.py. Now the next question we need to answer is, how does Azure Function know that within this test.py, it is supposed to run this main function? You can say that Mayank, there is only one function in this file. So maybe that's how Azure Function knows. But no, that's not the case. Even if you have more than one function here with any name, Azure Function in this current scenario will only run this main function. Or I should rephrase myself. It will start the run from main function. Now, how does that happen? Let's go to function.json once and see if we find anything. We have a property of file, but we do not have any property of function. That is because there is a property which if not specified by default runs the main function. So that's what's happening now. Let's go to the documentation so that I can explain it to you a bit more. I will share the link of this documentation, obviously, in the description below. So this is Azure Functions Python Developer Guide. Let's scroll down a bit. And here you will find this section, Alternative entry point. This is what we are discussing right now. scriptFile we already discussed. The next point is entry point. entryPoint property tells the Azure Function which Python function should it run within the scriptFile. And if you do not specify the entry point property by default, the value is main. That is main. In our scenario, what's happening is. We have not specified this property, so that's why it's by default assuming main and running that. Now, let's try this. Copy this property here. Come here. Function.json and let's paste it. Let's specify. Let's specify test_2, maybe. All right, let's do that. And let's save this now. You know, once you make a change, you need to deploy those changes for that. We need to go to Azure. Click on deploy. Deploy to function app. app-testingofmultiplefiles. Let's wait for the pop up to show up. And click on Deploy. Our deployment is now complete. Let's again try to run our function. And this time you see that we have the function. Let's copy the function URL. Go to the browser, paste it and press enter. And you will see that we are getting this error. Why are we getting this error? We are getting this error because the function name in our file test.py is main. But the function name in our function.json file is test_2. Now to prove it to you, let me do one thing. Let me change the name here test_2 save it. Go to Azure and deploy this again. And click on Deploy. The deployment is complete. You can see that loading here is complete. Let's again try to copy the function URL. Go back to our browser and this time it should load. And you can see that the result loaded successfully. We can see test_2. This statement loaded successfully. Why? Because the function name specified here matches with the function name specified here. Now let's see how you can work with multiple functions. Let's go to a test.py file and let's say I want to create a function which returns the sum of two numbers. So define some number. We have num1 int data type, num2 int data type and it also returns int data type. And num3 will return num1 plus num2 returns num3. Right. Our function is ready. Now we know that just creating this function will not do anything because our script starts from this function test_2 function because that is what's specified here. We need to call this sum numbers function within our test_2 function. Let's try that. Let's create some variables num1 is equal to 10 num2 is equal to 20 and num3 is equal to sum numbers num1 and num2. Now we have this num3 variable and we want to see this value in our results. Let's completely replace this string from here. Start an f string. The sum of two numbers. What we have num1 and num2 is num3. Right. Now when we run this function this should return to us 10 plus 20 that is 30. You already know whenever you make any change to your function on your local you need to first deploy those changes. Let's do that. Let's click on deploy to function app. app-testingofmultiplefiles. And wait for it. All right. Yes let's Deploy. All right. The deployment is complete. Let's again load our function here to copy its URL. It is loading. Let's wait for it to load. It is loaded. Copy function URL. Go to the browser. Create a new tab. Press enter and you will see the sum of two numbers viz. 10 and 20 is 30. So that is how you can work with multiple functions in a Python file in Azure Functions. One more important thing that I would like to point out here is the function will work perfectly fine if you define these variable outside the test_2 function also. Why? Because then these will act as global variables for our test.py script. The important thing that you need to make sure is that your sum_numbers function should be called from within your test_2 function. Because from this function our test script starts its execution. All right. Now you may ask if instead of defining the function in the same Python file, what if you need to create a new Python file? You may have like 10, 20 different functions for it and you want to create a separate Python file for it where you will store all your functions. So we can do that also very well. Let's see how you can do that. Come to your this function folder, create a new file. Let's call this sample_codes.py. And in this sample_codes.py, let's transfer our function from here to here. Right. After this, you can. OK, let's test this example in this manner. I will keep num1 and num2 as global variables here only. Now, you need to do an import here. And here you need to make sure that you do a relative import. Absolute imports will also work, but I just like relative imports here. from.import sample_codes as sc. Let's call it sc. Right. And we will append sc here and the error is gone. Let's see if our function will work perfectly fine. You already know the process. Once you make a change on your local, you need to deploy those changes. Let's click deploy to function app, testing multiple files. And let's click on deploy. All right. Our deployment is complete. Let's copy the function URL. Go to the browser, open a new tab, enter the URL, press enter. And you will see the same results. The sum of two numbers viz. 10 and 20 is 30. The same result we saw when we ran the function from within the same file. All right. Now we know that how we can work in Azure function with multiple Python files. Let's cover one more thing before we finish. In the current example, we saw how you can import a Python file or a module, but the same thing can be done with packages also. Let's say you have a separate package in your function in which you have this sample_codes file. So how will the import work in that scenario? Let's quickly do that. In your func-multiplefiles, create a new folder pkg1. And to tell Python that it is a package, we will create a file __init__.py within this. And we will transfer our sample_codes.py file within pkg1. Now our folder structure is this. We have func-multiplefiles. This is our Azure function and we have test.py within this. And then we have a package in which we have our sample_codes.py file. Let's go to our test.py file and you will see that Python extension has automatically changed my import to this. And let's see if it works. For that, you already know we need to go to Azure. Click on deploy to function app and app-testingofmultiplefiles. Click on Deploy. All right, our deployment is complete. Let's copy the function URL, go to the browser, create a new tab. There is a URL. Press enter. And you will see the same result. So the same result with package import, with module import and with function import from within the same file. Now you may ask what happens when you deploy a function with packages or modules. You can go back to your function. Let's, I mean your Azure. Let me take you to the home first and show you how you can navigate. So here you can search for your function app. Click on function app. This is the app that we created, app-testingofmultiplefiles. Click on this. Go to functions. This is our function, func-multiplefiles, func-multiplefiles. Click on this and go to this Code+Test. In this window, you can access all your code files. It may take a few seconds to load. It has loaded and you see by default it will load your default execution file that is test.py. But if you click on this dropdown, you will find all your files that are available here. Test.py, function.json and sample.dat. Alright guys, so this was all for today's video. Let me know if you like this video in the comments below. And let me also know if you have any ideas for the future videos. Thank you.
Info
Channel: CodingInfo
Views: 6,192
Rating: undefined out of 5
Keywords: azure, python, azurefunctions, vscode, visualstudiocode, python39, functionapp, folderstructure, multiplefiles, pythonpackage, pythonmodule, package, module, property, properties, functionproperties, function.json
Id: 7BcFGj4F5aI
Channel Id: undefined
Length: 18min 53sec (1133 seconds)
Published: Sun Feb 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.