none
[Python][Web Apps] Use Azure App Settings and local settings when building WebApp RRS feed

  • Question

  • So I´m trying out Python and Flask and would like to know how do I achieve deploying my app to Azure and Locally with different settings. I tried lurking around documentation and didn´t succeed. Could you point me to a source which tells how to use local settings for debugging and App Settings for production build in Azure? (In conjunction with Python, preferably). Thank you!

    Oh, forgot the main idea, that connectionstring should not be exposed on git.


    The data above this text is pseudorandom, brace yourselves.




    Friday, March 18, 2016 11:19 AM

Answers

  • I've come up with the minimal amount of code required to use Flask on Azure.

    Its a very basic site on GitHub here: https://github.com/timmyreilly/azure-app-settings-example

    And the way I manage tokens/keys/connection strings is demonstrated in my views.py file here:

    https://github.com/timmyreilly/azure-app-settings-example

    Then in the azure portal in my WebApp > Settings > Application Settings > App Settings > Key in this case is "BING_TOKEN" Value is "W3482398yuoihsdFNoNSENSE"

    Then I just add my tokens file to my .gitignore

    • Marked as answer by 4c74356b41MVP Monday, March 21, 2016 8:03 AM
    Friday, March 18, 2016 6:47 PM
  • Hi,

    We are diving into your issue. About your issue, @TimmyReilly's answer is correct. We can leverage App Settings of Azure Web Apps, and use os.getenv('SOME_SETTING') in python scripts to invoke this it.

    And about the deployment issue, currently, it should works fine without any problem to use Visual Studio with PTVS tools for deployment Flask apps.

    Thanks for your patience.

    • Marked as answer by 4c74356b41MVP Monday, March 21, 2016 8:03 AM
    Monday, March 21, 2016 7:49 AM
    Moderator

All replies

  • Not Python specific, but generally, the way to deal with these issues is to use the App Settings in the Azure portal. Any App Setting that you set becomes an environment variable at runtime. This makes it easy to keep secrets out of your file and instead rely on the runtime environment for them.

    David

    • Proposed as answer by David Ebbo Friday, March 18, 2016 3:56 PM
    Friday, March 18, 2016 3:56 PM
  • Yeah, that's pretty obvious, but how do I set those values in my app? I can't seem to figure it out, could you point me to some docs or examples please?

    The data above this text is pseudorandom, brace yourselves.

    Friday, March 18, 2016 4:46 PM
  • I've come up with the minimal amount of code required to use Flask on Azure.

    Its a very basic site on GitHub here: https://github.com/timmyreilly/azure-app-settings-example

    And the way I manage tokens/keys/connection strings is demonstrated in my views.py file here:

    https://github.com/timmyreilly/azure-app-settings-example

    Then in the azure portal in my WebApp > Settings > Application Settings > App Settings > Key in this case is "BING_TOKEN" Value is "W3482398yuoihsdFNoNSENSE"

    Then I just add my tokens file to my .gitignore

    • Marked as answer by 4c74356b41MVP Monday, March 21, 2016 8:03 AM
    Friday, March 18, 2016 6:47 PM
  • Hello, Timmy, I've tried following instructions on your blog, and well... http://testoveart1.azurewebsites.net/ - you can check the result here. I've done everything upto "git push azure master" and it deploys the site... after that site displays http 500 ;)

    PS C:\github\python> git init
    Initialized empty Git repository in C:/github/python/.git/
    PS C:\github\python> git remote add origin https://github.com/timmyreilly/azure-app-settings-example.git
    PS C:\github\python> git pull origin master
    remote: Counting objects: 48, done.
    remote: Compressing objects: 100% (25/25), done.
    remote: Total 48 (delta 15), reused 48 (delta 15), pack-reused 0
    Unpacking objects: 100% (48/48), done.
    From https://github.com/timmyreilly/azure-app-settings-example
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> origin/master
    PS C:\github\python> git remote add azure https://blog-ftp@testoveart1.scm.azurewebsites.net:443/testoveart1.git
    PS C:\github\python> git push azure master
    Password for 'https://blog-ftp@testoveart1.scm.azurewebsites.net:443':
    Counting objects: 48, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (40/40), done.
    Writing objects: 100% (48/48), 9.52 KiB | 0 bytes/s, done.
    Total 48 (delta 15), reused 0 (delta 0)
    remote: Updating branch 'master'.
    remote: Updating submodules.
    remote: Preparing deployment for commit id '49a5d32e52'.
    remote: Running custom deployment command...
    remote: Running deployment command...
    remote: Handling python deployment.
    remote: KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
    remote: Deleting file: 'hostingstart.html'
    remote: Copying file: '.gitignore'
    remote: Copying file: 'ptvs_virtualenv_proxy.py'
    remote: Copying file: 'readme.md'
    remote: Copying file: 'requirements.txt'
    remote: Copying file: 'runserver.py'
    remote: Copying file: 'web.2.7.config'
    remote: Copying file: 'web.debug.config'
    remote: Copying file: 'FlaskWebProject\views.py'
    remote: Copying file: 'FlaskWebProject\__init__.py'
    remote: Copying file: 'FlaskWebProject\static\content\site.css'
    remote: Copying file: 'FlaskWebProject\templates\index.html'
    remote: Copying file: 'FlaskWebProject\templates\map.html'
    remote: Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
    remote: Detecting Python runtime from site configuration
    remote: Detected python-2.7
    remote: Creating python-2.7 virtual environment.
    remote: ..................
    remote: New python executable in env\Scripts\python.exe
    remote: ..........................................................
    remote: Installing setuptools, pip...done.
    remote: Pip install requirements.
    remote: .
    remote: Downloading/unpacking Flask==0.10.1 (from -r requirements.txt (line 1))
    remote: .
    remote:   Running setup.py (path:D:\home\site\wwwroot\env\build\Flask\setup.py) egg_info for package Flask
    remote:
    remote:     warning: no files found matching '*' under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'docs'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'docs'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'examples'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'examples'
    remote:     no previously-included directories found matching 'docs\_build'
    remote:     no previously-included directories found matching 'docs\_themes\.git'
    remote: Downloading/unpacking Werkzeug>=0.7 (from Flask==0.10.1->-r requirements.txt (line 1))
    remote: Downloading/unpacking Jinja2>=2.4 (from Flask==0.10.1->-r requirements.txt (line 1))
    remote: Downloading/unpacking itsdangerous>=0.21 (from Flask==0.10.1->-r requirements.txt (line 1))
    remote:   Running setup.py (path:D:\home\site\wwwroot\env\build\itsdangerous\setup.py) egg_info for package itsdangerous
    remote:
    remote:     warning: no previously-included files matching '*' found under directory 'docs\_build'
    remote: Downloading/unpacking MarkupSafe (from Jinja2>=2.4->Flask==0.10.1->-r requirements.txt (line 1))
    remote:   Downloading MarkupSafe-0.23.tar.gz
    remote:   Running setup.py (path:D:\home\site\wwwroot\env\build\MarkupSafe\setup.py) egg_info for package MarkupSafe
    remote:
    remote: Installing collected packages: Flask, Werkzeug, Jinja2, itsdangerous, MarkupSafe
    remote:   Running setup.py install for Flask
    remote:
    remote:     warning: no files found matching '*' under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'docs'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'docs'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'tests'
    remote:     warning: no previously-included files matching '*.pyc' found under directory 'examples'
    remote:     warning: no previously-included files matching '*.pyo' found under directory 'examples'
    remote:     no previously-included directories found matching 'docs\_build'
    remote:     no previously-included directories found matching 'docs\_themes\.git'
    remote: .............
    remote:   Running setup.py install for itsdangerous
    remote:
    remote:     warning: no previously-included files matching '*' found under directory 'docs\_build'
    remote:   Running setup.py install for MarkupSafe
    remote:
    remote:     building 'markupsafe._speedups' extension
    remote:     ==========================================================================
    remote:     WARNING: The C extension could not be compiled, speedups are not enabled.
    remote:     Failure information, if any, is above.
    remote:     Retrying the build without the C extension now.
    remote:
    remote:
    remote:     ==========================================================================
    remote:     WARNING: The C extension could not be compiled, speedups are not enabled.
    remote:     Plain-Python installation succeeded.
    remote:     ==========================================================================
    remote: Successfully installed Flask Werkzeug Jinja2 itsdangerous MarkupSafe
    remote: Cleaning up...
    remote: ...............
    remote: Overwriting web.config with web.2.7.config
    remote:         1 file(s) copied.
    remote: Finished successfully.
    remote: Running post deployment command(s)...
    remote: Deployment successful.
    To https://blog-ftp@testoveart1.scm.azurewebsites.net:443/testoveart1.git
     * [new branch]      master -> master


    The data above this text is pseudorandom, brace yourselves.


    Saturday, March 19, 2016 7:37 PM
  • So at this point I'm about to give up. I've tried creating Flask app in Azure. It deploys and work. After that I create the same app in Visual Studio (well, I guess they are the same, as they look the same when created). Uploaded that Visual studio project to GitHub, deployed to website. 500. so I guess something is broken guys. Well at least I can't figure out for 2 days already how to do that stuff properly. And you know, I'm pretty bad at doing things, but not that bad.

    The data above this text is pseudorandom, brace yourselves.

    Sunday, March 20, 2016 3:17 PM
  • Hi,

    We are diving into your issue. About your issue, @TimmyReilly's answer is correct. We can leverage App Settings of Azure Web Apps, and use os.getenv('SOME_SETTING') in python scripts to invoke this it.

    And about the deployment issue, currently, it should works fine without any problem to use Visual Studio with PTVS tools for deployment Flask apps.

    Thanks for your patience.

    • Marked as answer by 4c74356b41MVP Monday, March 21, 2016 8:03 AM
    Monday, March 21, 2016 7:49 AM
    Moderator
  • The os.getenv works fine, thank you (both of you).

    So lets close this thread and move to another one, which I started that is about deployment issues, ok? https://social.msdn.microsoft.com/Forums/azure/en-US/f628a12b-79ae-4558-a930-410398fc923d/python-pythonflask-app-doesnt-work-when-deployed-from-github?forum=windowsazurewebsitespreview#b5931fe6-4ec1-44c8-bf5c-5e71fb6ed574


    The data above this text is pseudorandom, brace yourselves.


    Monday, March 21, 2016 8:03 AM