-
Notifications
You must be signed in to change notification settings - Fork 45
/
simple_app.py
58 lines (41 loc) · 1.43 KB
/
simple_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask_table import Table, Col, LinkCol
from flask import Flask
"""A example for creating a simple table within a working Flask app.
Our table has just two columns, one of which shows the name and is a
link to the item's page. The other shows the description.
"""
app = Flask(__name__)
class ItemTable(Table):
name = LinkCol('Name', 'single_item',
url_kwargs=dict(id='id'), attr='name')
description = Col('Description')
@app.route('/')
def index():
items = Item.get_elements()
table = ItemTable(items)
# You would usually want to pass this out to a template with
# render_template.
return table.__html__()
@app.route('/item/<int:id>')
def single_item(id):
element = Item.get_element_by_id(id)
# Similarly, normally you would use render_template
return '<h1>{}</h1><p>{}</p><hr><small>id: {}</small>'.format(
element.name, element.description, element.id)
class Item(object):
""" a little fake database """
def __init__(self, id, name, description):
self.id = id
self.name = name
self.description = description
@classmethod
def get_elements(cls):
return [
Item(1, 'Z', 'zzzzz'),
Item(2, 'K', 'aaaaa'),
Item(3, 'B', 'bbbbb')]
@classmethod
def get_element_by_id(cls, id):
return [i for i in cls.get_elements() if i.id == id][0]
if __name__ == '__main__':
app.run(debug=True)