Best Beginner guide for Python on Azure
Key takeaways from KonfHub contest #azuredevstories
Introduction
I recently participated in technical blogging contest cum learning program organized by KonfHub where all the participants have to complete any one of the tracks on MS Learn and I have completed the Python on Azure track in it. The goal of the initiative is to skill developers to learn new technologies on MS Learn and create content sharing their experiences building on Azure.
While completing the Python on Azure track I learnt how to build an AI app by using Python and Flask, How to automate Python deployments using Azure pipelines and How to host a web application using Azure App service.
I would like to make this more beginner friendly and explain What is python?, why python on Azure? and a few concepts revolving around Azure Functions by wrapping up with a fun project….:)
So let’s see What is python?
Python is an interpreted, high-level and general-purpose programming language.
Python is often used as a support language for software developers, for build control and management, testing, and in many other ways. It uses a simple object-oriented approach and very efficient high-level data structures.
People prefer Python over French
Unlike other programming languages python is fun to use, highly extensible and focuses on code simplicity and readability. If you get to the IDLE and type in import antigravity, it opens up a webpage with a comic about the antigravity module.
Now let’s see How Python on Azure creates magic?
Python on Azure is for Building web apps in the cloud, AI and Machine Learning, Dev tools(Visual Studio Code) and DevOps. Using Visual Studio code which is a code editor optimized for building and debugging modern web and cloud applications, python apps can be built on Azure.
Azure Functions
Azure Functions is an event driven, compute-on-demand experience that extends the existing azure application platform with capabilities to implement code triggered by events occurring in azure or third party service as well as on-premises systems.
Azure Functions are the individual functions created in a Function App. Every Function can be invoked using the configured trigger.
Azure Functions improve your end-to-end development experience, simplify complex orchestration challenges resolution, connect other services without hard-coding integrations for faster solutions development, can be built once and deploy anywhere and allows ability to use multiple languages in development(Develop your way).
Microsoft named a leader in The Forrester New Wave: Functions-as-a-Service Platforms
Now let’s use Visual studio code to create a python function that responds to HTTP requests. First install the Azure Functions extension for VS Code and select create new project icon from activity bar. For language choose python and select a python alias to create a virtual environment. Choose HTTP trigger template and give a function name.
Next, let’s run the function locally before publishing to azure. Under local project > Functions > choose Execute function now on the HTTP example. Press enter to send the existing message request to your function. When a notification is received in VS Code then it resembles that the code is executed locally and responded to the message request sent.
Next, sign-in to azure and publish the project to azure by choosing the deploy to function app button in the activity bar. Then select folder, subscription, function app in azure, region, runtime and a unique name for the function.
After the deployment, execute the same function again to see if the notification is being raised again or not. If the notification in VS Code is being raised again then it resembles that the function is working correctly…:)
Now let’s create a Web App and deploy it using Azure App Service. Before that let’s understand What is App Azure Service?
Azure App Service
Azure App Service is a fully managed web application hosting platform. This platform as a service offered by Azure allows you to focus on designing and building your app while Azure takes care of the infrastructure to run and scale your applications.
Every web app created on App service needs a deployment slot for deployment. Staging Deployment slot allows the users to test their code before production and upon successful results the same staging deployment slot can be modified to production slot.
Deployment to App service can also be done using VS Code extension called Deploy to azure. After installing the extension, start adding the CI/CD pipelines to you apps and have them deployed to the cloud continuously.
Creating a Web App
To create a web app, we need an Azure account(which can be created for free here). After getting a free account in azure create a web app in the Azure Portal with the following configuration.
Navigate to top left corner and click Create a resource and search for Web App. Click create. Use Concierge Subscription and select the Sandbox resource group in the drop-down of resource group. Select a unique name for the instance and use .NET Core 3.1 as Runtime Slack. After that select the Free F1 for size and click review and create.
It will take a few seconds for the web app to be deployed and after the deployment completes we can get a overview of the web page when we click the Go to resource button. The web page that displayed is the default web page which is created whenever a web app is created.
To confirm the app is running properly click on Browse Website to view the default web page of the web app. To preview your new web app’s default content, select its URL at the top right.
Similarly a web app can be made using VS Code and can be deployed to Azure from Visual Studio code itself. So let’s see how does it happen…
To start off, let us integrate Azure to Visual Studio Code. To do this add the Azure Tools extension to your VS Code and sign in using >Azure:sign in. After that create a folder for the web app files with main.py file and requirements.txt file. Paste the following code in main.py file and write flask in requirements.txt file.
from flask import Flask
myapp = Flask(__name__)@myapp.route(“/”)
def hello():
return “Hello World”
After installing all the requirements from “pip install -r requirements.txt” set flask app environment using “set FLASK_APP=hello:myapp” and run the app using “flask run”. This will create a local environment for the application and displays the web page in localhost.
Conclusion
With this technical blogging contest by KonfHub, every student practices the habit of reading documentation which helps in exploring the professional way of dealing with errors. Thank you KonfHub for this wonderful experience. I would love to participate in more such challenges.