PythonAnywhere Flask Project

Overview

How to deploy a small python flash application on PythonAnywhere

Project on Localhost

The project directory name is airtable_gallery, under a parent called titan. I will preserve these names for convenience. It is handy to have a parent direcory for storing notes, debug dumps, backups that don't go into the git repository. A virtual environment tool is used (mkvirtualenv)

    ~/titan/
    ~/titan/airtable_gallery/.git
    ~/titan/airtable_gallery/requirements.txt
    ~/titan/airtable_gallery/app.log
    ~/titan/airtable_gallery/*.pickle
    ~/titan/airtable_gallery/app/app.py
    ~/titan/airtable_gallery/app/*.py
    ~/titan/airtable_gallery/app/templates/
    ~/titan/airtable_gallery/app/assets/

Localhost Steps

  • Final testing and cleanup.

  • Save the virtual environment

    pip freeze > requirements.txt
  • Push to a new empty repo (no README) called 'airtable_gallery' at Bitbucket.com.
    git remote add origin https://[email protected]/tropis/airtable_gallery.git
    git push -u origin master

PythonAnywhere Steps

  • My account is gobot. Choose domain gobot.pythonanywhere.com for now.

  • Choose Manual Configuration, not Flask because my own directory design.

  • Choose Python 3.6

Web Tab Settings

Source code: /home/gobot/titan/airtable_gallery
Working dir: /home/gobot/titan/airtable_gallery

Path to Virtual env:    /home/gobot/.virtualenvs/airtable_gallery
Static files: /assets/  /home/gobot/titan/airtable_gallery/app/assets

In Bash console

git config --global user.name "Phil Hardaker"
git config --global user.email [email protected]

# Create base directory only.

mkdir  ~/titan
cd ~/titan

# Pull my repo, brings .git too
git clone https://[email protected]/tropis/airtable_gallery.git

mkvirtualenv --python=/usr/bin/python3.6 airtable_gallery
# Now paths set to virtualenv, see indicator (airtable_gallery)

# Install project libs
cd ~/titan/airtable_gallery
pip install -r requirements.txt

WSGI file

File location:

  ls /var/www/gobot_pythonanywhere_com_wsgi.py

But just use the Edit link on Web tab. We will include the secret AIRTABLE_API_KEY to be available in the environment.

    import sys
    import os
    os.environ["AIRTABLE_API_KEY"] = "key11111"

    path = '/home/gobot/titan/airtable_gallery/app'
    if path not in sys.path:
        sys.path.append(path)

    from app import app as application

Web Tab

Click "Reload" after any change to redeploy.

http://gobot.pythonanywhere.com