There are a multitude of ways to deploy your app to the web. I am going to focus on two ways here:
Heroku is a very popular platform for web app deployment. It is simple to setup, and can continuously update the web app with every commit to a version control service (e.g. GitHub).
Advantages of Heroku:
- Easy to setup; no need to fiddle with web server stuff.
- Free
- URL is http://
mydomain
.herokuapp.com/, which is not bad for branding. It's not tied to your username like PythonAnywhere.
Disadvantages of Heroku:
- No persistent disk storage, i.e. cannot store databases unless it's part of
git
repo (bad, bad, bad idea). - Free tier goes to sleep after 30 min. of activity; first load can take some time.
This project's "display table" can be found online here.
This outline assumes that you have your project already hosted on GitHub, and that everything goes smoothly.
- Sign up for an account at Heroku.
- Install the Heroku CLI, and then login.
cd
into your project directory.- Make sure that you have a
requirements.txt
file, which specifies the versions of Python packages that you are using.- You can use
pipreqs
, a Python package for automatically determining your Python package requirements, to create therequirements.txt
file.
- You can use
- Be sure to add
gunicorn
torequirements.txt
. - Run the command
heroku create "myprojectname"
, to create a new Heroku project based off your current project directory, with the project name being "myprojectname
". - Confirm that there is a
heroku
remote by running the command$ git remote -v
. - Create your procfile. For more information, click the link to read more. If you only need a refresher, read the Procfile section below.
- Create a
runtime.txt
file, which specifies the Python version for Heroku to use. - Run
git push heroku master
to deploy your app to the web!
You can do other configurations on your web dashboard.
- Open your project.
- Click on the
Deploy
tab. - Look for "Deployment method". Click on "GitHub".
- If you haven't already connected GitHub to your Heroku account, do so now.
- The bottom of the page will change to "Connect to GitHub". Type in the name of your repository name and click "Search".
- When your repository name shows up, click "Connect" next to it.
- The bottom of the page will now show, "Automatic deploys". Make sure the
master
branch is selected, and that you have clicked on "Enable Automatic Deploys".
Now, each time you git push to GitHub, Heroku will automatically pull in your GitHub repository and deploy it. No need to type a separate git push heroku master
command anymore - laziness at its best!
Here's what the Procfile looks like for this project.
web: gunicorn app:app
This tells Heroku to tell gunicorn
to execute my app.py
file.
PythonAnywhere is another service that is great for Python web app deployment. Advantages of PythonAnywhere:
- Free
- Provides (limited) storage, so it's useful if your app needs to retrieve a database.
- Provides a console through which you can
git clone
andgit pull
updates to your app.
Disadvantages of PythonAnywhere:
- Not as many convenience functions provided as Heroku.
- Project URL is https://
myusername
.pythonanywhere.com. Not as good for branding.
This project can be found at: http://ericmjl.pythonanywhere.com/
This outline is briefer, but it provides the set of instruction pages to follow in order.
- Make sure your Python app is deployed to a version control system, i.e. GitHub.
- Open a new
bash
console in PythonAnywhere. git clone
your repository using HTTPS (not SSH). For example, this project wouuld be cloned using$ git clone https://github.com/ericmjl/minimal-flask-example
.- Create a virtual environment using
$ mkvirtualenv --python=/usr/bin/python3.6 "myappname"
.- If there's an issue with
mkvirtualenv
, follow the commands here. - In the future, if you want to activate the virtual environment, PythonAnywhere provides an alternate command to activate it:
$ workon my-virtualenv
.
- If there's an issue with
- Install all required packages:
$ pip install -r requirements.txt
. - Follow the instructions on the Flask tutorial page under the title, "Setting up the Web app using Manual configuration".
- Instructions to be continued live...