Build your Flask application on Windows Azure Websites

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__)

@app.route(“/”)
def hello():
return “Hello World!”

if __name__ == “__main__”:
app.run()

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

helloflask

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
    1. 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

PYTHONPATH                D:\home\site\wwwroot

       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.

WSGI_HANDLER              myapplication.app

PY12

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

py34

Save the configuration and now it’s time to copy the content of ‘myflasksite’ to the newly created site ‘helloflask’ .

py23

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

py45

  • Select Local GIT repository and continue . This will set up a GIT deployment for your website

py55

  • 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

PY66

  • 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://flaskuser@helloworldflask.scm.azurewebsites.net/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 .

Advertisements

6 thoughts on “Build your Flask application on Windows Azure Websites

  1. Thanks for this! I’ve followed your steps but still getting a 500 error. The logs show “ErrorDescription An unknown FastCGI error occurred” when trying to navigate to ‘/’: Any ideas?

  2. In the configure page on windowsazure.com ,setup a new “application setting” for getting python logs

    WSGI_LOG D:\home\site\wwwroot\log.txt

    This will give you the actual error why it this failing in log.txt . I recommend to install Flask from the “Gallery” in Windows Azure websites . The Gallery has a basic template for Flask application that can be modified to your needs .

    1. Thanks! Will give it a try. One thing I noticed is that the template (and other blogs) have a web.config file that seems to duplicate some of the config settings in the portal…with the exception of this guy:

      Any idea how this works?

  3. Flask template was updated in September with the fix for the query string issue . Now the web.config has it set to appendQueryString=”true” . Did you see the value being set to false in web.config ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s