From f686b85a1c9476d76d1192d8c8dbcbbfb2eec50e Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sat, 9 Feb 2013 19:40:38 -0500 Subject: [PATCH 1/6] add simple Jinja2 template renderer and squares --- app/templates/render.py | 60 +++++++++++++++++++++++++++++++ app/templates/squares.jinja2.html | 12 +++++++ 2 files changed, 72 insertions(+) create mode 100644 app/templates/render.py create mode 100644 app/templates/squares.jinja2.html diff --git a/app/templates/render.py b/app/templates/render.py new file mode 100644 index 0000000..e0b28f2 --- /dev/null +++ b/app/templates/render.py @@ -0,0 +1,60 @@ +# Global list of available format parsers on your system +# mapped to the callable/Exception to parse a string into a dict +formats = {} + +class MalformedJSON(Exception): pass + +# json - simplejson or packaged json as a fallback +try: + import simplejson + formats['json'] = (simplejson.loads, simplejson.decoder.JSONDecodeError, MalformedJSON) +except ImportError: + try: + import json + formats['json'] = (json.loads, ValueError, MalformedJSON) + except ImportError: + pass + +import os +import sys +from optparse import OptionParser + +from jinja2 import Environment, FileSystemLoader + +def cli(opts, args): + if args[1] == '-': + data = sys.stdin.read() + else: + data = open(os.path.join(os.getcwd(), os.path.expanduser(args[1]))).read() + + try: + data = formats['json'][0](data) + except formats['json'][1]: + raise formats['json'][2](u'%s ...' % data[:60]) + sys.exit(1) + + env = Environment(loader=FileSystemLoader(os.getcwd())) + sys.stdout.write(env.get_template(args[0]).render(data)) + sys.exit(0) + + +def main(): + default_format = 'json' + if default_format not in formats: + default_format = sorted(formats.keys())[0] + + parser = OptionParser(usage="usage: %prog [options] ") + opts, args = parser.parse_args() + + if len(args) == 0: + parser.print_help() + sys.exit(1) + + # Without the second argv, assume they want to read from stdin + if len(args) == 1: + args.append('-') + + cli(opts, args) + +if __name__ == "__main__": + main() diff --git a/app/templates/squares.jinja2.html b/app/templates/squares.jinja2.html new file mode 100644 index 0000000..c56d1f5 --- /dev/null +++ b/app/templates/squares.jinja2.html @@ -0,0 +1,12 @@ + + + + + + {%- for item in rows %} + + + + + {%- endfor %} +
NumberSquare
{{ item.number }}{{ item.square }}
From 07f001b9744214b2977ee2d79f21eebf9791f078 Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sat, 9 Feb 2013 23:04:02 -0500 Subject: [PATCH 2/6] fix up rapid module, add git ignore --- .gitignore | 1 + app/rapid.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f356293..5040a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ lib-cov *.out *.pid *.gz +*.pyc pids logs diff --git a/app/rapid.py b/app/rapid.py index a7bc070..6e89c06 100644 --- a/app/rapid.py +++ b/app/rapid.py @@ -1,8 +1,8 @@ -def query_articles(): +def top_articles(): return [] -def search_articles(): +def search_articles(query): return [] -def insert_article(): +def insert_article(article): return False From 22e48fd831c1fcc8f1919e7ce1ff2f13d6fdad70 Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sat, 9 Feb 2013 23:04:58 -0500 Subject: [PATCH 3/6] upgrade serve script to livereload --- serve.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serve.sh b/serve.sh index f5caf94..1afe9e2 100755 --- a/serve.sh +++ b/serve.sh @@ -1,2 +1,2 @@ cd static -python -m SimpleHTTPServer +livereload -p 8000 From 4c6c559416b5fa1c0817f05651293aeb30290da2 Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sun, 10 Feb 2013 10:47:45 -0500 Subject: [PATCH 4/6] implement static code generation for simple tmpls jinja2 and template inheritance demonstrated --- app/templates/data.json | 1 + app/templates/index.jinja2.html | 20 ++++++++++++++++++++ app/templates/layout.jinja2.html | 10 ++++++++++ app/templates/squares.jinja2.html | 12 ------------ app/templates/submit.jinja2.html | 10 ++++++++++ 5 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 app/templates/data.json create mode 100644 app/templates/index.jinja2.html create mode 100644 app/templates/layout.jinja2.html delete mode 100644 app/templates/squares.jinja2.html create mode 100644 app/templates/submit.jinja2.html diff --git a/app/templates/data.json b/app/templates/data.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/app/templates/data.json @@ -0,0 +1 @@ +{} diff --git a/app/templates/index.jinja2.html b/app/templates/index.jinja2.html new file mode 100644 index 0000000..392c3f1 --- /dev/null +++ b/app/templates/index.jinja2.html @@ -0,0 +1,20 @@ +{% extends 'layout.jinja2.html' %} +{% block title %}Latest News{% endblock %} +{% block body %} +
+ + + + + + + + + + + + + + +
ScoreLinkPublished
+{% endblock %} diff --git a/app/templates/layout.jinja2.html b/app/templates/layout.jinja2.html new file mode 100644 index 0000000..1988bd7 --- /dev/null +++ b/app/templates/layout.jinja2.html @@ -0,0 +1,10 @@ + + + {% block title %}{% endblock %} + + + {% block body %} + {% endblock %} + + + diff --git a/app/templates/squares.jinja2.html b/app/templates/squares.jinja2.html deleted file mode 100644 index c56d1f5..0000000 --- a/app/templates/squares.jinja2.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - {%- for item in rows %} - - - - - {%- endfor %} -
NumberSquare
{{ item.number }}{{ item.square }}
diff --git a/app/templates/submit.jinja2.html b/app/templates/submit.jinja2.html new file mode 100644 index 0000000..6701c56 --- /dev/null +++ b/app/templates/submit.jinja2.html @@ -0,0 +1,10 @@ +{% extends 'layout.jinja2.html' %} +{% block title %}Submit News{% endblock %} +{% block body %} + +
+

Some Form.

+
+ +{% endblock %} + From 2bc63fd432f591e9442990a5ae4325e741d8dc5e Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sun, 10 Feb 2013 11:29:52 -0500 Subject: [PATCH 5/6] add simple Flask templating example --- app/app.py | 4 ++-- app/simple.py | 18 ++++++++++++++++++ app/templates/index.jinja2.html | 12 +++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 app/simple.py diff --git a/app/app.py b/app/app.py index f2aa540..4700fe2 100644 --- a/app/app.py +++ b/app/app.py @@ -1,11 +1,11 @@ from flask import Flask, render_template, request -from rapid import (query_articles, search_articles, insert_article) +from rapid import (top_articles, search_articles, insert_article) app = Flask(__name__) @app.route('/') def index(): - articles = query_articles() + articles = top_articles() return render_template('index.jinja2.html', articles=articles) diff --git a/app/simple.py b/app/simple.py new file mode 100644 index 0000000..b0d6248 --- /dev/null +++ b/app/simple.py @@ -0,0 +1,18 @@ +from flask import render_template, Flask +app = Flask(__name__) + +def top_articles(): + articles = [ + {"title": "Google", "score": 150, "link": "http://google.com"}, + {"title": "Yahoo", "score": 75, "link": "http://yahoo.com"}, + {"title": "Bing", "score": 50, "link": "http://bing.com"} + ] + return articles + +@app.route('/') +def index(): + articles = top_articles() + return render_template("index.jinja2.html", rows=articles) + +if __name__ == "__main__": + app.run(debug=True) diff --git a/app/templates/index.jinja2.html b/app/templates/index.jinja2.html index 392c3f1..582fe0e 100644 --- a/app/templates/index.jinja2.html +++ b/app/templates/index.jinja2.html @@ -10,11 +10,13 @@ - - - - - + {% for row in rows %} + + + + + + {% endfor %}
{{ row.score }}{{ row.title }}just now
{% endblock %} From d98a5b16d0a8ba710f9d9d3ac603a62dac5847dd Mon Sep 17 00:00:00 2001 From: Andrew Montalenti Date: Sun, 10 Feb 2013 13:07:16 -0500 Subject: [PATCH 6/6] sample article data --- app/templates/articles.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 app/templates/articles.json diff --git a/app/templates/articles.json b/app/templates/articles.json new file mode 100644 index 0000000..f22f3ce --- /dev/null +++ b/app/templates/articles.json @@ -0,0 +1,6 @@ +{ "rows": [ + {"title": "Google", "score": 150, "link": "http://google.com"}, + {"title": "Yahoo", "score": 75, "link": "http://yahoo.com"}, + {"title": "Bing", "score": 50, "link": "http://bing.com"} + ] +}