This repo serves as an example to get started with Flask.
- Clone the repo on your local device:
git clone git@github.com:chunloy/flask-workshop.git
- Navigate into
flask-workshop
directory and create a virtual environment:cd flask-workshop && python -m venv .venv
- Activate the virtual environment:
source .venv/bin/activate
- Install dependencies:
pip3 install -r requirements.txt
- Start the server in debugging mode:
flask run --debug
- Close the virtual environment when you're done:
deactivate
Note: See section below for things I forgot to mention during the workshop.
-
The code may look slightly different from the recording due to small changes that I made, I'd recommend using this repo for reference. Changes involved returning data as is because they are valid return types by default.
-
You can get away with running
flask run
as is because the module is namedapp.py
. If the module was named different, you'd have to specify the name in the command. -
Making a request to get details for a single artist:
see lines 95-98 in app.py
-
Making the
POST
request:curl -X POST localhost:5000/artists -d '{"artist_name":"notaaron","song_title":"song","featured_artist":"null","album_name":"hello, world","song_length":"4:00 PM"}' -H 'Content-type: application/json'
-X
specifies the type of request-d
specifies the data in the request body-H
specifies in the request header that the data isjson
- Try making a curl request with
GET
. This is my attempt at getting you allCLI Certified
.
- Prerequisites: install pip, virtualenv
- Setting up your project: activate & deactivate virtualenv
- Live demo: Setup Flask server + routing
- Q & A
These instructions assume you have Python 3.x installed.
Note: When running commands, be sure to use
python3
andpip3
, instead ofpython
orpip
, to avoid potential errors.
Run this command to install pip
:
cd ~
python3 -m pip install --user --upgrade pip
python3 -m pip --version
Link installation docs: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
Run this command to install virtualenv
:
pip3 install virtualenv
link to package description: https://pypi.org/project/virtualenv/
Why create a virtual environment? It installs packages only in the current directory. It's best to avoid installing packages globally to prevent version conflicts.
Note: The following commands are only vaild for Mac. If you're on Windows, you'll need to use a different set of commands.
- Create a virtual environment by running this command:
python3 -m venv .venv
Note: you should see a folder named
.venv
in your project's root directory
- Activate the virtual environment:
source .venv/bin/activate
Note: Make sure you do this BEFORE installing packages!
- Run this command to close the virtual environment:
deactivate
Congratulations! You've set up a virttual environment and can safely install packages.
After creating a project, you'll probably want to install and keep track of the packages you've installed. This will help other developers clone your project and install dependencies.
- Create a text file in the project's root directory:
touch requirements.txt
- Run this command to list your dependencies in the text file:
pip3 freeze > requirements.txt
Note: You'll want to run the above command every time you install new packages. Python doesn't keep track of them for you.