def application(environ, start_response): global messages status = '200 OK' response_headers = [('Content_type', 'text/HTML')] start_response(status, response_headers) # send different page depending on URL path path = environ['PATH_INFO'] db = BookDB() if path == '/index.html': book_list = db.titles() strings = [] for book in book_list: book_id = book['id'] book_title = book['title'] strings.append(book_id) strings.append(book_title) page = index_template % ( strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6], strings[7], strings[8], strings[9]) elif path == '/detail.html/': book_id = \ urlparse.parse_qs(environ['QUERY_STRING'])['book_id'][0] book_info = db.title_info(book_id) page = detail_template % (book_info['title'], book_info['title'], book_info['isbn'], \ book_info['publisher'], book_info['author']) else: page = notfound_template % path return [page] # list of strings - must return iterable, not just a string
def __init__(self): self.reader = CodeReader() self.db = BookDB(RegisterBooks.LIBRARIAN_DB_NAME) self.db.create_table() outdir = pathlib.Path(RegisterBooks.BOOKS_QRCODE_DIR) if not outdir.exists(): outdir.mkdir()
def my_admin_load_BooksDB(): my_books = BookDB() book_count = -1 for my_book in my_books.titles(): save_book(my_book['id'], my_books.title_info(my_book['id'])['isbn'].replace('-', ''), my_books.title_info(my_book['id'])['title'], my_books.title_info(my_book['id'])['author'], my_books.title_info(my_book['id'])['publisher'])
def __init__(self): self.bookdb = BookDB() self.book = Book()
import re from bookdb import BookDB DB = BookDB() def book(book_id): page = """ <h1>%(title)s</h1> <table> <tr><th>Author</th><td>%(author)s</td></tr> <tr><th>Publisher</th><td>%(publisher)s</td></tr> <tr><th>ISBN</th><td>%(isbn)s</td></tr> </table> <a href="/">Back to the list</a> """ book = DB.title_info(book_id) if book is None: raise NameError return page % book def books(): all_books = DB.titles() body = ['<h1>My Bookshelf</h1>', '<ul>'] item_template = '<li><a href="/book/%(id)s">%(title)s</a></li>' for book in all_books: body.append(item_template % book) body.append('</ul>') return '\n'.join(body)
def makeOne(self): from bookdb import BookDB return BookDB()
def __init__(self): self.bookdb = BookDB() self.safaribook = None
def detail(book_id=None): db = BookDB() return detail_template % (db.title_info(book_id))
def index(): db = BookDB() book_dict = db.titles() import pdb pdb.set_trace() return index_template % book_dict
def my_admin_load_BooksDB(request): my_books=BookDB()
def database(): from bookdb import BookDB return BookDB()
Send HTML page that echoes message from HTTP request To get started, point browser at echo_flask.html """ from flask import Flask, render_template, request from bookdb import BookDB app = Flask(__name__) app.debug = True # development only - remove on production machines # View functions generate HTTP responses including HTML pages and headers db = BookDB() ''' This function gets a dict of the ids and titles from the database and passes it to the template. ''' @app.route('/index.html/') def index(): return render_template('index.html', titles=db.titles()) ''' This function takes a book id and gets the title info from the database as a dict. This dict is passed to the template. ''' @app.route('/book_details/<book_id>') def detail(book_id=None): return render_template('detail.html', details=db.title_info(book_id))
""" My book navigator """ from flask import Flask, render_template, request from bookdb import BookDB app = Flask(__name__) app.debug = True # development only - remove on production machines # View functions generate HTTP responses including HTML pages and headers all_books = BookDB() #Show the welcome page @app.route('/welcome.html') def welcome(): return render_template('welcome.html') #Show the index page @app.route('/index.html') def index(): return render_template('index.html', books=all_books) #Show details page @app.route('/book.html') def book_info(): #render the template for the selected book based on the id tag