bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.37k stars 1.46k forks source link

Question: callback=template (avoid decorator/wrapper function)? #1212

Closed gnbl closed 3 years ago

gnbl commented 4 years ago

Good morning! I've tried the IRC with no response, so forgive me for asking the following question in the issue tracker:

I'd like to live-test specific templates with the development server. I suppose I could use a catch-all route to select the template directly, but as I also am experimenting with generating static pages, I'd like to specify the template from the command line.

Is there a way to "bind" a template to a request without decorator function?

These both work, but I'm wondering how it might be possible to avoid the wrapper function:

        @bottle.route('/')
        def wrap():
            return bottle.template(args.template)
        bottle.run(host='localhost', port=8080, reloader=True)
        def wrap():
            return bottle.template(args.template)
        app = bottle.Bottle()
        app.route('/', 'GET', callback=wrap)
        app.run(host='localhost', port=8080, reloader=True)

I suppose the callback (https://bottlepy.org/docs/dev/api.html#bottle.Bottle.route) would have to get its arguments somehow (https://bottlepy.org/docs/dev/api.html#bottle.template).

On a somewhat related note: the CLI needs a python file with route definitions (https://bottlepy.org/docs/dev/tutorial.html#command-line-interface). It might be a nice feature to serve or render a specified template, e.g.

# not an actual option!
python -m bottle --debug --reload --template test 
python -m bottle --render --template test 
# not an actual option!
defnull commented 3 years ago

In debug-mode, templates are not cached. Live-testing a template is as easy as writing minimal application that serves it, editing the template and hit reload. Writing a small script that does what you want would also quite straight forward. I do not think this use-case is common enough to justify adding complexity of the bottle.py CLI.