Python Web App

About

Notes

Overview

  • With ML applications, the model, scaler, and any other applicable components run in the Python-based backend.

Process

Flask

  • Data input through API requests using Postman which relays JSON data to the flask app and Python's request library.
  • Flask app connects to HTML web form.

Components

  • App
    • HTML form using Flask linked to home.html
    • Form data input into Flask.
    • Flask returns data generated by function through function.html.
  • Templates
    • home.html
      • Flask-based form for accepting user input.
      • Relays user input to Flask app.
    • function.html
      • Returns data generated by funcion.

Heroku Deployment

  • In an Anaconda Prompt, navigate to the location of the files to be deployed.
  • Create a venv with intended Python version:
  • conda create --name venvcustomname python=3.7
  • Activate new venv and install relevant libraries for application.
    • Use pip, not conda, so that a pip freeze requirements file can be generated.
    • Libraries
      • flask
      • Flask-WTF
      • scikit-learn
        • This also installs joblib and numpy.
      • tensorflow
      • gunicorn
        • Python web server gateway interface.
        • Used to push the flask app to Heroku server.
  • Generate pip freeze requirements file to log the installed libraries.
  • pip freeze > requirements.txt
  • Create process file required by Heroku.
    • Label the file Procfile with no file extension.
    • Include the following (note the name of the app file reference):
    • web: gunicorn app:app
  • On Heroku Dashboard, Create new app.
    • Choose unique App name. (my-tf-flower-app)
    • Choose Deployment method, namely Heroku Git and follow instructions.
  • Push rejected notifications
  • Heroku attempts to compress and build source.
    • Successful deployment results in message of
    • Launching...
      https://<appname>.herokuapp.com/ deployed to Heroku
      Verifying deploy... done.
  • Deactivate venv in prompt.

Local Testing

Postman

  • Use Postman to send a POST request to the API in JSON format.

Troubleshooting

TF on Heroku