Sunday, 13 January 2013

A Review of the Nagare Microframework

The analytics tell me that the keywords for this blog are "Best Python Framework" which is odd because I don't think I can pretend to have that answer. What I can do is point out what the differences are and let you find the one that suits you best. Nagare for example offers some concepts that you won't be used to if you come from using Django or Flask and the like.

If you have read Install Stackless Python on Ubuntu and could get stackless running the examples I showed you, then just follow these commands to get a stackless virtual environment running a nagare project called 'nagaredemo':

stackless /usr/lib/python2.7/dist-packages/virtualenv.py ~/Projects/nagaredemo
workon nagaredemo
cdvirtualenv
pip install nagare
nagare-admin create-app demos
cd demos/
stackless setup.py develop
nagare-admin serve demos
view raw nagare.sh hosted with ❤ by GitHub
In the examples below, paste the code directly into the file
//negaredemo/demos/demos/app.py
and be aware that when you change the code you need to  restart the development server (Ctrl-C stops it.)

Hello World!


from nagare import presentation
class Welcome(object):
message = "Hello World!"
@presentation.render_for(Welcome)
def render(welcome, h, *args):
"""
HTML fragment is associated with `Welcome` class as its default view.
- `welcome`: an object of type Welcome
- `h`: HTML renderer, it exposes HTML namespace
"""
with h.div:
h << h.span(welcome.message, class_='welcome-class')
return h.root
app = Welcome
view raw example1.py hosted with ❤ by GitHub

Okay, so example1 shows the basics of rendering a Welcome object.
http://localhost:8080/demos/
You'll notice immediately that we're not using a templating language here, but rather the built in Domain Specific Language (DSL). In reality you would use the DSL to build xhtml snippets and then use meld to insert them into templates. I've not used meld before, but it looks very straight forward - neat in fact. Now onto example2.

Callbacks


from nagare import presentation
class Counter(object):
def __init__(self):
self.val = 0
def increase(self):
self.val += 1
def decrease(self):
self.val -= 1
@presentation.render_for(Counter)
def render(counter, h, *args):
h << h.div('Value: ', counter.val)
# action method allows to register callbacks on HTML elements
# that can have actions (links, inputs, etc.)
h << h.a('++').action(counter.increase)
h << '|'
h << h.a('--').action(counter.decrease)
return h.root
app = Counter
view raw example2.py hosted with ❤ by GitHub

Here we've registered two callbacks on the DOM without having to explicitly declare the URL mappings. have a look at the rendered HTML to see how this has been implemented. Now, that's pretty handy.

Tiresome RESTful URLs


from nagare import presentation
class Counter(object):
def __init__(self):
self.val = 0
def increase(self):
self.val += 1
def decrease(self):
self.val -= 1
@presentation.render_for(Counter)
def render(counter, h, *args):
h << h.div('Value: ', counter.val)
h << h.a('++').action(counter.increase)
h << '|'
h << h.a('--').action(counter.decrease)
return h.root
@presentation.init_for(Counter, 'len(url) == 1 and url[0] == "Counter"')
def init(self, url, comp, http_method, request):
"""
Meaningful URLs are uses to initialize a component with the received URL.
Other than that application works the same as it used to do.
"""
value = request.params.get('value')
if value and value.isdigit():
self.val = int(value)
app = Counter
view raw example3.py hosted with ❤ by GitHub

Example 3 takes that same code and augments it to allow us to have more ReSTful URLs. I'm not sure this is as obvious as I'd like, but it works I suppose.
http://localhost:8080/demos/Counter?value=7

Saving State Automatically


from nagare import component, util
import random
class GuessNumber(component.Task):
"""
`component.Task` are used to provide a sensible way to program web applications in
direct style, i.e. as a sequence of actions.
"""
def go(self, comp):
number = random.randint(1, 10)
while True:
x = comp.call(util.Ask('Try: '))
if not x.isdigit() or int(x) != number:
comp.call(util.Confirm('Choose another number'))
if int(x) == number:
comp.call(util.Confirm('Congratulations !'))
break
app = GuessNumber
view raw example4.py hosted with ❤ by GitHub
Nagare can hide the request/response object and global session from the developer. Object states are automatically saved in a server side session during the request/response round-trip. This allows for a more direct style of programming like in a desktop application.

And there's more


There's more documentation on the Nagere website itself, so scoot along there and see what else there is to offer. Nagare is built upon ConfigObj, WebOb, Paste, PEAK-Rules and lxml, so it's on good foundations. I'm told by Hervé Coatanhay that the next release will introduce non stackless support using pypy or CPython.

Conclusion


Nagare is able to implement Continuations and that's a great thing. However I'm not sure I like the way it obfuscates the implementation. I'd much rather be able to see what was going on.

For example, saving state between requests is nice, but is it secure? Do I need to plough through the code to find out? In my opinion it would have been better to have offered a structure that allows for others to plug in solutions/implementations.

The documentation is somewhat limited, which is probably because the community is still fairly small. However sinceteh project has been running since 2008, perhaps that's something to be concerned about.

Thanks to Hervé Coatanhay for suggestion the illustrative code snippets.


9 comments:

  1. One thing that you can help clarify is difference between frameworks that build web applications (like google docs) versus frameworks that build websites (blogs, cms, etc)

    ReplyDelete
  2. In the Color Pink bonus the Pink Panther paints a wall. All the pink numbers are totaled to give the player’s cash prize. Wheel of Pink Bonus is another exciting game. The wheel has two layers. The outer one gives the multiplier. 야동

    ReplyDelete
  3. You possess lifted an essential offspring. Bless for using. I would want to study better latest transactions from this blog. Preserve posting. 오피

    ReplyDelete
  4. Menuco helps you to grow your business online. We will create your website, an online ordering system, and a rock-solid online presence. With Menuco grow more. You will get more sales. Stop relying on other applications. Get your own application and website. 마사지

    ReplyDelete
  5. Every device needs to be pre-set for proper use. That’s why we have a web page mywifiext.net for our Netgear Wi-Fi Extender users. It’s quite easy to set up your Netgear Extender device. Just go to mywifiext.net and follow a few simple steps written on the page. We know how to value all our customers. So, we tried to keep the steps as simple as possible. We hope you do not encounter any problems during the process. But if you still can not get your device started, we are always happy to help. Just go to Netgear support for further assistance. 건전마사지

    ReplyDelete
  6. Explore the ultimate solution for your digital infrastructure with managed digitalocean services. Experience seamless deployment, optimization, and support.

    ReplyDelete