Archive
Jinja2 example for generating a local file using a template
Here I want to show you how to generate an HTML file (a local file) using a template with the Jinja2 template engine.
Python source (proba.py)
#!/usr/bin/env python
import os
from jinja2 import Environment, FileSystemLoader
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(PATH, 'templates')),
trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def create_index_html():
fname = "output.html"
urls = ['http://example.com/1', 'http://example.com/2', 'http://example.com/3']
context = {
'urls': urls
}
#
with open(fname, 'w') as f:
html = render_template('index.html', context)
f.write(html)
def main():
create_index_html()
########################################
if __name__ == "__main__":
main()
Jinja2 template (templates/index.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Proba</title>
</head>
<body>
<center>
<h1>Proba</h1>
<p>{{ urls|length }} links</p>
</center>
<ol align="left">
{% set counter = 0 -%}
{% for url in urls -%}
<li><a href="{{ url }}">{{ url }}</a></li>
{% set counter = counter + 1 -%}
{% endfor -%}
</ol>
</body>
</html>
Resulting output
If you execute proba.py, you will get this output:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Proba</title>
</head>
<body>
<center>
<h1>Proba</h1>
<p>3 links</p>
</center>
<ol align="left">
<li><a href="http://example.com/1">http://example.com/1</a></li>
<li><a href="http://example.com/2">http://example.com/2</a></li>
<li><a href="http://example.com/3">http://example.com/3</a></li>
</ol>
</body>
</html>
You can find all these files here (GitHub link).
Using Go’s present with Python code snippets
A few weeks ago I started to learn Go (aka Golang). I enjoy it very much, I think it’s a very nice and logical language. It is similar to Python in several aspects, so it’s not difficult to start after Python.
If you watch some videos about Go, you will notice that most presentations are made with the same presentation software. And the most interesting part is that you can include Go code snippets in the presentation that you can execute with a button click. The output is also shown. That is, if you want to show some code, you don’t need to jump around between a terminal and the presentation; everything is at one place.
Example: http://www.youtube.com/watch?v=f6kdp27TYZs at 6:57.
Problem
You want to do the same with Python code snippets. You want to use this presentation system and you want to embed runnable Python codes in it.
Solution
These presentations are done with the Go package called present. Here I won’t explain how to set up Go on your machine, I leave it as an exercise (link1, link2). Once you have Go installed on your machine, install the present package:
$ go get code.google.com/p/go.talks/present
The good news is that present is prepared to run scripts (whose first line starts with a shebang). The file that is responsible for executing Go programs and eventually Python scripts is here: $GOPATH/src/code.google.com/p/go.tools/playground/socket/socket.go.
However, I found a bug in socket.go (reported here). The last line of the function shebang() must be changed (Update 20140227: my patch has been integrated since then, so if you install the present package, you’ll have the correct version):
old (buggy):
return fs[0], fs[1:]
new (corrected):
return fs[0], fs
After this compile present. Enter the directory $GOPATH/src/code.google.com/p/go.talks/present and execute the command “go install“. It will place the binary “present” in the folder $GOPATH/bin.
Try it out
I put a simple demo presentation here: https://github.com/jabbalaci/go-present-for-python . Download the files and launch “present” in the directory where the downloaded files are. Visit http://127.0.0.1:3999/ and check out the demo that contains a Python and a Go code snippet. Don’t forget to click on the “Run” buttons :)
Update (20140215) — convert presentation to PDF
You can convert your presentation to PDF easily. Navigate to the first slide in Firefox (I prefer this browser) and go to File -> Print…. Select “print to file”, change the page orientation to “landscape” and hide all the headers and footers (blank). Firefox will print every slide, each slide on a new page.
Update (20140218)
This blog post got included in Go Newsletter Issue #21 (Feb. 2014). Awesome!
Update (20140331)
Right now I’m at the Write the Docs conference in Budapest, Hungary. Today I gave a lightning talk with the title “Using Go’s present with Python code snippets”. The presentation converted to PDF is here.
DARPA Open Catalog
The Defense Advanced Research Projects Agency (DARPA) published its Open Catalog, a website that collects a curated list of DARPA-sponsored software and peer-reviewed publications.
The list contains several Python projects.
MoviePy: script-based movie editing
I haven’t tried it yet but it looks awesome.
“MoviePy is a Python module for script-based movie editing, which enables basic operations (cuts, concatenations, title insertions) to be done in a few lines. It can also be used for advanced compositing and special effects.”
Example: putting some clips together:
import os
from moviepy.editor import *
files = sorted( os.listdir("clips/") )
clips = [ VideoFileClip('clips/%s'%f) for f in files]
video = concatenate(clips, transition = VideoFileClip("logo.avi"))
video.to_videofile("demos.avi",fps=25, codec="mpeg4")
The author of MoviePy shows how to manipulate GIF files with MoviePy: http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python/.
jpegtran-cffi: fast JPEG transformations
I haven’t tried it yet but it seems perfect for creating thumbnails for instance for a collection of images.
jpegtran-cffi has a very intuitive interface. Examples:
from jpegtran import JPEGImage
img = JPEGImage('image.jpg')
# Dimensions
print img.width, img.height # "640 480"
# Transforming the image
img.scale(320, 240).save('scaled.jpg')
img.rotate(90).save('rotated.jpg')
img.crop(0, 0, 100, 100).save('cropped.jpg')
# Transformations can be chained
data = (img.scale(320, 240)
.rotate(90)
.flip('horizontal')
.as_blob())
It looks nice, worth checking out.
Fabric
I haven’t used Fabric yet, but I heard a lot about it. This post is a reminder for me to check it out once.
“Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.”
See this post for a concrete example: Deploying Python Apps with Fabric.
Another link with examples: Systems Administration with Fabric.
Related projects
pip-tools
“A set of two command line tools (pip-review + pip-dump) to help you keep your pip-based packages fresh, even when you’ve pinned them.
pip-review checks PyPI and reports available updates. It uses the list of currently installed packages to check for updates, it does not use any requirements.txt.
pip-dump dumps the exact versions of installed packages in your active environment to your requirements.txt file.”
I haven’t used it yet, so this post a reminder for me. I think I will need it soon.
faker: generate fake data
“Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.”
Discussion @reddit.
Faker is a hot project at the moment of writing with lots of new ideas and reported issues. If you want to contribute, do it now :)
PyCon 2014 talk schedule
PyCon 2014 talk schedule is online: https://us.pycon.org/2014/schedule/talks/.
Each talk has an abstract, so you can peek into the future to see the hot topics in 2014.
PyCon 2014 will be held in Montreal this year where I spent 4 whole years between January 2008 and October 2011. I wish I could attend PyCon this year :)
News extraction
With newspaper, you can do “news extraction, article extraction and content curation in python. Built with multithreading, 10+ languages, NLP, ML, and more!”
I haven’t tried it yet but if you need a corpus with news articles, this project can help.

You must be logged in to post a comment.