Skip to content

Instantly share code, notes, and snippets.

@jstacoder
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstacoder/822525ba610f1d88d557 to your computer and use it in GitHub Desktop.
Save jstacoder/822525ba610f1d88d557 to your computer and use it in GitHub Desktop.
programatic page generation with flask-htmlbuilder
#!/usr/bin/python
try:
from htmlbuilder import html
except ImportError:
import sys
print '\n'.join(sys.path)
sys.exit()
from flask import Flask
add_styles = (
('.long-text',['overflow-wrap:break-word']),
('.footer',['background-color:white']),
('body',['padding-bottom:70px']),
)
def filter_false(x):
return x != False
def fmt_rule(rule):
return '\t{};\n'.format(rule)
def get_style(selector,rules):
style = '\n%s {\n' % selector
end = '}\n'
tmp = ''
for rule in rules:
tmp += fmt_rule(rule)
return style + tmp + end
def add_style_tag(styles=None):
if styles is None:
styles = add_styles
if len(add_styles) == 1:
if len(add_styles[0]) == 0:
return ''
_styles = ''
for selector,rules in styles:
_styles += get_style(selector,rules)
return html.style()(
_styles
)
def collect(indent,*args):
rtn = ''
for itm in args:
rtn += itm.render(indent)
return rtn
def make_list_group_item(item,link=False,active=False):
cls = 'list-group-item'
if active:
cls = cls + ' active'
if not link:
rtn = html.span(class_=cls)(
item
)
else:
rtn = html.a(class_=cls)(
item
)
return rtn
def make_nav_item(item,active=False):
if active:
rtn = html.li(class_='active')(
html.a(href=item.url)(
item.text
)
)
else:
rtn = html.li()(
html.a(href=item.url)(
item.text
)
)
return rtn
def make_navbar(items=[]):
rtn = html.nav(class_='navbar navbar-default',role='navigation')(
html.div(class_='container-fluid')(
html.div(class_='navbar-header')(
html.button(**{'type':'button','class_':'navbar-toggle collapsed','data-toggle':'collapse',
'data-target':'#bs-collapse'})(
html.span(class_='sr-only')('toggle navigation'),
html.span(class_='icon-bar')(),
html.span(class_='icon-bar')(),
html.span(class_='icon-bar')(),
),
html.a(class_='navbar-brand',href='#')('Brand')
),
html.div(class_='collapse navbar-collapse',id_='bs-collapse')(
html.ul(class_='nav navbar-nav')(
html.li(class_='active')(
html.a(href='#')(
'Link'
)
),
html.li()(
html.a(href='#')(
'Link'
)
),
html.li()(
html.a(href='#')(
'Link'
)
),
html.li(class_='dropdown')(
html.a(**{
'class_':'dropdown-toggle','data-toggle':'dropdown',
'role':'button','aria-expanded':'false'
})(
'Dropdown',
html.span(class_='caret')()
),
html.ul(class_='dropdown-menu',role='menu')(
html.li()(
html.a(href='#')(
'Action'
)
),
html.li()(
html.a(href='#')(
'Action'
)
),
html.li(class_='divider')(),
html.li()(
html.a(href='#')(
'Action'
)
),
html.li(class_='divider')(),
html.li()(
html.a(href='#')(
'Action'
)
),
)
)
),
html.form(class_='navbar-form navbar-left',role='search')(
html.div(class_='form-group')(
html.input(**{
'type':'text','class_':'form-control',
'placeholder':'search'
})
)
),
html.ul(class_='nav navbar-nav navbar-right')(
html.li()(
html.a(href='#')(
'Link'
)
),
)
)
)
)
return rtn
def make_list_group(items):
head = make_list_group_item(items.pop(0),True,True)
itms = [make_list_group_item(x,True) for x in items]
itms.insert(0,head)
return html.div(class_='list-group')(
itms
)
def make_panel(content,title=False,footer=False):
head = False
if title:
head = html.div(class_='panel-heading')(
html.h3(class_='panel-title')(title)
)
body = html.div(class_='panel-body')(
html.p()(
content
)
)
foot = False
if footer:
foot = html.div(class_='panel-footer')(
footer
)
return html.div(class_='panel panel-default')(
filter(filter_false,[head,body,foot])
)
def get_icon(lib,name):
cls = '{0} {0}-{1}'.format(lib,name)
return html.span(class_=cls)()
def make_footer():
return html.div(class_='footer navbar nav navbar-fixed-bottom')(
html.div(class_='container-fluid')(
html.hr(),
html.div(class_='row')(
html.div(class_='col-md-12')(
html.p()(
get_icon('glyphicon','copyright-mark'),
'2014'
)
)
)
)
)
def get_col(*args,**kwargs):
rtn = []
content = kwargs.pop('content','')
fmt = 'col-{size}-{num} '
if 'offset' in kwargs:
offset = kwargs.pop('offset')
extra_fmt = ' col-{size}-offset-{num} '
fmt += extra_fmt
else:
offset = False
if 'push' in kwargs:
push = kwargs.pop('push')
extra_fmt = ' col-{size}-push-{num} '
fmt += extra_fmt
else:
push = False
if 'pull' in kwargs:
pull = kwargs.pop('pull')
extra_fmt = ' col-{size}-pull-{num} '
fmt += extra_fmt
else:
pull = False
class_ = ''
for size,num in kwargs.items():
class_ += fmt.format(size=size,num=num)
return html.div(class_=class_.strip())(
content
)
def get_head():
return (
html.link(
rel='stylesheet',
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"),
html.link(
rel='stylesheet',
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.theme.min.css"),
html.script(src="https://code.jquery.com/jquery.min.js")(),
html.script(src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js")(),
)
def main():
app = Flask(__name__)
page = html.html()(
html.head()(
get_head(),
add_style_tag()
),
html.body(
make_navbar(),
html.div(
class_='container-fluid'
)(
html.div(class_='row')(
get_col(md=12,sm=12,xs=12,
content=\
html.div(
class_='page-header'
)(
html.h1()("my title")
)
)
),
html.div(class_='row')(
get_col(md=9,sm=12,xs=12,
content=[
html.p(class_='lead')(
'Content Title'
),
html.div(class_='row')(
get_col(md=3,sm=3,xs=6,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
get_col(md=3,sm=3,xs=6,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
get_col(md=3,sm=3,xs=6,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
get_col(md=3,sm=3,xs=6,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
),
html.div(class_='row')(
get_col(md=4,sm=4,xs=4,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
get_col(md=4,sm=4,xs=4,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
get_col(md=4,sm=4,xs=4,
content=\
make_panel(
html.p()('main content'),
title='title2',
),
),
),
html.div(class_='row')(
html.div(class_='col-xs-6 col-md-6')(
make_panel(
html.p()('main content'),
title='main title',
),
),
html.div(class_='col-xs-6 col-md-6')(
make_panel(
html.p()('main content'),
title='title2',
),
)
),
html.div(class_='row')(
html.div(class_='col-md-12 col-sm-12 col-xs-12')(
make_panel(
html.p(class_='long-text')(
'main contentkjdfjvhdfjk;vfnlkj hnfjnljk hcvnklj' +
'bhcvlk jhl kjhn jnlfjhfnlkjbhfblkjxfhblxfjkbhflxkj' +
'bhxflbjfhlfjkhfldjkhxflk bhxdfl kbhflkbjxhlbkjxdfh' +
'bxfklcjhxklbhxcflkjxhlbkjxhblxjkh bxlfkj xlkj ghxd' +
'fljk bhxdfklj xfhjkl bhxklj bxhkl bjhxl; bfjkh bklxf '
),
title='main title',
),
),
),
]
),
html.div(class_='col-md-3')(
html.div(class_='row')(
html.div(class_='col-md-12')(
html.p()('sidebar'),
make_list_group(['item1','item2','item3']),
make_list_group(['item1','item2','item3']),
make_list_group(['item1','item2','item3']),
make_list_group(['item1','item2','item3'])
)
)
)
)
)
),
make_footer(),
)
@app.route('/')
def index():
return page.render(4)
return app
if __name__ == '__main__':
main().run(host='0.0.0.0',port=9090)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment