Several static directories for tornado
<p>We are building uServices based on tornado. There are some routes which are common for all uServices, for the time being <code>health</code> and <code>docs</code>. The docs route is built using <code>Swagger</code>. That means, the swagger route, and associated assets, are part of our common library (but not the documentation itself, which is uService related), which is simply a requirement to our uServices.</p>

<p>Since swagger needs static assets, and each uService needs its own static assets too, I have a problem: my static assets are coming from two completely different places. I have found a (very inconvenient) hack to solve this:</p>

<ol>
<li>track static assets in the common repo</li>
<li>track static assets in the uService repo</li>
<li>when deploying, copy those static assets, from both sources, to a deployment static folder (and hope that there are no clashes)</li>
<li>specify the deployment static folder as the <code>static_path</code></li>
</ol>

<p>Step 3 is quite complex, because it involves pip-installing the common library, finding the assets there (already a hack), and copying them around:</p>

<ul>
<li>install the common library with pip</li>
<li>look for the location of the installed library (<code>site-packages</code>)</li>
<li>copy the static assets to the deployment static folder</li>
</ul>

<p>It would be much easier if, as happens with the <a href="http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.get_template_path" rel="nofollow"><code>template_path</code></a>, it would be possible to have a per-application static folder:</p>

<pre><code>&gt; Return None to load templates relative to the calling file.
</code></pre>

<p>I have been searching the web, and it seems <a href="http://stackoverflow.com/a/3935046/647991">this is not possible</a>, but I wanted to double check.</p>

<p>An alternative would be to simplify asset extracting from a packaged python module, but not sure what is the preferred method to do this, without having  to recur to hacks searching in the <code>virtualenv</code> for the <code>site-packages</code>.</p>

<p>It's not possible to do this with the <code>static_path</code> setting, but as long as you don't require the <code>static_url()</code> function, you can create multiple <code>StaticFileHandler</code> entries in your URLSpec list:</p>

<pre><code>Application([
    ('/static1/(.*)', tornado.web.StaticFileHandler, dict(path='static1')),
    ('/static2/(.*)', tornado.web.StaticFileHandler, dict(path='static1')),
])
</code></pre>

<p>As a substitute for <code>static_url</code>, you can call <code>StaticFileHandler.make_static_url</code> and pass it the arguments that would have been global if you were using <code>static_path</code>.</p>

python tornado assets