Let’s setup your dev environment
Before we can build our Flask-based web app, we need to clone the project and install a few Python packages.
Clone the project from GitHub
This repository is a skeleton for the project. The Python files are empty – we’ll be adding code throughout this tutorial. However, the index.html and main.js are ready for you from the start.
- Open command line (Windows) or terminal (macOS/Linux). In your home directory, clone the project from GitHub:
git clone https://github.com/erhopf/camp2019.git - Navigate to the project directory:
cd camp2019
Install virtualenv and enable your dev environment
- Now that we’ve cloned the project, we need to install some dependencies. First, let’s install
virtualenv. This is an isolated development environment that allows you to install packages for testing without impacting the entire system. Run:pip install virtualenvNow, let’s make sure everything is working. Run:
virtualenv --versionThe version should be printed to terminal. Anything else means that something went wrong. Let us know, we’re here to help!
-
Next, let’s create a virtual environment for our project. Run:
macOS/Linux:
virtualenv venv --python=python3We’ve explicitly declared that the virtual environment should use Python 3. This ensures that users with multiple Python installations are using the correct version.
Windows CMD / Windows Bash:
virtualenv venvMake sure that your virtualenv is using Python 3x. If you’re virtualenv was created with Python 2.7.x, please see Troubleshooting.
-
The commands to activate your virtual environment will vary depending on if you are a Windows or macOS/Linux user.
macOS/Linux:
source venv/bin/activateWindows CMD:
venv\Scripts\activateWindows Bash:
source venv/Scripts/activateAfter running this command, your command line or terminal session should be prefaced with
venv.
You can deactivate the session at any time by typing this into the command line or terminal
deactivate.
Install requests
Requests is a popular module that is used to send HTTP 1.1 requests. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. If you’d like to know more, I encourage you to check out their site.
pip install requests
Install and configure Flask
- Next we need to install Flask. Flask is the micro framework that we’ll use to build our web app. It handles the routing, and allows us to make server-to-server calls that hide our subscription keys from the end user. To install Flask, run:
pip install FlaskLet’s make sure Flask was installed. Run:
flask --versionThe version should be printed to terminal. Anything else means that something went wrong. Let us know, we’re here to help!
-
To run the flask app you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal which app to work with by exporting the
FLASK_APPenvironment variable:macOS/Linux:
export FLASK_APP=app.pyWindows:
set FLASK_APP=app.py