from flask import Flask from flask import render_template import json import random import re app = Flask(__name__) with open("./static/beers.json") as beers_json: beer = json.loads(beers_json.read()) @app.route('/') def hello_world(): hello_beer = random.choice(list(beer.keys())) return render_template('Homepage.jinja2', item=beer[hello_beer]) @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 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)