Skip to content

Commit 525a7f9

Browse files
committed
Adding startup shim for App Service on Linux
1 parent 5cea79f commit 525a7f9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
This sample contains the completed program from the tutorial, [Using Flask in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-flask). Intermediate steps are not included.
22

3-
It also contains the Dockerfile and uwsgi.ini files necessary to build a container with a production server. The resulting image works both locally and when deployed to Azure App Service.
3+
It also contains the Dockerfile and uwsgi.ini files necessary to build a container with a production server. The resulting image works both locally and when deployed to Azure App Service. See [Deploy Python using Docker containers](https://code.visualstudio.com/docs/python/tutorial-deploy-containers).
4+
5+
The `startup.py` file, for its part, is specifically for deploying to Azure App Service on Linux without containers. Because the app code is in its own *module* in the `hello_app` folder (which has an `__init__.py`), trying to start the Gunicorn server within App Service on Linux produces an "Attempted relative import in non-package" error. The `startup.py` file, therefore, is just a shim to import the app object from the `hello_app` module, which then allows you to use startup:app in the Gunicorn command line (see `startup.txt`).
46

57
Contributions to the sample are welcome. When submitting changes, also consider submitting matching changes to the tutorial, the source file for which is [tutorial-flask.md](https://github.com/Microsoft/vscode-docs/blob/master/docs/python/tutorial-flask.md).
68

startup.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# In this sample, the Flask app object is contained within the hello_app *module*;
2+
# that is, hello_app contains an __init__.py along with relative imports. Because
3+
# of this structure, a file like webapp.py cannot be run directly as the startup
4+
# file through Gunicorn; the result is "Attempted relative import in non-package".
5+
#
6+
# The solution is to provide a simple alternate startup file, like this present
7+
# startup.py, that just imports the app object. You can then just specify
8+
# startup:app in the Gunicorn command.
9+
10+
from hello_app.webapp import app

startup.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gunicorn --bind=0.0.0.0 --workers=4 startup:app

0 commit comments

Comments
 (0)