Running Python Flask apps on Azure App Service
- Published on
- Reading time
- Authors
- Name
- Simon Waight
- Mastodon
- @simonwaight
I enjoyed speaking at the Sydney Python (SyPy) meetup on how Python Flask developers can quickly deploy their web applications onto Web Apps hosted on Linux on the Azure App Service platform.
But... why Python?!
Anyone taking a cursory glance through my writings will see that prior to last year I didn't write about Python. It's not that I wasn't interested, it's just that I wasn't spending any time working with the language. Throughout my career I've had the chance to work with all sorts of languages and I have to say that Python's been one on my list to use for a while. Clearly a lot of people love it and are making good careers out of it!
The role I currently have has given me more time to invest in learning new languages and you can see that I've started blogging more about Python, though I am clearly closer to a noob than an advanced user! My nirvana moment came when I realised that the Try Except construct looked very familiar to C#'s (but don't talk to me about tabs / spaces or colons...)
In future I'll be writing a bunch more about Python in the context of Azure Functions because I think serverless has a lot to offer the data science and machine learning communities - both of which are heavy users of Python.
Free Continuous Deployment (CD)
For this session I used App Service's inbuilt CD offering, Kudu, which can use webhook triggers on GitHub, Git and Bitbucket repositories as a mechanism to trigger a deployment. Kudu is open source so you can check out how it works on GitHub.
Kudu uses a pull-based CD approach where it will clone your repository locally and than do work based on the contents it finds (such as recognising a repository contains Python code, for example). Kudu is really handy when you don't have an existing CD process and want to rapidly configure hands-free deployments.
If you want to perform push-based CD, then you can use services like Azure DevOps, Bitbucket Pipelines or GitHub Actions to push your solution. I looked at using GitHub Actions in a previous blog post.
As a value add for those reading this far... the App Service team is also adding a new build engine along the lines of Kudu (which is named after an African antelope) called Oryx (also named after an African antelope) which is also open source and on GitHub.
Runtime Configuration
I glossed over this in the talk, but a powerful feature of App Services is Application Settings. These hold runtime configuration values, and in the Python world are passed into your running application as environment variables that are accessible using the familiar 'os.environ' array. You can publish these via the CLI or edit them directly in the Portal.
Presentation
My slides from this talk are below.
Useful Links
Rather than dump a bunch of links into the back of my slides I thought I would bring them here and give a bit more context to each.
- https://learn.microsoft.com/azure/app-service/containers/how-to-configure-python - read about the internals of how Python runs on App Service.
- https://github.com/Azure-App-Service/python - this is where the Dockerfiles that build the baseline Docker images used for Python Web Apps.
- https://learn.microsoft.com/azure/app-service/containers/tutorial-python-postgresql-app - don't like how Microsoft builds our Python containers, or have your own special sauce to include? Build and deploy your own Container image :)
- https://code.visualstudio.com/docs/python/tutorial-deploy-app-service-on-linux - a tutorial for end-to-end Python development in Visual Studio Code, including how you can deploy to App Service from there (hint: use Kudu from a SCM instead!!!)
- https://learn.microsoft.com/azure/app-service/containers/quickstart-python - the basic Flask demo used at the meetup - I cloned this for my use.
- https://github.com/Azure-Samples/app-service-msi-keyvault-python - how to use Managed Service Identities (MSIs) with Python to securely connect to other Azure resources.
- https://github.com/AzureAD/azure-activedirectory-library-for-python - this is the main repository for the Azure Active Directory Authentication Library (ADAL) for Python. This is the easiest way to get started integrating your Python application authentication into Azure AD - the 'sample' folder contains all you need.
- https://medium.com/microsoft-open-source-stories/python-at-microsoft-flying-under-the-radar-eabbdebe4fb0 - read about the history of Python at Microsoft!
Feel free to reach out on Twitter @simonwaight or drop me an email (I bet you can figure it out @microsoft.com).
Happy Days 😎