Yann's Blog - Software and hardware

September 29, 2008

yyafl now has layout support, with decorators

Filed under: Python,Software — Yann @ 11:35 pm

Just finished adding in a flexible layout system to yyafl, my reimplementation of Django newforms for other Python web frameworks or WSGI adapters. Now its simple to add default layouts or render a different layout on demand.

For example, to add a simple TableLayout() to a form:

class Form1(yyafl.Form):
    name = fields.CharField(label = "User name", required = True)
    email = fields.CharField(label = "Your e-mail address", required = True)
    hidden = fields.CharField(widget = HiddenInput, default = "123")
    _layout = yyafl.layout.TableLayout()

and to render the form to HTML:

# Render using the layout specified in _layout above.
content += f.render()
# Or invoke the layout explicitly
l = yyafl.layout.TableLayout()
content += l.render(f)

yyafl Layout()s also understand decorators:

decorators = { '*' : MarkRequiredDecorator(),
    'name_field' : HighlightDecorator() }
layout = yyafl.layout.TableLayout(decorators = decorators)
layout.render(f)

Decorators can change attributes and wrap fields in markup blocks. In the above example, the ‘*’ signifies the decorator should apply to all fields, and the HighlightDecorator should only apply to the field called ‘name_field’.

There are still some not yet implemented ideas:

  • It doesn’t generated the <form> blocks yet, you need to roll your own.
  • Field reordering and grouping isn’t supported by the default layout engines yet, though you could write your own.
  • No AJAX form support (though its coming!)
  • Automatic CSRF prevention forms.
  • Error message printing in the form.

You can find these latest changes in the Git repository linked from the main yyafl web page.

September 24, 2008

Why I Use CherryPy, in someone elses words

Filed under: Python,Software — Yann @ 10:03 pm

With the popularity of Django or Rails, people have asked before why I use CherryPy. The reason is simple, I love the flexibility. I get to pick what I want to use inside, CherryPy simply worries about how to get the information to a web browser. I found this post to be a good overview of the strengths of CherryPy. Give it a good read. I completely agree with the author’s points, and wish CherryPy would get a bit more attention in the do everything web-framework era.

Powered by WordPress