<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reliably Broken &#187; bottle</title>
	<atom:link href="http://reliablybroken.com/b/tag/bottle/feed/" rel="self" type="application/rss+xml" />
	<link>http://reliablybroken.com/b</link>
	<description>It&#039;s a blog: let&#039;s do funch!</description>
	<lastBuildDate>Thu, 01 Dec 2011 21:27:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Class-based views for Bottle</title>
		<link>http://reliablybroken.com/b/2010/12/class-based-views-for-bottle/</link>
		<comments>http://reliablybroken.com/b/2010/12/class-based-views-for-bottle/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 21:39:46 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bottle]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=578</guid>
		<description><![CDATA[I&#8217;m not convinced this is actually a good idea, but I have an approach for using class-based views as handlers for a route with Bottle. (If you were mad keen on Django&#8217;s shift to class-based views you might reckon life wouldn&#8217;t be complete with a Bottle-based application until you employ classes for views. However Bottle&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not convinced this is actually a good idea, but I have an approach for using class-based views as handlers for a route with <a href="http://bottle.paws.de/">Bottle</a>.</p>

<p><em>(If you were mad keen on <a href="http://www.djangoproject.com/">Django&#8217;s</a> shift to <a href="http://docs.djangoproject.com/en/dev/topics/class-based-views/">class-based views</a> you might reckon life wouldn&#8217;t be complete with a Bottle-based application until you employ classes for views. However Bottle&#8217;s use of decorators for tying URLs to views means it is less a natural fit than the same thing in Django.)</em></p>

<p>The problem is that you can&#8217;t just decorate the method in your class using <a href="http://bottle.paws.de/docs/dev/api.html#routing"><code>bottle.route</code></a> because if you use that decorator on a method in a class you are telling Bottle to use the method before it has been bound to an instance of that class.</p>

<p>So although I wish it did, the following example will not work:</p>

<pre><code>import bottle

class ViewClass(object):
    @bottle.route("/")
    def home_view(self):
        return "My home page."

obj = ViewClass()
bottle.run()
</code></pre>

<p>Running that will lead to errors about not enough arguments passed to the view method of your <code>ViewClass</code> instance.</p>

<p>Instead you need to register the route right after the object is created. This can be done in <a href="http://docs.python.org/reference/datamodel.html#object.__new__">the class&#8217;s <code>__new__</code> method</a>:</p>

<pre><code>import bottle

class ViewClass(object):
    def __new__(cls, *args, **kwargs):
        obj = super(ViewClass, cls).__new__(cls, *args, **kwargs)
        bottle.route("/")(obj.home_view)
        return obj

    def home_view(self):
        return "My home page."

obj = ViewClass()
bottle.run()
</code></pre>

<p>It works. It isn&#8217;t that pretty. You could achieve exactly the same thing by explicitly passing the <code>obj.home_view</code> method to <code>bottle.route</code> <em>after</em> the instance is created. The advantage to doing this in the <code>__new__</code> method is it will happen automatically whenever <code>ViewClass</code> is instantiated.</p>

<p>And if you go down this path then <a href="http://bottle.paws.de/docs/dev/tutorial.html#accessing-request-data">you should be aware of threads</a>. Hey! Nice threads! Also I have a cold.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/12/class-based-views-for-bottle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bottle&#8217;s view decorator and default variables</title>
		<link>http://reliablybroken.com/b/2010/08/curried-bottle-views/</link>
		<comments>http://reliablybroken.com/b/2010/08/curried-bottle-views/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 10:57:46 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bottle]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=525</guid>
		<description><![CDATA[Bottle&#8216;s @view decorator provides a simple way to designate a template to render an HTML page. Your view function just has to return a dictionary, and its contents can be accessed from the template using the '{{ name }}' syntax. The @view decorator can also take keyword arguments. These are treated as default template variables [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bottle.paws.de/">Bottle</a>&#8216;s <a href="http://bottle.paws.de/docs/0.8/api.html#bottle.view"><code>@view</code> decorator</a> provides a simple way to designate a template to render an HTML page. Your view function just has to return a dictionary, and its contents can be accessed from the template using the <code>'{{ name }}'</code> syntax.</p>

<p>The <code>@view</code> decorator can also take keyword arguments. These are treated as default template variables &#8211; if the dictionary returned by your view function doesn&#8217;t have a key for one of the keyword arguments then the template will use the value passed into the decorator, like so:</p>

<pre><code>from bottle import view

@view('default.html', author='David Buxton')
def home():
    return {'title': 'Home page'}
</code></pre>

<p>That would render any instance of <code>'{{ author }}'</code> as <code>'David Buxton'</code>. And then you can have another view function that overrides the keywords by returning a different value in the dictionary:</p>

<pre><code>from bottle import view

@view('default.html', author='David Buxton')
def music():
    return {'title': 'Thalassocracy', 'author': 'Frank Black'}
</code></pre>

<p>And at that point I wonder what is the advantage of using keyword arguments with <code>@view</code>: you have to decorate each function separately, and if you want to override a keyword in your return dictionary then it would be easier not to specify the keyword in the first place.</p>

<p>Thus the real point of using keywords with the <code>@view</code> function is only apparent if you curry the <code>@view</code> decorator with keywords first so that you can re-use the curried decorator and avoid repeating yourself.</p>

<p><em>Someday I will re-write the previous sentence. Until then, sorry.</em></p>

<p>Instead of passing a default author each time as in the examples above, let&#8217;s make a new <code>@view</code> decorator (using Python&#8217;s <a href="http://docs.python.org/library/functools.html">functools module</a>) and then use that on each view function:</p>

<pre><code>import functools
from bottle import view

view = functools.partial(view, author='David Buxton')

@view('default.html')
def home():
    return {'title': 'Home page'}

@view('default.html')
def music():
    return {'title': 'Thalassocracy', 'author': 'Frank Black'}
</code></pre>

<p>The new decorator means you get the default keyword arguments wherever you use <code>@view</code> while permitting any function to override those defaults in the dictionary it returns.</p>

<p>And if you wanted to get really lazy you could even pass in a template name when wrapping the decorator with <code>functools.partial</code>, however you would not be able to use your wrapped decorator to change the template name because it is a positional argument (like what <a href="http://docs.python.org/library/functools.html#functools.partial">it explains here in the functools documentation</a>). You would also have to call the decorator with no arguments like <code>'@defaultview()'</code>. So forget I mentioned it.</p>

<p>I&#8217;m not saying you are lazy.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/08/curried-bottle-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django-style routing for Bottle</title>
		<link>http://reliablybroken.com/b/2010/07/django-style-routing-for-bottle/</link>
		<comments>http://reliablybroken.com/b/2010/07/django-style-routing-for-bottle/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 21:57:40 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bottle]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=510</guid>
		<description><![CDATA[Bottle provides the @route decorator to associate URL paths with view functions. This is very convenient, but if you are a Django-reject like me then you may prefer having all your URLs defined in one place, the advantage being it is easy to see at a glance all the different URLs your application will match. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bottle.paws.de/">Bottle</a> provides the <a href="http://bottle.paws.de/docs/0.8/api.html#bottle.route"><code>@route</code> decorator</a> to associate URL paths with view functions. This is very convenient, but if you are a <a href="http://www.djangoproject.com/">Django</a>-reject like me then you may prefer having all your URLs defined in one place, the advantage being it is easy to see at a glance <a href="http://docs.djangoproject.com/en/1.2/topics/http/urls/#example">all the different URLs your application will match</a>.</p>

<p><em>Updated: I have re-written this post and the example to make it simpler following Marcel Hellkamp&#8217;s comments (Marcel is the primary author of Bottle). My original example was needlessly complicated.</em></p>

<p>It is possible to have <a href="http://docs.djangoproject.com/en/dev/topics/http/urls/">a Django-style urlpatterns stanza</a> with a Bottle app. Here&#8217;s how it can work:</p>

<pre><code>from bottle import route

# Assuming your *_page view functions are defined above somewhere
urlpatterns = (
    # (path, func, name)
    ('/', home_page, 'home'),
    ('/about', about_page, 'about'),
    ('/contact', contact_page, 'contact'),
)

for path, func, name in urlpatterns:
    route(path, name=name)(func)
</code></pre>

<p>Here we run through a list where each item is a triple of URL path, view function and a name for the route. For each we simply call the <code>route</code> method and then invoke it with the function object. Not as flexible as using the decorator on a function (because the <code>@route</code> decorator can take additional keyword arguments) but at least you can have all the routes in one place at the end of the module.</p>

<p>Then again if you have so many routes that you need to keep them in a pretty list you probably aren&#8217;t writing the simple application that Bottle was intended for.</p>

<p>(This was tested with Bottle&#8217;s 0.8 and 0.9-dev branches.)</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/07/django-style-routing-for-bottle/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

