Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- app = Flask(__name__)
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- from database_setup import Base, Restaurant, MenuItem
- engine = create_engine('sqlite:///restaurantmenu.db')
- Base.metadata.bind = engine
- DBSession = sessionmaker(bind = engine)
- session = DBSession()
- @app.route('/')
- @app.route('/hola')
- def hello_world():
- restaurants_list = session.query(Restaurant).all()
- output = ''
- for restaurant in restaurants_list:
- items_list = session.query(MenuItem).filter_by(restaurant_id =
- restaurant.id)
- output += '<h2>{}</h2>'.format(restaurant.name)
- for item in items_list:
- output += item.name
- output += '</br>'
- output += item.price
- output += '</br>'
- output += item.description
- output += '</br>'
- output += '</br>'
- return output
- # configuration
- import sys
- from sqlalchemy import Column, ForeignKey, Integer, String
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import relationship
- from sqlalchemy import create_engine
- Base = declarative_base()
- # classes (represent the tables within the database)
- class Restaurant(Base):
- # table information (creates the tables)
- __tablename__ = 'restaurant'
- # mapper code (creates variables that we'll use to create columns within the tables)
- name = Column(String(80), nullable = False)
- id = Column(Integer, primary_key = True)
- class MenuItem(Base):
- __tablename__ = 'menu_item'
- name = Column(String(80), nullable = False)
- id = Column(Integer, primary_key = True)
- course = Column(String(250))
- description = Column(String(250))
- price = Column(String(8))
- restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
- restaurant = relationship(Restaurant)
- # configuration
- engine = create_engine('sqlite:///restaurantmenu.db')
- Base.metadata.create_all(engine)
Add Comment
Please, Sign In to add comment