Introduction This post is the first in what I hope will be a series on neat things you can do with Morepath. Morepath is a Python web micro framework with some very interesting capabilities. What we'll look at today is what you can do with Morepath's link generation in a server-driven web application. While Morepath is an excellent fit to create REST APIs, it also works well server aplications. So let's look at how Morepath can help you to create a batching UI. On the special occasion of this post we also released a new version of Morepath, Morepath 0.11.1! A batching UI is a UI where you have a larger amount of data available than you want to show to the user at once. You instead partition the data in smaller batches, and you let the user navigate through these batches by clicking a previous and next link. If you have 56 items in total and the batch size is 10, you first see items 0-9. You can then click next to see items 10-19, then items 20-29, and so on until you see the last few items 50-55. Clicking previous will take you backwards again. In this example, a URL to see a single batch looks like this: http://example.com/?start=20 To see items 20-29. You can also approach the application like this: http://example.com/ to start at the first batch. I'm going to highlight the relevant parts of the application here. The complete example project can be found on Github. I have included instructions on how to install the app in the README.rst there.

Model First we need to define a few model classes to define the application. We are going to go for a fake database of fake persons that we want to batch through. Here's the Person class: class Person ( object ): def __init__ ( self , id , name , address , email ): self . id = id self . name = name self . address = address self . email = email We use the neat fake-factory package to create some fake data for our fake database; the fake database is just a Python list: fake = Faker () def generate_random_person ( id ): return Person ( id , fake . name (), fake . address (), fake . email ()) def generate_random_persons ( amount ): return [ generate_random_person ( id ) for id in range ( amount )] person_db = generate_random_persons ( 56 ) So far nothing special. But next we create a special PersonCollection model that represents a batch of persons: class PersonCollection ( object ): def __init__ ( self , persons , start ): self . persons = persons if start < 0 or start >= len ( persons ): start = 0 self . start = start def query ( self ): return self . persons [ self . start : self . start + BATCH_SIZE ] def previous ( self ): if self . start == 0 : return None start = self . start - BATCH_SIZE if start < 0 : start = 0 return PersonCollection ( self . persons , start ) def next ( self ): start = self . start + BATCH_SIZE if start >= len ( self . persons ): return None return PersonCollection ( self . persons , self . start + BATCH_SIZE ) To create an instance of PersonCollection you need two arguments: persons , which is going to be our person_db we created before, and start , which is the start index of the batch. We define a query method that queries the persons we need from the larger batch, based on start and a global constant, BATCH_SIZE . Here we do this by simply taking a slice. In a real application you'd execute some kind of database query. We also define previous and next methods. These give back the previous PersonCollection and next PersonCollection . They use the same persons database, but adjust the start of the batch. If there is no previous or next batch as we're at the beginning or the end, these methods return None . There is nothing directly web related in this code, though of course PersonCollection is there to serve our web application in particular. But as you notice there is absolutely no interaction with request or any other parts of the Morepath API. This makes it easier to reason about this code: you can for instance write unit tests that just test the behavior of these instances without dealing with requests, HTML, etc.

Path Now we expose these models to the web. We tell Morepath what models are behind what URLs, and how to create URLs to models: @App.path ( model = PersonCollection , path = '/' ) def get_person_collection ( start = 0 ): return PersonCollection ( person_db , start ) @App.path ( model = Person , path = '{id}' , converters = { 'id' : int }) def get_person ( id ): try : return person_db [ id ] except IndexError : return None Let's look at this in more detail: @App.path ( model = PersonCollection , path = '/' ) def get_person_collection ( start = 0 ): return PersonCollection ( person_db , start ) This is not a lot of code, but it actually tells Morepath a lot: When you go to the root path / you get the instance returned by the get_person_collection function.

you get the instance returned by the function. This URL takes a request parameter start , for instance ?start=10 .

, for instance . This request parameter is optional. If it's not given it defaults to 0 .

. Since the default is a Python int object, Morepath rejects any requests with request parameters that cannot be converted to an integer as a 400 Bad Request. So ?start=11 is legal, but ?start=foo is not.

object, Morepath rejects any requests with request parameters that cannot be converted to an integer as a 400 Bad Request. So is legal, but is not. When asked for the link to a PersonCollection instance in Python code, as we'll see soon, Morepath uses this information to reconstruct it. Now let's look at get_person : @App.path ( model = Person , path = '{id}' , converters = { 'id' : int }) def get_person ( id ): try : return person_db [ id ] except IndexError : return None This uses a path with a parameter in it, id , which is passed to the get_person function. It explicitly sets the system to expect an int and reject anything else, but we could've used id=0 as a default parameter instead here too. Finally, get_person can return None if the id is not known in our Python list "database". Morepath automatically turns this into a 404 Not Found for you.

View & template for Person While PersonCollection and Person instances now have a URL, we didn't tell Morepath yet what to do when someone goes there. So for now, these URLs will respond with a 404. Let's fix this by defining some Morepath views. We'll do a simple view for Person first: @App.html ( model = Person , template = 'person.jinja2' ) def person_default ( self , request ): return { 'id' : self . id , 'name' : self . name , 'address' : self . address , 'email' : self . email } We use the html decorator to indicate that this view delivers data of Content-Type text/html , and that it uses a person.jinja2 template to do so. The person_default function itself gets a self and a request argument. The self argument is an instance of the model class indicated in the decorator, so a Person instance. The request argument is a WebOb request instance. We give the template the data returned in the dictionary. The template person.jinja2 looks like this: <!DOCTYPE html> <html> <head> <title> Morepath batching demo </title> </head> <body> <p> Name: {{ name }} <br/> Address: {{ address }} <br/> Email: {{ email }} <br /> </p> </body> </html> Here we use the Jinja2 template language to render the data to HTML. Morepath out of the box does not support Jinja2; it's template language agnostic. But in our example we use the Morepath extension more.jinja2 which integrates Jinja2. Chameleon support is also available in more.chameleon in case you prefer that.

View & template for PersonCollection Here is the view that exposes PersonCollection : @App.html ( model = PersonCollection , template = 'person_collection.jinja2' ) def person_collection_default ( self , request ): return { 'persons' : self . query (), 'previous_link' : request . link ( self . previous ()), 'next_link' : request . link ( self . next ()), } It gives the template the list of persons that is in the current PersonCollection instance so it can show them in a template as we'll see in a moment. It also creates two URLs: previous_link and next_link . These are links to the previous and next batch available, or None if no previous or next batch exists (this is the first or the last batch). Let's look at the template: <!DOCTYPE html> <html> <head> <title> Morepath batching demo </title> </head> <body> <table> <tr> <th> Name </th> <th> Email </th> <th> Address </th> </tr> {% for person in persons %} <tr> <td><a href= " {{ request.link ( person ) }} " > {{ person.name }} </a></td> <td> {{ person.email }} </td> <td> {{ person.address }} </td> </tr> {% endfor %} </table> {% if previous_link %} <a href= " {{ previous_link }} " > Previous </a> {% endif %} {% if next_link %} <a href= " {{ next_link }} " > Next </a> {% endif %} </body> </html> A bit more is going on here. First it loops through the persons list to show all the persons in a batch in a HTML table. The name in the table is a link to the person instance; we use request.link() in the template to create this URL. The template also shows a previous and next link, but only if they're not None , so when there is actually a previous or next batch available.