Skip to content

Instantly share code, notes, and snippets.

@ericbarnhill
Last active June 20, 2018 15:30
Show Gist options
  • Save ericbarnhill/3e3cfe143efc147759f6d3ad33e1d628 to your computer and use it in GitHub Desktop.
Save ericbarnhill/3e3cfe143efc147759f6d3ad33e1d628 to your computer and use it in GitHub Desktop.
Errata for Flask by Example Tutorial in Ubuntu 18.04 LTS

Errata for RealPython's "Flask by Example", updated for Ubuntu LTS

These are notes on the excellent "Flask by Example" tutorial which begins here: https://realpython.com/flask-by-example-part-1-project-setup/

I really appreciate this tutorial and its use of several relevant frameworks and services.

However, many of the code snippets do not work exactly as written for a user coming in with a blank slate and the latest Ubuntu LTS. I took notes as I went along making it work and keep them stored here. I will format these later after I am done.

Chapter 1

Creating the environment We use conda right from the beginning to handle a big problem later which is that pip on Ubuntu LTS breaks on trying to install psycopg2:

mkdir $CODE/flask_app
conda create -n flask_app
source activate flask_app

as per the tutorial:

touch app.py .gitignore README.md requirements.txt

and copy the README.md off the tutorial repo.

conda install flask

instead of pip freeze > requirements.txt use

conda list --export > requirements.txt

(hat tip https://stackoverflow.com/questions/41249401/) Enter the code for app.py and run. Verify that it works. Follow the instructions to create the Procfile, then install gunicorn:

conda install gunicorn
conda list --export > requirements.txt

The conda-generated requirements file now requires substantial editing by hand for Heroku. You now have to go into the conda-generated requirements.txt and convert the first "=" on each line to "==". You must then delete the second equals to the end of the line. Finally, remove the following requirements: libedit, libffi, libgcc-ng, libstdcxx-ng, ncurses, openssl, python, readline, sqlite, tk, xz, zlib.

Note: if you have an incompatibility in your requirements.txt that causes the repository pushes to Heroku to bounce, remember to commit to your own git repo before pushing again, or the edited requirements.txt will not be read.

Create runtime.txt with the following line. The version 3.5.1 in the tutorial will not work on Heroku:

python-3.6.5

Commit changes to git. Example code:

git add .
git commit -a -m "created environment and set requirements"
git push origin master
git remote add origin https://github.com/ericbarnhill/flask_app
git push origin master

Now to setup heroku:

heroku login

(enter credentials)

heroku create wordcount-pro-ericbarnhill
heroku create wordcount-stage-ericbarnhill

Please recall the note above, that if anything in your requirements.txt has to be changed, commit your repository again so the changes are picked up.

git remote add stage git@heroku.com:wordcount-stage-ericbarnhill.git
git remote add pro git@heroku.com:wordcount-stage-ericbarnhill.git
git push stage master
git push pro master

Now make the config file

touch config.py (or vim)

and copy in the code for the configuration classes.

Setting environment variables Conda requires a different approach to environment variables. A good tutorial is here. You can set desired environment variables in your conda environment this way. Then run source activate again to add the variables.

The rest of the chapter can be run without alterations.

Chapter 2

Here the installation of new packages, as well as their import into the .py files, is quite different from the tutorial. Install the packages in conda:

conda install -c conda-forge psycopg2 flask-script flask-migrate flask-sqlalchemy

do not create a new requirements.txt export file. Make a new conda export to another file

conda list --export > requirements_new.txt

then copy over the requirements for alembic, flask-migrate, flask-script, flask-sqlalchemy, psycopg2, and sqlalchemy, altering the first "=" to "==" and removing everything from each line starting from and including the second "=". Examples:

alembic==0.9.9
flask-migrate==2.1.1
flask-script==2.0.6
flask-sqlalchemy==2.3.2
[etc]

Now we have accomplished one of the chief payoffs of the adjustments to the chapter of the last tutorial. conda will successfully install psycopg2 on the latest Ubuntu LTS out of the box, while pip's psycopg2 breaks. Thanks to making the adjustment of using conda we can proceed with the tutorial.

Update any versions in your requirements.txt that need updating after this new installation, which may update some packages. Add a new environment variable DATABASE_URL. However note that the version given in the tutorial:

export DATABASE_URL="postgresql://localhost/wordcount_dev"

will probably not work for your Ubuntu postgreSQL setup. You may need an entry of the form

postgresql://user:password@localhost/wordcount_dev

where user and password are your login credentials for your postgres database. The following codes in the tutorial require several changes. In the file app.py, instead of

from flask.ext.sqlalchemy

use

from flask_sqlalchemy

Note that the line

from models import Result

will cause circular dependencies if app.py or models.py is run, and must be run in the context of manage.py

In the file manage.py, instead of

from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

use

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

the rest of the chapter should execute fine as written.

Chapter 3

Import the packages with conda:

conda install -c conda-forge requests beautifulsoup4 nltk

This installation is lengthy. Afterwards add the relevant requirements to requirements.txt manually, skipping the others. Mine were:

nltk==3.2.5
beautifulsoup4==4.6.0
requests==2.19.1
requests-oauthlib==0.8.0

Commit and push changes to local, stage and pro repositories.

git push origin master
git push stage master
git push pro master

When you update codes in this chapter, make sure to change the import names to the correct import names as shown above in previous chapters (e.g. flask_migrate).

Chapter 4

again install packages with conda:

conda install -c conda-forge redis rq

and again hack your requirements.txt file to add redis and rq, but in pip-compatible format. redis-server should make right out of the box.


notes from earlier run

runtime must be python-3.6.5 remove pkg-resources from pip freeze

pip install wheel is required psycopg2 must be installed with sudo apt install python-psycopg2 -- update: better to just start with conda

flask_script, flask_migrate, flask_sqlalchemy - use underscores

circular dependendices between models.py and app.py -- need to remove models import line in app.py

need soft link between var and tmp

make sure username and password from SQL database are used

needed to manually add flask-script to dependencies (must have been installed in some other way)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment