2: Prepare your app for deployment to Azure App Service

Previous step: configure your environment

In this article, you prepare an app to deploy to the Azure App Service for this tutorial. You can use an existing app or create or download an app.

If you already have an app

If you already have an app that you'd like to work with, make sure you have a requirements.txt file in your project root that lists your dependencies, including frameworks like Flask or Django. You can use any framework of your choosing.

If you don't already have an app

If you don't already have an app, use one of the options below. Be sure to verify that the app runs locally.

The remainder of this tutorial uses the code shown in Option 3.

Option 1: Use the VS Code Flask tutorial sample

Download or clone https://github.com/Microsoft/python-sample-vscode-flask-tutorial, which is the result of following the Flask Tutorial. Note that the app code is in the hello_app folder, specifically. Review the sample's readme.md file for instructions on running the app locally.

Option 2: Use the VS Code Django tutorial sample

Download or clone https://github.com/Microsoft/python-sample-vscode-django-tutorial, which is the result of following the Django Tutorial.

Ideally, Django apps deployed to the cloud also use a cloud-based database, such as PostgreSQL for Azure. For more information, see Tutorial: Deploy a Django web app with PostgreSQL using the Azure portal.

If your Django app uses a local SQLite database like this sample, it's easiest for this tutorial to include a pre-initialized and pre-populated copy of the db.sqlite3 file in your repository. Otherwise, you need to configure a post-build command to run Django's migrate command in the container to which the app is deployed. For more information, see App Service configuration - Customize build automation.

Option 3: Create a minimal Flask app

This section describes the minimal Flask app used in this walkthrough. The code is available on GitHub - python-docs-flask-minimal.

  1. Create a new folder, open it in VS Code, and add a file named hello.py with the contents below. The app object is purposely named myapp to demonstrate how the names are used in the startup command for the App Service, as you learn later.

    from flask import Flask
    myapp = Flask(__name__)
    
    @myapp.route("/")
    def hello():
        return "Hello Flask, on Azure App Service for Linux"
    
  2. In the same folder, create a file named requirements.txt with the following contents:

    Flask
    
  3. Open a terminal using the menu command Terminal > New Terminal.

  4. In the terminal, navigate to the folder containing hello.py. All the remaining terminal commands are run in this folder.

  5. Create and activate a virtual environment named .venv:

    :: Assumes Windows
    py -3 -m venv .venv
    .venv\scripts\activate
    
  6. When VS Code prompts you to activate the newly-created environment, answer Yes.

  7. Install the app's dependencies:

    pip install -r requirements.txt
    
  8. Set a FLASK_APP environment variable tells Flask where to find the app object:

    set FLASK_APP=hello:myapp
    
  9. Run the app:

    flask run
    
  10. Open the app in a browser using the URL http://127.0.0.1:5000/. You should see the message "Hello Flask, on Azure App Service for Linux."

  11. Stop the Flask server by pressing Ctrl+C in the terminal.

Having issues? Let us know.