Windows Azure websites helps you create highly scalable websites with fast deployment to the Windows Azure . Since Python stack is available on Windows Azure websites , I have been excited to build Python applications on Windows Azure Websites .
Flask is popular python framework based on Werkzeug, Jinja 2 . In this tutorial I will show you how to create a Hello world Flask application on Windows Azure .
Create a folder for your application if you haven’t already , say ‘myflasksite‘ and include the source for the following libraries :
Extract the TAR packages for the above libraries to your application folder . Here is how the directory structure would look like
myflasksite \ +-flask (folder name must be 'flask' and this is case-sensitive) \ |-- folders and files of flask library +--jinja2(folder name must be 'jinja' and this is case-sensitive) \ |-- folders and files of jinja2 library +--werkzeug (folder name must be 'werkzeug' and this is case-sensitive) \ |-- folders and files of werkzeug library +--myapplication (this is where your application content will live . You can change the name of this folder to the name of your application) \ |--application content
In ‘myapplication’ folder create a __init__.py file and enter this code in the file which will serve a “Hello World” message when the site is browsed . For more on Flask check out the documentation here
from flask import Flask
app = Flask(__name__)
return “Hello World!”
if __name__ == “__main__”:
Now all we need to do is setup an Azure website and configure it to run the Flask application . Once you are logged into the Windows Azure Management portal , you will need to create a new site with “Quick create” as shown below and enter the site name , say “helloworldflask”
Once the site is up and running , access the Dashboard of the site and click on ‘ Configure’ tab . To run Flask application , you need to :
- Create app settings for your flask application
- You need to set the Python Path for the application in web.config . For Azure websites, use ‘D: \home\site\wwwroot’ as this will point to the site root where the any frameworks needed by your application will reside
2. You need to set the WSGI_HANDLER for the application in web.config . It’s a module/package name, followed by the attribute in the module to be used; for example mypackage.mymodule.handler. Add parentheses to indicate that the attribute should be called.
Note that WSGI_HANDLER is referencing the name of your application folder , in this case it is ‘myapplication
- Create a handler to handle requests
EXTENSION * SCRIPT PROCESSOR PATH D:\python27\python.exe ADDITIONAL ARGUMENTS D:\python27\scripts\wfastcgi.py
Save the configuration and now it’s time to copy the content of ‘myflasksite’ to the newly created site ‘helloflask’ .
Now your site is configured for using Flask . You can use FTP or GIT to deploy your Flask Application to this newly created Azure Website , for this tutorial I will show how to use GIT . Here are the steps :
- Install GIT on your local machine , if you haven’t already
- Setup GIT deployment on Azure Website :
Click on “Setup deployment from source” your helloworldflask website Dashboard
- Select Local GIT repository and continue . This will set up a GIT deployment for your website
- You will now see a Deployments Tab for your site with a list of instructions on how to deploy your local site content to the azure website
- Open Git BASH command Line and move to your application folder (‘myflasksite’ folder )
- At the command prompt, and then type this command:
git init git add . git commit -m "initial commit"
- At the command prompt, change to the root directory for your app, and then type this command:
git remote add azure https://firstname.lastname@example.org/helloworldflask.git git push azure master
Once the content is deployed successfully , browse the site
That’s it . Your Flask application is up and running on Azure website .