from flask import Flask from flask import render_template import json import random import re app = Flask(__name__) with open("./data/beers.json") as beers_json: beer = json.loads(beers_json.read()) @app.route('/') def index(): return render_template('Homepage.jinja2', suggestions=all_beer_names()) @app.route('/random') def random_beer(): rando = random.choice(list(beer.keys())) return render_template('random_beer.jinja2', beer=beer[rando]) @app.route('/similar', methods=['POST']) def similar(): return render_template('similar.jinja2', beers=[]) @app.route('/brewery//') def get_brewery_list(brewery_id): return json.dumps(find_beers_from_brewery(brewery_id)) @app.route('/brewery//') def get_beer_details(brewery_id, beer_id): beer_result = beer_lookup(brewery_id,beer_id) return render_template('beerpage.jinja2', beer_data=beer_result) def beer_lookup(brewery_id, beer_id): item_id = brewery_id + "-" + beer_id try: beer_result = beer[item_id] return beer_result except: return None def all_beer_names(): return [value[0] for key, value in beer.items()] def find_beers_from_brewery(brewery_id): pattern = re.compile('^' + brewery_id + '-.*') beers = [value for key, value in beer.items() if pattern.match(key)] return beers if __name__ == '__main__': app.run() class BeerInfo(object): def __init__(self, item_json): self.parent_style = item_json[1][1] self.base_style = item_json[1][0] self.name = item_json[0] self.abv = item_json[2] class Brewery(object): def __init__(self, id): self.id = id self.brews = [] def add_brew(self, brew): self.brews.append(brew)