Show prices through a minimal webapp
This commit is contained in:
33
webapp.py
Normal file
33
webapp.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from bottle import hook, request, route, run, static_file, view
|
||||
import models
|
||||
from models import Price
|
||||
import os
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# Web application
|
||||
|
||||
|
||||
@route("/")
|
||||
@view("search_results")
|
||||
def results_page():
|
||||
query = Price.select().order_by(Price.date.desc())
|
||||
results = list(query)
|
||||
return dict(results=results)
|
||||
|
||||
|
||||
@route("/static/<filename>")
|
||||
def serve_static_file(filename):
|
||||
project_dir = os.path.dirname(__file__)
|
||||
static_root = os.path.join(project_dir, "static")
|
||||
return static_file(filename, root=static_root)
|
||||
|
||||
|
||||
@hook("before_request")
|
||||
def connect_to_db():
|
||||
models.db.connect()
|
||||
|
||||
|
||||
@hook("after_request")
|
||||
def close_db_connection():
|
||||
models.db.close()
|
||||
Reference in New Issue
Block a user