Home Azure Cloud Deploying Python (FLASK) App with VS Code To Azure App Service

Deploying Python (FLASK) App with VS Code To Azure App Service

293
0

In our previous posts, we covered how to set up VS Code for Python development and how to configure VS Code for Azure development. In this post, we’ll put those two together and show how to use Python inside VS Code to build and deploy applications directly to Azure.


Why Python + VS Code + Azure?

  • Python is one of the most popular languages for data science, web apps, and automation.
  • VS Code gives you a lightweight but powerful development environment with great Python and Azure support.
  • Azure provides cloud services (Functions, Web Apps, Storage, Databases) that can scale your Python projects to production.

Together, they create a smooth workflow: develop locally in Python → test in VS Code → deploy to Azure.


Prerequisites

Before you start, make sure you’ve completed the following prerequisites

  1. If you are new to app services and app service plans, watch this video Getting Started with Azure App Services. Note: If you are part of 4MCEA, please refer to the app service session recordings and labs from the course. AIEA students may skip practicing this (but understand the code) if they do not want to learn about app services.
  2. Following the post Getting Started with Python, install Python.
  3. Following the post Setting Up VS Code for Python Development, install VS Code, add the essential Python extensions, then create a project folder named azure-python-flask-demo, set up a virtual environment inside it, create a hello.py file as shown, run it, and also set recommended VS code settings. You can remove the hello.py file once you test it successfully as we will create a new file.

Hint: You may also create the folder outside VS Code, and to open VS Code with that folder selected, navigate into it from Terminal/PowerShell and run:

code .
  1. Following Getting Started with Azure CLI on Your Local Machine, install Azure CLI and connect to Azure.
  2. Following Setting Up VS Code for Azure Development, install essential extensions and connect to Azure.
  3. Optional: A resource group with necessary permissions to create a web app and a Linux app service plan. Note: You may also create this later from within the exercise steps while creating a web app. If you are a 4MCEA or AIEA student, you may have got one of these or both of these from your instructor.

Step 1: Create a Python Project

First, install flask in your virtual environment:

pip install flask

Next, create a new file called app.py inside your project folder and add the following code:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def hello():
    return jsonify(message="Hello from Python on Azure via Flask!")

if __name__ == "__main__":
    # For local development only
    app.run(host="0.0.0.0", port=5000, debug=True)

Create a file named requirements.txt in the same folder and add the following lines so Azure knows which packages to install:

flask

Run the app locally:

python app.py

Click Allow if a Windows Security popup comes up. Then, open your browser and go to http://127.0.0.1:5000. You should see the JSON response:

{"message": "Hello from Python on Azure via Flask!"}

Note: Flask is a lightweight web framework that’s perfect for simple APIs and web apps. We’ll explore Flask features like routing, templates, and blueprints in a later post.


Step 2: Create a Web App and Deploy App

  1. Assuming the Azure App Service extension (included in the Azure Tools pack) is installed, you can click on Azure icon from left side panel and from the Resource’s tab, right click on App services and click on Create New Web App… (Advanced).

Alternatively, open the Command Palette from search bar (Ctrl+Shift+P on Windows/Linux or Cmd+Shift+P on macOS) and search for Azure App Service: Create New Web App… (Advanced).

Note: We selected the Advanced option to manually choose the Resource Group and App Service Plan. If you select the option without Advanced (e.g. Create New Web App… after right clicking within the Resources or Azure App Service: Create New Web App… from command pallete), Azure will automatically create the RG and App Service plan for you. While the non-advanced approach is simpler, it’s not considered a best practice, as it limits control over resource selection and app service plan.

  1. If you selected the Advanced option,
    • Select region as East US,
    • For Select default hostname format, select Secure unique default hostname,
    • Select a resource group,
    • For the name of the web app, give a unique name like CoCanPyFlaskApp,
    • Runtime as Python 3.13,
    • The app service identified for this exercise
    • For application insights, select Skip for now.

Note: If you select the non-advanced option, you need to select region as East US, for the name of the web app, give a unique name like CoCanPyFlaskApp, Runtime as Python 3.13, and pricing tier as Free (F1) or Standard (S1).

  1. After the web app is created, from the list of resources in the left sidebar, right-click on the newly created web app, and choose Deploy to Web App.
  2. In a short while, your Python application will be live on Azure with its own public URL. VS Code will also show a popup message with a button to Browse Website. You can also right-click the app in the Azure panel and choose Browse Website to open it directly in your browser.

💡 Note: Deployment may take a little time (this can depend on your chosen pricing tier). During that time, your app’s URL might initially show the default Azure website. Once deployment is complete and the app has fully started, you’ll see your Flask API response instead.

Note 1: If you see an error like “Subscription is not registered to use Microsoft.OperationalInsights”, it means your subscription is missing a required resource provider. To fix this, go to your subscription in the Azure Portal → Resource providers → search for Microsoft.OperationalInsights → click Register. Alternatively, you can run:

az provider register --namespace Microsoft.OperationalInsights

Note 2: When you open the URL right after deployment, you’ll likely see the default Azure App Service welcome page saying “Hey, Python developers!” instead of your hello.py output.
This happens because Azure App Service expects a web framework (like Flask or FastAPI) or a configured startup command — not just a plain Python script. We’ll fix this in a later blog post by setting up a proper startup process so your actual Python app runs on Azure.


Wrap-Up

By combining Python setup and Azure setup in VS Code, you now have a complete environment to build, test, and deploy applications:

  • Write and debug Python code locally.
  • Deploy it as a web app or serverless function.
  • Connect it to Azure services like Storage or Databases.

This setup turns VS Code into a one-stop shop for Python cloud development. Whether you’re building a small script or a production-ready application, you can move from your laptop to the cloud in just a few clicks.

Previous articleExplaining the Python Program That Connects to Azure From Google Colab
Next articleUnderstanding a Simple Web Server Code Using Python (Flask)
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here