Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. from flask import Flask
  2. app = Flask(__name__)
  3.  
  4. from sqlalchemy import create_engine
  5. from sqlalchemy.orm import sessionmaker
  6. from database_setup import Base, Restaurant, MenuItem
  7.  
  8. engine = create_engine('sqlite:///restaurantmenu.db')
  9. Base.metadata.bind = engine
  10. DBSession = sessionmaker(bind = engine)
  11. session = DBSession()
  12.  
  13. @app.route('/')
  14. @app.route('/hola')
  15. def hello_world():
  16. restaurants_list = session.query(Restaurant).all()
  17. output = ''
  18. for restaurant in restaurants_list:
  19. items_list = session.query(MenuItem).filter_by(restaurant_id =
  20. restaurant.id)
  21. output += '<h2>{}</h2>'.format(restaurant.name)
  22. for item in items_list:
  23. output += item.name
  24. output += '</br>'
  25. output += item.price
  26. output += '</br>'
  27. output += item.description
  28. output += '</br>'
  29. output += '</br>'
  30. return output
  31.  
  32. # configuration
  33. import sys
  34. from sqlalchemy import Column, ForeignKey, Integer, String
  35. from sqlalchemy.ext.declarative import declarative_base
  36. from sqlalchemy.orm import relationship
  37. from sqlalchemy import create_engine
  38.  
  39. Base = declarative_base()
  40.  
  41. # classes (represent the tables within the database)
  42. class Restaurant(Base):
  43.  
  44. # table information (creates the tables)
  45. __tablename__ = 'restaurant'
  46.  
  47. # mapper code (creates variables that we'll use to create columns within the tables)
  48. name = Column(String(80), nullable = False)
  49. id = Column(Integer, primary_key = True)
  50.  
  51. class MenuItem(Base):
  52.  
  53. __tablename__ = 'menu_item'
  54.  
  55. name = Column(String(80), nullable = False)
  56. id = Column(Integer, primary_key = True)
  57. course = Column(String(250))
  58. description = Column(String(250))
  59. price = Column(String(8))
  60. restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
  61. restaurant = relationship(Restaurant)
  62.  
  63. # configuration
  64. engine = create_engine('sqlite:///restaurantmenu.db')
  65. Base.metadata.create_all(engine)
Add Comment
Please, Sign In to add comment