''' Created on Dec 31, 2014 @author: Brett Paufler Copyright Brett Paufler Basic Template for an HTML Webpage Used by: page_color_wheel page_image_rendered_as_table ''' #import jinja2 from jinja2 import Template def assemble_head(headerDict={}, head=''): '''returns a formatted head string given a headerDict with optional values: head_first, title, script, style, head_last ''' #CAN EXTEND BY EXPANDING FORMATTING SLOTS if head == '': head = ''' {{head_first}} {{title}} {{head_last}} ''' head = head.replace(" ", "") return Template(head).render(headerDict) def assemble_body(bodyDict={}, body=''): '''same idea as assemble_head only for the main body of the html bodyDict takes optional values: header, main, footer ''' #CAN EXTEND BY EXPANDING FORMATTING SLOTS if body == '': body = ''' {{header}} {{main}} {{footer}} ''' body = body.replace(" ", "") return Template(body).render(bodyDict) def assemble_page(head, body, sN="base_test.html", html=''): '''outputs a completed html w ''' #CAN EXTEND BY EXPANDING FORMATTING SLOTS if html == '': html = ''' {{head}} {{body}} ''' html = html.replace(" ", "") page = Template(html).render(head=head, body=body) with open(sN, 'w') as f: f.write(page) print page print "\n\nHTML SAVED as %s" % sN return page def default_footer(copy='',terms=False, home_page=False): ''' ''' text = '''
{{copy}} {% if home_page %}
www.Paufler.net

{% endif %} Brett@Paufler.net
© Copyright 2015 Brett Paufler

{% if terms %}
Terms of Service
{% endif %}
''' text = text.replace(" ", "") return Template(text).render(copy=copy, terms=terms) def default_header(title='Page Title',tag_line=''): text = '''

{{title}}

{{tag_line}}
''' text = text.replace(" ", "") return Template(text).render(title=title, tag_line=tag_line) def base_page(body_dictionary={}, head_dictionary={}, sN="base.html"): '''This relies on Assemble page, so if that breaks, this breaks Sample Dictionaries below. Given a body_dictionary head_dictionary prints, saves, and returns a base webpage ''' if not head_dictionary: head_dictionary = {'head_first':'', 'title': "Brett's Test Page", 'script': "", 'style':"", 'head_last':'', } if not body_dictionary: body_dictionary = {'header':default_header('Test Page', "Isn't Python Great"), 'main':"

Something should go here.

", 'footer':default_footer('
Footer Insert
',True), } h = assemble_head(head_dictionary) b = assemble_body(body_dictionary) p = assemble_page(h, b, sN) #with open(sN,'w') as f: # f.write(p) #print p return p if __name__ == "__main__": base_page(body_dictionary={}, head_dictionary={})