The instructions below are meant for the local setup only. The classroom workspace is already set for your to start practicing.
-
Developers using this project should already have Python3, pip and node installed on their local machines.
-
Start your virtual environment From the backend folder run
# Mac users
python3 -m venv venv
source venv/bin/activate
# Windows users
> py -3 -m venv venv
> venv\Scripts\activate
- Install dependencies
From the backend folder run
# All required packages are included in the requirements file.
pip3 install -r requirements.txt
# In addition, you will need to UNINSTALL the following:
pip3 uninstall flask-socketio -y
Mac users can follow the commands below:
which postgres
postgres --version
# Start/stop
pg_ctl -D /usr/local/var/postgres start
pg_ctl -D /usr/local/var/postgres stop
Windows users can follow the commands below:
- Find the database directory, it should be something like that: C:\Program Files\PostgreSQL\13.2\data
- Then, in the command line, execute the folllowing command:
# Start the server
pg_ctl -D "C:\Program Files\PostgreSQL\13.2\data" start
# Stop the server
pg_ctl -D "C:\Program Files\PostgreSQL\13.2\data" stop
If it shows that the port already occupied error, run:
sudo su -
ps -ef | grep postmaster | awk '{print $2}'
kill <PID>
-
Verify the database username
Verify that the database user in the/backend/books.psql
,/backend/models.py
, and/backend/test_flaskr.py
files must be either thestudent
orpostgres
(default username). FYI, the classroom workspace uses thestudent
/student
user credentials, whereas, the local implementation can use the dafaultpostgres
user without a password as well. (See the/backend/setup.sql
for more details!) -
Create the database and a user
In your terminal, navigate to the /nd0044-c2-API-Development-and-Documentation-exercises/1_Requests_Starter/backend/ directory, and run the following:
cd nd0044-c2-API-Development-and-Documentation-exercises/1_Requests_Starter/backend
# Connect to the PostgreSQL
psql postgres
#View all databases
\l
# Create the database, create a user - `student`, grant all privileges to the student
\i setup.sql
# Exit the PostgreSQL prompt
\q
- Create tables
Once your database is created, you can create tables (bookshelf
) and apply contraints
# Mac users
psql -f books.psql -U student -d bookshelf
# Linux users
su - postgres bash -c "psql bookshelf < /path/to/exercise/backend/books.psql"
You can even drop the database and repopulate it, if needed, using the commands above.
Navigate to the /backend/flaskr/__init__.py
file, and finish all the @TODO
thereby building out the necessary routes and logic to get the backend of your app up and running.
Once you've written your code, start your (backend) Flask server by running the command below from the /backend/
directory.
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
These commands put the application in development and directs our application to use the __init__.py
file in our flaskr folder. Working in development mode shows an interactive debugger in the console and restarts the server whenever changes are made. If running locally on Windows, look for the commands in the Flask documentation.
The application will run on http://127.0.0.1:5000/
by default and is set as a proxy in the frontend configuration. Also, the current version of the application does not require authentication or API keys.
(You can start the frontend even before the backend is up!)
From the frontend
folder, run the following commands to start the client:
npm install // only once to install dependencies
npm start
By default, the frontend will run on localhost:3000
. Close the terminal if you wish to stop the frontend server.
If any exercise needs testing, navigate to the /backend
folder and run the following commands:
psql postgres
dropdb bookshelf_test
createdb bookshelf_test
\q
psql bookshelf_test < books.psql
python test_flaskr.py
from distutils.log import error import os from flask import Flask, request, abort, jsonify from flask_sqlalchemy import SQLAlchemy # , or_ from flask_cors import CORS import random
from models import setup_db, Book, db
BOOKS_PER_SHELF = 8 def paginate(request,query): # Pagination page = request.args.get('page', 1, type=int) start = (page - 1 ) * BOOKS_PER_SHELF end = start + BOOKS_PER_SHELF
books = [book.format() for book in query]
current_books = books[start:end]
return current_books