Skip to content

Instantly share code, notes, and snippets.

@awesomekimn
Created April 16, 2022 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesomekimn/ee5cccf427300cb671d2122a712dac9e to your computer and use it in GitHub Desktop.
Save awesomekimn/ee5cccf427300cb671d2122a712dac9e to your computer and use it in GitHub Desktop.
Virtual Bookshelf
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<form action="" method="POST">
<label>Book Name</label>
<input name="title" type="text">
<label>Book Author</label>
<input name="author" type="text">
<label>Rating</label>
<input name="rating" type="text">
<button type="submit">Add Book</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Rating</title>
</head>
<body>
<form action="{{ url_for('home') }}" method="POST">
<p>Book Name: {{book.title}}</p>
<p>Current Rating {{book.rating}}</p>
<input hidden="hidden" name="id" value="{{book.id}}">
<input name="rating" type="text" placeholder="New Rating">
<button type="submit">Change Rating</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Library</title>
</head>
<body>
<h1>My Library</h1>
{% if books == []: %}
<p>Library is empty.</p>
{% endif %}
<ul>
{% for book in books: %}
<li>
<a href="{{ url_for('delete', id=book.id) }}">Delete</a>
{{ book.title }} - {{ book.author }} - {{ book.rating }}/10
<a href="{{ url_for('edit', id=book.id) }}">Edit Rating</a>
</li>
{% endfor %}
</ul>
<a href="{{ url_for('add') }}">Add New Book</a>
</body>
</html>
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# CREATE DATABASE
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///books.db"
# Optional: But it will silence the deprecation warning in the console.
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# CREATE TABLE
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(250), unique=True, nullable=False)
author = db.Column(db.String(250), nullable=False)
rating = db.Column(db.Float, nullable=False)
db.create_all()
@app.route('/')
def home():
all_books = db.session.query(Book).all()
return render_template('index.html', books=all_books)
@app.route("/add", methods=["GET", "POST"])
def add():
if request.method == "POST":
# CREATE RECORD
new_book = Book(
title=request.form["title"],
author=request.form["author"],
rating=request.form["rating"]
)
db.session.add(new_book)
db.session.commit()
return redirect(url_for('home'))
return render_template("add.html")
@app.route("/edit", methods=["GET", "POST"])
def edit():
if request.method == "POST":
#UPDATE RECORD
book_id = request.form["id"]
book_to_update = Book.query.get(book_id)
book_to_update.rating = request.form["rating"]
db.session.commit()
return redirect(url_for('home'))
book_id = request.args.get('id')
book_selected = Book.query.get(book_id)
return render_template("edit_rating.html", book=book_selected)
@app.route('/delete')
def delete():
book_id = request.args.get('id')
# DELETE A RECORD BY ID
book_to_delete = Book.query.get(book_id)
db.session.delete(book_to_delete)
db.session.commit()
return redirect(url_for('home'))
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment