This is the Python Charmers Coffee Shop application.
-
Get the
.envfile from the trainers. This is configured for minimal configuration -
In the
.envfile change yourDB_NUMBERsetting (at the top of the file) to either your personal DB number or your group number. -
Create a new virtual environment:
python3 -m venv venv
-
Activate the virtual environment
OS Command Windows venv/bin/activateOS X / *nix source venv/bin/activate -
Install the application requirements
pip install -r requirements.txt pip install -r dev_requirements.txt
-
Set the
FLASK_APPenvironment variablsOS Command Windows set FLASK_APP=server OS X / *nix export FLASK_APP=server -
Upgrade the database with
flask db upgrade -
Run the application locally with
flask run
- Install Zappa with
pip install zappa(if you're using Python 3.7 usepip install git+https://github.com/itamt/Zappa.git) - Run
zappa initto initialise the zappa application.- The app's functon will be
server.app - For now select
nwhen asked if you want to deploy globally
- The app's functon will be
- Run
zappa deploy devto deploy the application - Test the application by visiting the URL you're provided
Create and activate a virtual environment, and then install the requirements:
pip install -r requirements.txt
pip install -r dev_requirements.txt
Copy sample environment file from the environment_config folder into a file
.env in the base folder:
cp environment_config/sample.env .env
The following environment variables should be updated in the .env files:
| Variable | Description |
|---|---|
SECRET_KEY |
The application secret key. You could use `python3 -c "import os; print(os.urandom(48))" |
SQLALCHEMY_DATABASE_URL |
The SQLAlchemy connection string. The application is tested with both SQLite and PostgreSQL |
SECURITY_PASSWORD_SALT |
The database salt. Can be generated in the same way as the SECRET_KEY |
UPLOADED_PHOTOS_DEST |
The absolute path to the folder where uploaded photos will be stored on your local machine |
To run an app you will also need to set an environment variable FLASK_APP to server:
export FLASK_APP=server
You can use Alembic to set up the database:
flask db upgrade
Ultimately this application is intended to be deployed on AWS. As such there's
a dependency on the boto3 library. The application will work locally
provided you have boto3 installed, but for uploading files to S3 and
deployment to AWS Lambda you will need the AWS CLI installed and configured.
Installing the AWS CLI is easy (if you wish there is complete documentation available as well):
- Outside your Virtual Environment (to install globally on your system) run:
pip3 install awscli
- Copy your AWS Access Key ID and your AWS Secret Access Key from your IAM role in the AWS management console
- From the command line run:
to complete the configuration process
aws configure
- Test the configuration by running:
to list all of your S3 buckets along with their unique identifiers
aws s3 ls
You can run the server locally using the standard flask comands.
flask run
There will be an extended exercises per day based on the coffee shop application.
The solutions will be separated into individual branches (in the form
day_n_exercise), and there is a combined_solutions branch where all
extended exercises are merged together.
The front page of the coffee shop application is very bare at the moment. We can do better:
-
Update the / endpoint to display the 10 most recently added shops.
Since we haven't covered SQLAlchemy yet, use the following to generate shops to pass through to the template:
from coffeeshop.server.shop.models import Shop shops = Shop.query.order_by(Shop.date_added.desc()).limit(10)
-
Update the template to show a list of these shops, linking to the individual shop page (see the search results for inspiration if you get stuck)
It would be nice to create an API for people to programatically query the coffee shops, rather than having to scrape the site.
Use Connexion to create a simple API to access the Shops model. It should have
two endpoints:
- A list endpoint which lists stores 10 at a time. It should take a
pageparameter which defaults to1and can be any positive integer, which is the query offset for which stores to return. - A search endpoint which lists stores matching a query passed via the
qparameter, in the same way the currentcoffeeshop.server.shop.views.search_shopmethod handles queries. It should also take a page parameter.
The functions that are mapped by the app.yaml file should be store in
coffeeshop/server/shop/api.py
You will also need to create a new create_connexion_app method within your
coffeeshop/server/__init__.py file.
Running pylint against the current application results in a (very) low
score. There are issues with the code style, but there are also some errors
which are being falsely reported, specifically:
- E1101 (instance of object doesn't have member)
- R0401 (circular imports)
Generate a .pylintrc file for your project with:
pylint --generate-rcfile > .pylintrcIn the .pylintrc file add those two errors to the disabled messages list.
Re-run pylint and update the files so that the score is at least 8 out of 10.
To deploy to AWS Lambda you'll need:
- To have the AWS CLI installed and configured
- To install Zappa (if you're using Python 3.7 use
pip install git+https://github.com/itamt/Zappa.git) - You will need a database setup on RDS where the application data will be stored - the connection string for an accessible PostGreSQL database will be provided via a download link by the trainers.
- You'll need to create a new bucket on S3 to store photos. It should not block public ACLs from uploading. It will be used to store uploaded photos.
- You'll need to update the following environment variables in your
.envfile (note: it's a good idea to backup your development.envfile first - possibly withcp .env environment_config/dev.env):SECRET_KEYshould be generated for production as per the instructions aboveSECURITY_PASSWORD_SALTlikewise the password salt should be updatedSQLALCHEMY_DATABASE_URIthe connection string for your RDS databaseFLASK_ENVshould now be set toproductionWTF_CSRF_ENABLEDshould beTrue
- Run the Alembic upgrade to add your database tables on the server
Your goal is to upload photos to a S3 bucket. As such you'll need to update your
S3 settings accordingly (in your .env file):
| Variable | Description |
|---|---|
S3_BUCKET |
The name of the bucket you've set up to store your data |
S3_KEY_BASE |
The name of the folder inside your S3 bucket where the photo will be stored |
S3_LOCATION |
If your bucket is outside Sydney you'll need to update the bucket location |
Finally, to ensure the upload works, set the environment variable that will control your app:
On Windows:
set APP_SETTINGS="coffeeshop.server.config.ProductionConfig"On Unix or OS X:
export APP_SETTINGS="coffeeshop.server.config.ProductionConfig"Then run the app as normal. Test that when you create a new shop and add a photo that instead of being stored locally, the file is uploaded to your S3 bucket.