Search code examples
pythonbottle

python bottle webserver caches html files


I'm using a bottle webserver in python. When I am debugging, I want to make changes to the pseudo-html template files and then reload the page to see it in the browser. It seems that bottle caches the template files and I can't get them to load from the newly-modified template.

Here's my situation:

First approach

  1. run web server, pull up main.tpl.htlml
  2. change the file name of secondary.tpl.html to secondary2.tpl.html
  3. open secondary.tpl.html (no longer exists)
  4. get 404 error ie. all well and good

Second approach

  1. run web server, pull up main.tpl.htlml
  2. open secondary.tpl.html, works fine
  3. change the file name of secondary.tpl.html to secondary2.tpl.html
  4. reload secondary.tpl.html (no longer exists)
  5. get the original secondary.tpl.html web page that no longer should exist

I have forced the browser to request the file a second time (not using its own cache) and, via a python debugger (PyCharm), I can see that the bottle webserver is at least pretending to go through the motions of doing all of the things it is supposed to do with a template file.


Solution

  • From http://bottlepy.org/docs/dev/stpl.html#bottle.SimpleTemplate

    Just keep in mind that compiling and rendering templates are two different actions, even if the template() helper hides this fact. Templates are usually compiled only once and cached internally, but rendered many times with different keyword arguments.

    But http://bottlepy.org/docs/dev/tutorial.html#templates states:

    Caching

    Templates are cached in memory after compilation. Modifications made to the template files will have no affect until you clear the template cache. Call bottle.TEMPLATES.clear() to do so. Caching is disabled in debug mode.

    Which you could find at http://bottlepy.org/docs/dev/tutorial.html#debug-mode

    bottle.debug(True)

    In this mode, Bottle is much more verbose and provides helpful debugging information whenever an error occurs. It also disables some optimisations that might get in your way and adds some checks that warn you about possible misconfiguration.

    Here is an incomplete list of things that change in debug mode:

    The default error page shows a traceback.
    Templates are not cached.
    Plugins are applied immediately.

    If that still doesn't help, a bit of browsing turns up https://github.com/bottlepy/bottle/blob/master/bottle.py#L3937

    class SimpleTemplate(BaseTemplate):
        def prepare(self,
                escape_func=html_escape,
                noescape=False,
                syntax=None, **ka):
            self.cache = {}
            ...
    

    which seems to clear the cache.