Today we're going to look at how to deploy applications to the web so that others can view them. The goal here is not to create a production-ready system. While we'll incorporate some libraries that are highly performant, we won't be following the absolute best practices for industry use so that we can instead make things a bit easier to prototype.
A number of cloud "platform as a service" tools exist for hosting web-based projects efficiently. In short, they allow you to upload web server code to a cloud backend which then does the hard part of making them web-accessible. While today we will make use of Heroku, there are many different providers. These include (in no particular order) Microsoft Azure, AWS, Google App Engine, and Digital Ocean. I encourage you to examine each and figure out which set of features and end-user agreement works best for you. For the purposes of this class, you should not need a paid account.
Our system will involve the following components:
Commands used in the lecture:
# Make a project directory mkdir <PROJECT NAME, e.g. jmr395-info4310-hw1> cd <PROJECT NAME> # Initialize an empty git repository (make sure Git is installed) git init # Initial login to Heroku (only do once, make sure Heroku is installed) heroku login # optional: run "heroku autocomplete" and add the command it echoes to your .bashrc # Make a new Heroku project heroku create <PROJECT NAME, e.g. jmr395-info4310-hw1> # See how Heroku added a new remote repository git remote # Make a Flask app (make sure Python 3, Flask, and WhiteNoise are installed) # Use a text editor of your choice. I use Atom, which can run from the command line atom app.py # See the demo files for what we added to app.py # At this point we also made a static directory and put some files in it for Whitenoise to serve # Test Flask server to see if it works (command may just be python for your machine) python3 app.py # Heroku needs a requirements file atom requirements.txt # It includes: # gunicorn - library that runs our code, must include # flask - imported in our .py # whitenoise - imported in our .py # Heroku needs a Procfile telling it how to start the app atom Procfile # It includes # web: gunicorn app:app - "Host a web site using gunicorn and running app.py # Commit the changes to your local git repository git status git add app.py git add requirements.txt # etc. to add all changed/added files # Now commit the changes git commit -m "first commit" # send all recent changes to the "master" branch the "heroku" remote repository git push remote heroku master # Heroku will now install the libraries and boot up # recommended command heroku ps:scale web=1 # Open the Heroku site in your web browser heroku open # For ease, we also connected our local repository to Github # go to Github, make a repo, copy the .git web address, push our local changes git remote add github https: ...blah... .git git push remote github master