Navigation

Welcome to the Werkzeug tutorial in which we will create a TinyURL clone
that stores URLs in a redis instance. The libraries we will use for this
applications are Jinja 2 for the templates, redis for the database
layer and, of course, Werkzeug for the WSGI layer.

You can use pip to install the required libraries:

pipinstallJinja2redisWerkzeug

Also make sure to have a redis server running on your local machine. If
you are on OS X, you can use brew to install it:

brewinstallredis

If you are on Ubuntu or Debian, you can use apt-get:

sudoapt-getinstallredis-server

Redis was developed for UNIX systems and was never really designed to
work on Windows. For development purposes, the unofficial ports however
work well enough. You can get them from github.

In this tutorial, we will together create a simple URL shortener service
with Werkzeug. Please keep in mind that Werkzeug is not a framework, it’s
a library with utilities to create your own framework or application and
as such is very flexible. The approach we use here is just one of many you
can use.

As data store, we will use redis here instead of a relational database
to keep this simple and because that’s the kind of job that redis
excels at.

Werkzeug is a utility library for WSGI. WSGI itself is a protocol or
convention that ensures that your web application can speak with the
webserver and more importantly that web applications work nicely together.

A basic “Hello World” application in WSGI without the help of Werkzeug
looks like this:

A WSGI application is something you can call and pass an environ dict
and a start_response callable. The environ contains all incoming
information, the start_response function can be used to indicate the
start of the response. With Werkzeug you don’t have to deal directly with
either as request and response objects are provided to work with them.

The request data takes the environ object and allows you to access the
data from that environ in a nice manner. The response object is a WSGI
application in itself and provides a much nicer way to create responses.

Before we get started, let’s create the folders needed for this application:

/shortly/static/templates

The shortly folder is not a python package, but just something where we
drop our files. Directly into this folder we will then put our main
module in the following steps. The files inside the static folder are
available to users of the application via HTTP. This is the place where
CSS and JavaScript files go. Inside the templates folder we will make
Jinja2 look for templates. The templates you create later in the tutorial
will go in this directory.

Now let’s get right into it and create a module for our application. Let’s
create a file called shortly.py in the shortly folder. At first we
will need a bunch of imports. I will pull in all the imports here, even
if they are not used right away, to keep it from being confusing:

Then we can create the basic structure for our application and a function
to create a new instance of it, optionally with a piece of WSGI middleware
that exports all the files on the static folder on the web:

The basic idea here is that our Shortly class is an actual WSGI
application. The __call__ method directly dispatches to wsgi_app.
This is done so that we can wrap wsgi_app to apply middlewares like we
do in the create_app function. The actual wsgi_app method then
creates a Request object and calls the dispatch_request
method which then has to return a Response object which is then
evaluated as WSGI application again. As you can see: turtles all the way
down. Both the Shortly class we create, as well as any request object
in Werkzeug implements the WSGI interface. As a result of that you could
even return another WSGI application from the dispatch_request method.

The create_app factory function can be used to create a new instance
of our application. Not only will it pass some parameters as
configuration to the application but also optionally add a WSGI middleware
that exports static files. This way we have access to the files from the
static folder even when we are not configuring our server to provide them
which is very helpful for development.

Now that we have the basic application class, we can make the constructor
do something useful and provide a few helpers on there that can come in
handy. We will need to be able to render templates and connect to redis,
so let’s extend the class a bit:

Next up is routing. Routing is the process of matching and parsing the URL to
something we can use. Werkzeug provides a flexible integrated routing
system which we can use for that. The way it works is that you create a
Map instance and add a bunch of
Rule objects. Each rule has a pattern it will
try to match the URL against and an “endpoint”. The endpoint is typically
a string and can be used to uniquely identify the URL. We could also use
this to automatically reverse the URL, but that’s not what we will do in this
tutorial.

Here we create a URL map with three rules. / for the root of the URL
space where we will just dispatch to a function that implements the logic
to create a new URL. And then one that follows the short link to the
target URL and another one with the same rule but a plus (+) at the
end to show the link details.

So how do we find our way from the endpoint to a function? That’s up to you.
The way we will do it in this tutorial is by calling the method on_
+ endpoint on the class itself. Here is how this works:

We bind the URL map to the current environment and get back a
URLAdapter. The adapter can be used to match
the request but also to reverse URLs. The match method will return the
endpoint and a dictionary of values in the URL. For instance the rule for
follow_short_link has a variable part called short_id. When we go
to http://localhost:5000/foo we will get the following values back:

endpoint='follow_short_link'values={'short_id':u'foo'}

If it does not match anything, it will raise a
NotFound exception, which is an
HTTPException. All HTTP exceptions are also
WSGI applications by themselves which render a default error page. So we
just catch all of them down and return the error itself.

If all works well, we call the function on_ + endpoint and pass it the
request as argument as well as all the URL arguments as keyword arguments
and return the response object that method returns.

This logic should be easy to understand. Basically we are checking that
the request method is POST, in which case we validate the URL and add a
new entry to the database, then redirect to the detail page. This means
we need to write a function and a helper method. For URL validation this
is good enough:

reverse-url: + the URL will store the short id. If the URL was
already submitted this won’t be None and we can just return that value
which will be the short ID. Otherwise we increment the last-url-id
key and convert it to base36. Then we store the link and the reverse
entry in redis. And here the function to convert to base 36:

The link detail view is very similar, we just render a template
again. In addition to looking up the target, we also ask redis for the
number of times the link was clicked and let it default to zero if such
a key does not yet exist:

And here are all the templates. Just drop them into the templates
folder. Jinja2 supports template inheritance, so the first thing we will
do is create a layout template with blocks that act as placeholders. We
also set up Jinja2 so that it automatically escapes strings with HTML
rules, so we don’t have to spend time on that ourselves. This prevents
XSS attacks and rendering errors.

layout.html:

<!doctype html><title>{%blocktitle%}{%endblock%} | shortly</title><linkrel=stylesheethref=/static/style.csstype=text/css><divclass=box><h1><ahref=/>shortly</a></h1><pclass=tagline>Shortly is a URL shortener written with Werkzeug
{%blockbody%}{%endblock%}</div>

new_url.html:

{%extends"layout.html"%}{%blocktitle%}Create New Short URL{%endblock%}{%blockbody%}<h2>Submit URL</h2><formaction=""method=post>{%iferror%}<pclass=error><strong>Error:</strong>{{error}}{%endif%}<p>URL:
<inputtype=textname=urlvalue="{{url}}"class=urlinput><inputtype=submitvalue="Shorten"></form>{%endblock%}