https://github.com/belovetech/bookshelf
This is a simple bookshelf flask application. The project's goal is to test general knowledge of PostgreSQL, database interaction with the server, and many other things.
https://github.com/belovetech/bookshelf
flask postgresql python
Last synced: 3 months ago
JSON representation
This is a simple bookshelf flask application. The project's goal is to test general knowledge of PostgreSQL, database interaction with the server, and many other things.
- Host: GitHub
- URL: https://github.com/belovetech/bookshelf
- Owner: belovetech
- Created: 2022-06-12T22:07:36.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-13T09:45:02.000Z (about 4 years ago)
- Last Synced: 2025-02-24T08:36:08.096Z (over 1 year ago)
- Topics: flask, postgresql, python
- Language: Python
- Homepage:
- Size: 39.2 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Local Development
The instructions below are meant for the local setup only. The classroom workspace is already set for your to start practicing.
#### Pre-requisites
- 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
```bash
# 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
```bash
# 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
```
### Step 0: Start/Stop the PostgreSQL server
Mac users can follow the commands below:
```bash
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:
```bash
# 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:
```bash
sudo su -
ps -ef | grep postmaster | awk '{print $2}'
kill
```
### Step 1 - Create and Populate the database
1. **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 the `student` or `postgres` (default username). FYI, the classroom workspace uses the `student`/`student` user credentials, whereas, the local implementation can use the dafault `postgres` user without a password as well. (See the `/backend/setup.sql` for more details!)
2. **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:
```bash
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
```
3. **Create tables**
Once your database is created, you can create tables (`bookshelf`) and apply contraints
```bash
# 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.**
### Step 2: Complete the ToDos and Start the backend server
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](http://flask.pocoo.org/docs/1.0/tutorial/factory/).
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.
### Step 3: Start the frontend
(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.
---
## Additional information
#### Running Tests
If any exercise needs testing, navigate to the `/backend` folder and run the following commands:
```bash
psql postgres
dropdb bookshelf_test
createdb bookshelf_test
\q
psql bookshelf_test < books.psql
python test_flaskr.py
```