Skip to content

Commit 8161565

Browse files
authored
Merge pull request #279683 from JimacoMS4/add-fastapi-to-app-service-python-quickstart
Add FastAPI to App Service Python Quickstart
2 parents 531151f + af9fe28 commit 8161565

10 files changed

+146
-10
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,12 @@
12251225
"url": "https://github.com/MicrosoftDocs/SupportArticles-docs",
12261226
"branch": "main",
12271227
"branch_mapping": {}
1228+
},
1229+
{
1230+
"path_to_root": "msdocs-python-fastapi-webapp-quickstart",
1231+
"url": "https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart",
1232+
"branch": "main",
1233+
"branch_mapping": {}
12281234
}
12291235
],
12301236
"branch_target_mapping": {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
author: JimacoMS4
3+
ms.author: v-jibrannian
4+
ms.topic: include
5+
ms.date: 07/02/2024
6+
---
7+
First, configure the startup command in Azure App Service. Navigate to the page for the App Service instance in the Azure portal.<br>
8+
<br>
9+
1. Select **Configuration** under the **Settings** heading in the menu on the left side of the page.
10+
1. Make sure the **General settings** tab is selected.
11+
1. In the **Startup Command** field, enter *gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app*.
12+
1. Select **Save** to save your changes.
13+
1. Wait for the notification that the settings are updated before proceeding.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
author: JimacoMS4
3+
ms.author: v-jibrannian
4+
ms.topic: include
5+
ms.date: 07/02/2024
6+
---
7+
Next, restart the web app.<br>
8+
<br>
9+
1. Select **Overview** in the menu on the left side of the page.
10+
1. On the top menu, select **Restart**.

articles/app-service/includes/quickstart-python/deploy-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ ms.author: charris
44
ms.topic: include
55
ms.date: 04/01/2022
66
---
7-
Since the `az webapp up` command created the necessary resources and deployed your application in a single step, you can move on to [4 - Browse to the app](#4---browse-to-the-app).
7+
Since the `az webapp up` command created the necessary resources and deployed your application in a single step, you can move on to the next step.
88

99
---

articles/app-service/includes/quickstart-python/deploy-visual-studio-code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ ms.author: charris
44
ms.topic: include
55
ms.date: 04/01/2022
66
---
7-
Since the previous step created the necessary resources and deployed your application in a single step, you can move on to [4 - Browse to the app](#4---browse-to-the-app).
7+
Since the previous step created the necessary resources and deployed your application in a single step, you can move on to the next step.
88

99
---
12 KB
Loading
63.4 KB
Loading
7.13 KB
Loading
26.4 KB
Loading

articles/app-service/quickstart-python.md

Lines changed: 115 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: 'Quickstart: Deploy a Python (Django or Flask) web app to Azure'
2+
title: 'Quickstart: Deploy a Python (Django, Flask, or FastAPI) web app to Azure'
33
description: Get started with Azure App Service by deploying your first Python app to Azure App Service.
44
ms.topic: quickstart
5-
ms.date: 07/26/2023
5+
ms.date: 06/28/2024
66
ms.author: msangapu
77
author: msangapu-msft
88
ms.devlang: python
@@ -13,7 +13,7 @@ ms.custom: devx-azure-cli, devx-azure-portal, devx-vscode-azure-extension, devdi
1313

1414
[!INCLUDE [regionalization-note](./includes/regionalization-note.md)]
1515

16-
In this quickstart, you'll deploy a Python web app (Django or Flask) to [Azure App Service](./overview.md#app-service-on-linux). Azure App Service is a fully managed web hosting service that supports Python apps hosted in a Linux server environment.
16+
In this quickstart, you deploy a Python web app (Django, Flask, or FastAPI) to [Azure App Service](./overview.md#app-service-on-linux). Azure App Service is a fully managed web hosting service that supports Python apps hosted in a Linux server environment.
1717

1818
To complete this quickstart, you need:
1919

@@ -25,7 +25,7 @@ To complete this quickstart, you need:
2525
2626
## 1 - Sample application
2727

28-
This quickstart can be completed using either Flask or Django. A sample application in each framework is provided to help you follow along with this quickstart. Download or clone the sample application to your local workstation.
28+
This quickstart can be completed using either Flask, Django, or FastAPI. A sample application in each framework is provided to help you follow along with this quickstart. Download or clone the sample application to your local workstation.
2929

3030
### [Flask](#tab/flask)
3131

@@ -39,6 +39,12 @@ git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart
3939
git clone https://github.com/Azure-Samples/msdocs-python-django-webapp-quickstart
4040
```
4141

42+
### [FastAPI](#tab/fastapi)
43+
44+
```Console
45+
git clone https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart.git
46+
```
47+
4248
---
4349

4450
To run the application locally:
@@ -103,6 +109,36 @@ Having issues? [Let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
103109

104110
Having issues? [Let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
105111

112+
### [FastAPI](#tab/fastapi)
113+
114+
1. Go to the application folder:
115+
116+
```Console
117+
cd msdocs-python-fastapi-webapp-quickstart
118+
```
119+
120+
1. Create a virtual environment for the app:
121+
122+
[!INCLUDE [Virtual environment setup](./includes/quickstart-python/virtual-environment-setup.md)]
123+
124+
1. Install the dependencies:
125+
126+
```Console
127+
pip install -r requirements.txt
128+
```
129+
130+
1. Run the app:
131+
132+
```Console
133+
uvicorn main:app --reload
134+
```
135+
136+
1. Browse to the sample application at `http://localhost:8000` in a web browser.
137+
138+
:::image type="content" source="./media/quickstart-python/run-django-app-localhost.png" alt-text="Screenshot of the FastAPI app running locally in a browser.":::
139+
140+
Having issues? [Let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
141+
106142
---
107143

108144
## 2 - Create a web app in Azure
@@ -121,6 +157,7 @@ To create Azure resources in VS Code, you must have the [Azure Tools extension p
121157
> [Download Azure Tools extension pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-node-azure-pack)
122158

123159
In the application folder, open VS Code:
160+
124161
```Console
125162
code .
126163
```
@@ -178,7 +215,73 @@ Azure App service supports multiple methods to deploy your application code to A
178215

179216
Having issues? Refer first to the [Troubleshooting guide](./configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
180217

181-
## 4 - Browse to the app
218+
## 4 - Configure startup script
219+
220+
Based on the presence of certain files in a deployment, App Service automatically detects whether an app is a Django or Flask app and performs default steps to run your app. For apps based on other web frameworks like FastAPI, you need to configure a startup script for App Service to run your app; otherwise, App Service runs a default read-only app located in the *opt/defaultsite* folder.
221+
222+
To learn more about how App Service runs Python apps and how you can configure and customize its behavior with your app, see [Configure a Linux Python app for Azure App Service](configure-language-python.md).
223+
224+
### [Azure CLI](#tab/azure-cli/flask)
225+
226+
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
227+
228+
### [Azure CLI](#tab/azure-cli/django)
229+
230+
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
231+
232+
### [Azure CLI](#tab/azure-cli/fastapi)
233+
234+
For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: `gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app`.
235+
236+
First, configure the startup command using the [az webapp config set](/cli/azure/webapp/config#az-webapp-config-set) command.
237+
238+
```azurecli
239+
az webapp config set \
240+
--startup-file "gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app" \
241+
--name $APP_SERVICE_NAME \
242+
--resource-group $RESOURCE_GROUP_NAME
243+
```
244+
245+
Next, restart the web app using the [az webapp restart](/cli/azure/webapp#az-webapp-restart) command.
246+
247+
```azurecli
248+
az webapp restart \
249+
--name $APP_SERVICE_NAME \
250+
--resource-group $RESOURCE_GROUP_NAME
251+
```
252+
253+
### [VS Code](#tab/vscode-aztools/flask)
254+
255+
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
256+
257+
### [VS Code](#tab/vscode-aztools/django)
258+
259+
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
260+
261+
### [VS Code](#tab/vscode-aztools/fastapi)
262+
263+
Use Azure CLI or the Azure portal to configure the startup command.
264+
265+
### [Azure portal](#tab/azure-portal/flask)
266+
267+
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
268+
269+
### [Azure portal](#tab/azure-portal/django)
270+
271+
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
272+
273+
### [Azure portal](#tab/azure-portal/fastapi)
274+
275+
For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: `gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app`.
276+
277+
| Instructions | Screenshot |
278+
|:----------------|-----------:|
279+
| [!INCLUDE [Configure startup command from Azure portal 1](./includes/quickstart-python/configure-startup-azure-portal-1.md)] | :::image type="content" source="./media/quickstart-python/configure-startup-azure-portal-1-240px.png" alt-text="A screenshot of the location in the Azure portal where to configure the startup command." lightbox="./media/quickstart-python/configure-startup-azure-portal-1.png"::: |
280+
| [!INCLUDE [Configure startup command from Azure portal 2](./includes/quickstart-python/configure-startup-azure-portal-2.md)] | :::image type="content" source="./media/quickstart-python/configure-startup-azure-portal-2-240px.png" alt-text="A screenshot of how to reset the web app in the Azure portal." lightbox="./media/quickstart-python/configure-startup-azure-portal-2.png"::: |
281+
282+
---
283+
284+
## 5 - Browse to the app
182285

183286
Browse to the deployed application in your web browser at the URL `http://<app-name>.azurewebsites.net`. If you see a default app page, wait a minute and refresh the browser.
184287

@@ -190,18 +293,22 @@ The Python sample code is running a Linux container in App Service using a built
190293

191294
Having issues? Refer first to the [Troubleshooting guide](./configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
192295

193-
## 5 - Stream logs
296+
## 6 - Stream logs
194297

195298
Azure App Service captures all messages output to the console to assist you in diagnosing issues with your application. The sample apps include `print()` statements to demonstrate this capability.
196299

197300
### [Flask](#tab/flask)
198301

199-
:::code language="python" source="~/msdocs-python-flask-webapp-quickstart/app.py" range="9-29" highlight="11,24,27":::
302+
:::code language="python" source="~/msdocs-python-flask-webapp-quickstart/app.py" range="9-29" highlight="3,16,19":::
200303

201304
### [Django](#tab/django)
202305

203306
:::code language="python" source="~/msdocs-python-django-webapp-quickstart/hello_azure/views.py" range="5-21" highlight="2,11,14":::
204307

308+
### [FastAPI](#tab/fastapi)
309+
310+
:::code language="python" source="~/msdocs-python-fastapi-webapp-quickstart/main.py" range="12-30" highlight="3,15,18":::
311+
205312
---
206313

207314
The contents of the App Service diagnostic logs can be reviewed using the Azure CLI, VS Code, or Azure portal.
@@ -291,7 +398,7 @@ Having issues? [Let us know](https://aka.ms/PythonAppServiceQuickstartFeedback).
291398
## Next steps
292399

293400
> [!div class="nextstepaction"]
294-
> [Tutorial: Python (Django) web app with PostgreSQL](./tutorial-python-postgresql-app.md)
401+
> [Tutorial: Python (Django or Flask) web app with PostgreSQL](./tutorial-python-postgresql-app.md)
295402
296403
> [!div class="nextstepaction"]
297404
> [Configure Python app](./configure-language-python.md)

0 commit comments

Comments
 (0)