Tangelo: The Beauty of Simplicity

December 18, 2014
This article is part of a series on Tangelo, a Python-supercharged web application framework. The first article, and index, can be found here.

It’s a good time to be developing applications for the web. There are many web application frameworks out there, which means lots of choices, which means it’s more likely that the thing you need exists. Among the sea of choices, why Tangelo? This post explores some reasons you would want Tangelo to host your application and some reasons you might not.

First, a quick disclaimer. If you’re planning to build a traditional web app with a database, models to represent application data, and views to display it, then existing systems such as Ruby on Rails will match up to your project goals better than Tangelo. In our work, we have been looking beyond, toward scientific simulation and visualization, as well as other unusual applications that must pull together data from many unconventional sources, using experimental technology to process and visualize it.

To that end, we have adopted a bold idea: Python scripts should be considered a first-class citizen alongside HTML, JavaScript, and CSS files, adding their voices and the richness of the Python ecosystem to your web applications. We have designed Tangelo to deliver this idea as simply as possible, in order to open up web application programming to everyone, including people unfamiliar with terms like “routing,” “content-type header,” and “RESTful.”

Django, Flask, Bottle, and CherryPy are all Python-based web frameworks but they tend to relegate Python itself to second-class status, wrapped up in a bit of red tape. Here’s an example, in a file called bottle-hello.py:

import bottle

app = bottle.Bottle()

def greet(name="Earthling"):
    return "Greetings %s, take me to your leader." % (name)

​bottle.run(app, host="localhost", port=8080)

When run with python bottle-hello.py, this application will serve a bit of dynamic content at http://localhost:8080/hello/Roni, but notice how the simple code to create the content is suspended within a spider web of scaffolding around it. In Tangelo, things are much simpler. In your application directory, creating the following file, named hello.py,

def run(name="Earthling"):
    return "Greetings %s, take me to your leader." % (name)

and then issuing the command tangelo --root . will serve this minimal application, which behaves the same way as the Bottle application but with fewer lines of code and no exposure of any framework API. Tangelo’s internal server logic handles the scaffolding you see in lines 1, 3, and 9, while the names of files and directories on disk determine the routing you see in line 5. All that remains is the heart of the web service, expressed in general, Pythonic terms. When we say “first-class citizen,” this is just what we mean: zero-API creation of Python web services in individual files that are composed almost entirely of application logic.

In the excitement over Python, we shouldn’t forget about the humble static content that serves as the foundation of any web application. Serving static content complicates things a bit further for the Bottle application: it needs to declare new routes, listing files or directories to serve at each one. In Tangelo, all you need to do is include the files on disk, in paths that reflect the desired routing – zero-API static content, for free.

The other frameworks mentioned above look largely similar to the Bottle example – we have showed the Bottle example because the other frameworks are all at least as complicated (some significantly more so).

Much of our motivation in designing Tangelo this way is simplicity, and the subsequent flexibility it begets. For example, thinking of a web application as a bundle of HTML, JavaScript, CSS, and Python files enables reusability.  There are ways to reuse the Python callbacks that make up a Bottle application, but none so simple or flexible as Tangelo's approach of simply handing someone a Python file to drop into their own web application. The question moves from “How do I get route X to do action Y?” to the simpler “How do I do action Y?” This is the same question any Python programmer in any context – not just web programming – wants to be engaged with. In other words, it frees the developer from the mechanics of web servers, thereby abstracting function from form, and leaving our focus on the substance of the problem at hand.

In a later post, we will see some examples of the kinds of things Tangelo can do to solve problems on the web.

Leave a Reply