In this project, you’ll build a book review website. Users will be able to register for your website and then log in using their username and password. Once they log in, they will be able to search for books, leave reviews for individual books, and see the reviews made by other people. You’ll also use the a third-party API by Goodreads, another book review website, to pull in ratings from a broader audience. Finally, users will be able to query for book details and book reviews programmatically via your website’s API.
For this project, you’ll need to set up a PostgreSQL database to use with our application. It’s possible to set up PostgreSQL locally on your own computer, but for this project, we’ll use a database hosted by Heroku, an online web hosting service.
Alternatively, if you install
PostgreSQL on your own computer, you
should be able to run psql URI
on the command line, where the URI
is
the link provided in the Heroku credentials list.
pip
. If you downloaded Python from Python’s
website, you likely already have pip
installed (you can check by running
pip
in a terminal window). If you don’t have it installed, be sure to
install it before moving on!To try running your first Flask application:
project1
distribution directory from https://cdn.cs50.net/web/2020/x/projects/1/project1.zip and unzip it.project1
directory.pip3 install -r requirements.txt
in your terminal window to make sure
that all of the necessary Python packages (Flask and SQLAlchemy, for
instance) are installed.FLASK_APP
to be application.py
. On a Mac or
on Linux, the command to do this is export FLASK_APP=application.py
. On
Windows, the command is instead set FLASK_APP=application.py
. You may
optionally want to set the environment variable FLASK_DEBUG
to 1
, which
will activate Flask’s debugger and will automatically reload your web
application whenever you save a change to a file.DATABASE_URL
to be the URI of your database,
which you should be able to see from the credentials page on Heroku.flask run
to start up your Flask application.flask
, you should see the text
"Project 1: TODO"
!Goodreads is a popular book review website, and we’ll be using their API in this project to get access to their review data for individual books.
import requests
res = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "KEY", "isbns": "9781632168146"})
print(res.json())
where KEY
is your API key, will give you the review and rating data for the
book with the provided ISBN number. In particular, you might see something like
this dictionary:
{'books': [{
'id': 29207858,
'isbn': '1632168146',
'isbn13': '9781632168146',
'ratings_count': 0,
'reviews_count': 1,
'text_reviews_count': 0,
'work_ratings_count': 26,
'work_reviews_count': 113,
'work_text_reviews_count': 10,
'average_rating': '4.04'
}]
}
Note that work_ratings_count
here is the number of ratings that this
particular book has received, and average_rating
is the book’s average score
out of 5.
Alright, it’s time to actually build your web application! Here are the requirements:
books.csv
,
which is a spreadsheet in CSV format of 5000 different books.
Each one has an ISBN number, a title, an author, and a publication year.
In a Python file called import.py
separate from your web application,
write a program that will
take the books and import them into your PostgreSQL database. You will first need to
decide what table(s) to create, what columns those tables should have, and how
they should relate to one another. Run this program by running
python3 import.py
to import the books into
your database, and submit this program with the rest of your project code./api/<isbn>
route, where <isbn>
is an ISBN number, your website should return a JSON
response containing the book’s title, author, publication date, ISBN number,
review count, and average score. The resulting JSON should follow the format:{
"title": "Memory",
"author": "Doug Lloyd",
"year": 2015,
"isbn": "1632168146",
"review_count": 28,
"average_score": 5.0
}
If the requested ISBN number isn’t in your database, your website should return a 404 error.
execute
method) in
order to make database queries. You should not use the SQLAlchemy ORM (if
familiar with it) for this project.README.md
, include a short writeup describing your project, what’s
contained in each file, and (optionally) any other additional information the
staff should know about your project.requirements.txt
!Beyond these requirements, the design, look, and feel of the website are up to you! You’re also welcome to add additional features to your website, so long as you meet the requirements laid out in the above specification!
session
, which can store different values for different users.
In particular, if each user has an id
, then you could store that id
in the
session (e.g., in session["user_id"]
) to keep track of which user is
currently logged in.Any order is fine!
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Make sure that you’ve set your DATABASE_URL
environment variable before running
flask run
!
IMPORTANT: On 1 July 2020, CS50W will update to a new version and this version of this project will no longer be accepted for credit. If you haven’t completed the entire course by that date, that’s okay! If you ultimately receive a passing grade on this project, we will consider it equivalent in scope and content to Project 2 in the new version of the course, and within two weeks after 1 July 2020 your gradebook at https://cs50.me/cs50w will update to reflect that you have received credit for having completed Project 2.
submit50
, execute
submit50 web50/projects/2020/x/1
Otherwise, using Git, push your work to https://github.com/me50/USERNAME.git
, where USERNAME
is your GitHub username, on a branch called web50/projects/2020/x/1
.
You can then go to https://cs50.me/cs50w to view your progress!