Skip to content

Commit 59dc46d

Browse files
authored
Add flask code base
1 parent 533779b commit 59dc46d

File tree

9 files changed

+329
-0
lines changed

9 files changed

+329
-0
lines changed

Pipfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
flask = "*"
8+
flask-sqlalchemy = "*"
9+
10+
[dev-packages]
11+
12+
[requires]
13+
python_version = "3.8"

Pipfile.lock

Lines changed: 221 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/__init__.py

Whitespace-only changes.

server/__main__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
sys.path.append('.')
3+
4+
from server.webapp import flaskapp, db, TEMPLATES
5+
from server.models import *
6+
from server.routes import *
7+
8+
default_books = [
9+
("The Hobbit", "JRR Tolkien", True),
10+
("The Fellowship of the Ring", "JRR Tolkien", True),
11+
("The Eye of the World", "Robert Jordan", False),
12+
("A Game of Thrones", "George R. R. Martin", True),
13+
("The Way of Kings", "Brandon Sanderson", False)
14+
]
15+
16+
17+
if __name__ == "__main__":
18+
for bookname, bookauthor, hasread in default_books:
19+
if not Book.query.filter_by(name=bookname).first():
20+
book = Book(name=bookname, author=bookauthor, read=hasread)
21+
db.session.add(book)
22+
db.session.commit()
23+
24+
db.create_all()
25+
flaskapp.run('0.0.0.0', debug=True)

server/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
from server.models.books import Book

server/models/books.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
from server.webapp import db
3+
4+
5+
class Book(db.Model):
6+
id = db.Column(db.Integer, primary_key=True)
7+
name = db.Column(db.String(128), unique=False, nullable=False)
8+
author = db.Column(db.String(128), unique=False, nullable=False)
9+
10+
read = db.Column(db.Boolean(), unique=False, nullable=False)

server/routes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
from flask import request, render_template, make_response
3+
4+
from server.webapp import flaskapp, db
5+
from server.models import Book
6+
7+
8+
@flaskapp.route('/')
9+
def index():
10+
name = request.args.get('name')
11+
author = request.args.get('author')
12+
read = bool(request.args.get('read'))
13+
14+
if name:
15+
result = db.engine.execute(
16+
"SELECT * FROM Book WHERE name LIKE '%" + name + "%'"
17+
)
18+
books = [row for row in result]
19+
if len(books) == 0:
20+
return make_response(f"Search Result not found: {name}", 404)
21+
elif author:
22+
result = db.engine.execute(
23+
"SELECT * FROM Book WHERE author LIKE '%" + author + "%'"
24+
)
25+
books = [row for row in result]
26+
if len(books) == 0:
27+
return make_response(f"Search Result not found: {author}", 404)
28+
else:
29+
books = Book.query.all()
30+
31+
return render_template('books.html', books=books)

server/webapp.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
3+
from flask import Flask
4+
from flask_sqlalchemy import SQLAlchemy
5+
6+
ROOT = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
7+
TEMPLATES = os.path.join(ROOT, 'templates')
8+
9+
flaskapp = Flask("BookStore", template_folder=TEMPLATES)
10+
flaskapp.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLITE_URI', 'sqlite:////tmp/test.db')
11+
flaskapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
12+
13+
db = SQLAlchemy(flaskapp)

templates/books.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Book Store</title>
5+
</head>
6+
<body>
7+
<h1>Book Store</h1>
8+
<ul id="books">
9+
{% for book in books %}
10+
<li>{{ book.name }} by {{ book.author }}</li>
11+
{% endfor %}
12+
</ul>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)