Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Martijn Faassen: Build a better batching UI with Morepath and Jinja2

$
0
0

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:

classPerson(object):def__init__(self,id,name,address,email):self.id=idself.name=nameself.address=addressself.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()defgenerate_random_person(id):returnPerson(id,fake.name(),fake.address(),fake.email())defgenerate_random_persons(amount):return[generate_random_person(id)foridinrange(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:

classPersonCollection(object):def__init__(self,persons,start):self.persons=personsifstart<0orstart>=len(persons):start=0self.start=startdefquery(self):returnself.persons[self.start:self.start+BATCH_SIZE]defprevious(self):ifself.start==0:returnNonestart=self.start-BATCH_SIZEifstart<0:start=0returnPersonCollection(self.persons,start)defnext(self):start=self.start+BATCH_SIZEifstart>=len(self.persons):returnNonereturnPersonCollection(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='/')defget_person_collection(start=0):returnPersonCollection(person_db,start)@App.path(model=Person,path='{id}',converters={'id':int})defget_person(id):try:returnperson_db[id]exceptIndexError:returnNone

Let's look at this in more detail:

@App.path(model=PersonCollection,path='/')defget_person_collection(start=0):returnPersonCollection(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.
  • This URL takes a request parameter start, for instance ?start=10.
  • 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.
  • 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})defget_person(id):try:returnperson_db[id]exceptIndexError:returnNone

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')defperson_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')defperson_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>{%forpersoninpersons%}<tr><td><ahref="{{request.link(person)}}">{{person.name}}</a></td><td>{{person.email}}</td><td>{{person.address}}</td></tr>{%endfor%}</table>{%ifprevious_link%}<ahref="{{previous_link}}">Previous</a>{%endif%}{%ifnext_link%}<ahref="{{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.

That's it

And that's it, besides a few details of application setup, which you can find in the complete example project on Github.

There's not much to this code, and that's how it should be. I invite you to compare this approach to a batching UI to what an implementation for another web framework looks like. Do you put the link generation code in the template itself? Or as ad hoc code inside the view functions? How clear and concise and testable is that code compared to what we just did here? Do you give back the right HTTP status codes when things go wrong? Consider also how easy it would be to expand the code to include searching in addition to batching.

Do you want to try out Morepath now? Read the very extensive documentation. I hope to hear from you!


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>