0
|
1 <!DOCTYPE html>
|
|
2 <html>
|
|
3 <head>
|
|
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
5 <title>Introducing TinCan</title>
|
|
6 <style type="text/css">
|
|
7 .kbd { font-family: monospace; }
|
|
8 </style>
|
|
9 </head>
|
|
10 <body>
|
|
11 <h1>Introducing TinCan, a “Code-Behind” MVC Framework for Bottle</h1>
|
|
12 <h2>Introduction</h2>
|
|
13 <p>TinCan is a Python 3 code-behind web framework implemented in the Bottle
|
|
14 microframework. As with Bottle, all the code is in one module, and there
|
|
15 is no direct dependency on anything that is not part of the standard
|
|
16 Python library (except of course for Bottle itself).</p>
|
|
17 <p>The default templating engine for TinCan is <a href="https://chameleon.readthedocs.io/en/latest/">Chameleon</a>.
|
|
18 TinCan adds Chameleon as a fully-supported templating engine for Bottle.
|
|
19 Any template engine supported by Bottle can be used to render TinCan
|
|
20 Pages.</p>
|
|
21 <h2>Why Do This?</h2>
|
|
22 <p>In short, there is too much repeating oneself in most all Python web
|
|
23 frameworks (and this includes Bottle). One is always saying “this is
|
|
24 controller <span class="kbd">foo</span>, whose view is in the template <span
|
|
25 class="kbd">foo.pt</span>, at route <span class="kbd">/foo</span>.”</p>
|
|
26 <p>That’s a lot more busywork than just writing <span class="kbd">foo.php</span>
|
|
27 or <span class="kbd">foo.cgi</span>, so many simple webapps end up being
|
|
28 implemented via the latter means. That’s unfortunate, as CGI isn’t very
|
|
29 resource-efficient, and there’s much nicer languages to code in than PHP
|
|
30 (such as Python :-) ). Worst of all, you now have logic and presentation
|
|
31 all scrambled together, never a good idea.</p>
|
|
32 <p>What if, instead, you could write <span class="kbd">foo.pspx</span> and
|
|
33 <span class="kbd">foo.py</span>, and a framework would automatically
|
|
34 create the <span class="kbd">/foo.pspx</span> route for you, much like
|
|
35 ASP.NET or JSP would for a <span class="kbd">.aspx</span> or <span class="kbd">.jsp</span>
|
|
36 file? The matching code-behind in the <span class="kbd">.py</span> file
|
|
37 would be easily detected (same name, different extension) and
|
|
38 automatically associated with the template, of course. You could focus on
|
|
39 writing pages instead of repeating yourself saying the obvious over and
|
|
40 over again. </p>
|
|
41 <p>This is what TinCan lets you do.</p>
|
|
42 <h2>Hang On, Code-Behind Isn’t MVC!</h2>
|
|
43 <p>Why <em>isn’t</em> it? The model, as always, is the data and containing
|
|
44 core business logic. The template file defines the view presented to the
|
|
45 user, and the code-behind is the intermediary between the two. A
|
|
46 controller by any other name…</p>
|
|
47 <h2>How Can There Be Multiple Views for One Controller?</h2>
|
|
48 <p>Easily. Take a look at the <code>#python</code> header directive. </p>
|
|
49 <h2>Multiple Controllers for One View?</h2>
|
|
50 <p>Personally, I don’t think this is the best of ideas. Just because two
|
|
51 controllers might be able to share a view <em>now</em> does not mean they
|
|
52 will continue to in the future. Then you change one (controller, view)
|
|
53 pair and another controller someplace else breaks!</p>
|
|
54 <p>However, if you insist, check out the <code>#template</code> and <code>#hidden</code>
|
|
55 header directives. </p>
|
|
56 <h2>But This Causes Less SEO-Friendly Routes!</h2>
|
|
57 <p>First, this is not always important. Sometimes, all you want to do is get
|
|
58 a small, simple, special-purpose, site up and running with a minimum of
|
|
59 busywork. Why should you be forced to do more work just because that extra
|
|
60 work benefits someone <em>else</em>?</p>
|
|
61 <p>Second, when it is, you can always use <code>RewriteRule</code> (Apache)
|
|
62 <code>rewrite</code> (nginx), or the equivalent in your favorite Web
|
|
63 server, and code your templates to use the SEO-friendly version of your
|
|
64 URL’s. With TinCan sitting behind a good, production-grade web server, you
|
|
65 get the best of both worlds: fast, simple deployment when you want it, and
|
|
66 SEO-friendly URL’s when you want it. </p>
|
|
67 <h2>But What about Routing Things Based on Both Path and Method?</h2>
|
|
68 <p>That’s easy enough to do, as TinCan is implemented on top of Bottle. You
|
|
69 can add your Bottle routes, using the <code>@route</code> decorator on
|
|
70 your controller methods, same as always. Just stick them in the same
|
|
71 start-up script you use to launch your TinCan files.</p>
|
|
72 <p>If for some reason you don’t want to mess with manually creating routes
|
|
73 and associating them with controllers in Bottle (even in cases like this
|
|
74 where it arguably makes sense), and want to do <em>everything</em> the
|
|
75 TinCan way, you can create a set of hidden (using the <code>#hidden</code>
|
|
76 directive) pages and a main dummy page whose code-behind forwards (<code>page.request.app.forward</code>)
|
|
77 to the appropriate hidden page depending on request method. </p>
|
|
78 <h2>What about Launching Multiple TinCan Webapps?</h2>
|
|
79 <p>It works just as well (and just as poorly) as launching multiple Bottle
|
|
80 webapps. Note that the big limitation here is Python’s module subsystem;
|
|
81 there is only one. Thus, all webapps share the same module path. There is
|
|
82 no way to have one webapp using an older version of a given module served
|
|
83 by the same server as another using a newer version, save renaming one of
|
|
84 the modules. This is a Python issue, not a Bottle issue or a TinCan issue.</p>
|
|
85 <p>Note that TinCan bypasses the Python module cache and manages its own
|
|
86 importing of code-behind files, so there is no problem if you have
|
|
87 multiple webapps using the same relative URL paths. TinCan will keep all
|
|
88 those code-behind files straight; it will not confuse one webapp’s <span
|
|
89 class="kbd">/index.py</span> with another’s.</p>
|
|
90 <h2>What about Bottle Plugins?</h2>
|
|
91 <p>I am working on adding support for these.</p>
|
|
92 </body>
|
|
93 </html>
|