goFrendiAsgard
goFrendiAsgard

Reputation: 4084

Bottlepy serve static file with dynamic root

Serve static file in bottlepy is as simple as this:

@route('statics/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='assets')

Thus the static file served should be in assets directory.

So, this one: http://127.0.0.1:8080/statics/jquery.js will refer to assets/jquery.js

So far, I don't find any problem. But let's say, I want to make a framework with application folder contains some models, views and controllers. The directory structure is like this:

|---applications
|   |--- assets
|---start.py
|---core
    |--- __init__.py

In core/__init__.py I put a function to run bottle and route assets directory

from bottle import route, run, static_file

@route('assets/<filepath:path>')
def _serve_assets(path):
    # I want the root to be dynamic, because It is not always be applications
    return static_file(path, root=os.path.join('applications', 'assets'))

def framework_start(application_path = 'applications', **kwargs):
    # A lot of logic
    run(**kwargs)

And on start.py i do this:

from core import framework_start
framework_start(application_path = 'applications')

So far the static files served as expected. But I want it to still works even after I change the code in start.py into this:

framework_start(application_path = 'app')

and the directory structure into this

|---apps
|   |--- assets
|---start.py
|---core
    |--- __init__.py

So, how to do that? How to make a static routing with dynamic root?

Upvotes: 0

Views: 1321

Answers (1)

f p
f p

Reputation: 3223

return static_file(filename, root=filefolder)

The first argument is the name only, and the second is the folder that contains it.

You know where the docs are...

From your comments I think I got it.

If you want the template to have the path, you need to send it to bottle by something like <input type="hidden" name="arch"... and recieve it with request.GET.get('arch', '') and use it with root=....

Upvotes: 1

Related Questions