forked from Callum-Freeburn27/Book-Cover-Scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI_Flask_App.py
71 lines (58 loc) · 2.47 KB
/
GUI_Flask_App.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, render_template, request, redirect, url_for, flash
from werkzeug.utils import secure_filename
from flask_navigation import Navigation
import os
from booksDatabase import db, Books
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
UPLOAD_FOLDER = './static/'
exampleBookDetails = {}
exampleBookDetails['Title'] = 'Why Nations Fail'
exampleBookDetails['Author'] = ' Daron Acemoglu and James Robinson'
exampleBookDetails['Genre'] = 'Economics'
exampleBookDetails['Publisher'] = 'Crown Business'
app = Flask(__name__)
app.config ['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///books.sqlite3'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = 'the random string'
db.init_app(app)
nav = Navigation(app)
nav.Bar('top', [
nav.Item('Home', 'home_page'),
nav.Item('Upload Book Cover', 'upload_cover'),
nav.Item('Show Books Database', 'show_database', {'page': 1}),
nav.Item('About Us', 'about_us'),
])
def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def home_page():
return render_template('homepage.html', image = 'static/why_nations_fail.jpg', exampleBookDetails=exampleBookDetails)
@app.route('/uploadImage', methods=['POST', 'GET'])
def upload_cover():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part', 'error')
return redirect(url_for('upload_cover'))
file = request.files['file']
if file and not allowed_file(file.filename):
flash('Not a Valid File, must be a png, jpg or jpeg', 'error')
return redirect(url_for('upload_cover'))
if file.filename == '':
flash('No selected file', 'error')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(file)
return redirect(url_for('book_details'))
return render_template('uploadCover.html')
@app.route('/bookDetails')
def book_details():
return render_template('book_details.html', bookDetails=exampleBookDetails, image = 'static/why_nations_fail.jpg')
@app.route('/show_all')
def show_database():
return render_template('show_all.html', Books = Books.query.all())
@app.route('/about_us')
def about_us():
return render_template('about_us.html')
if __name__ == '__main__':
app.run(debug=True)