Bottle Static files

前端 未结 5 1927
小蘑菇
小蘑菇 2020-12-23 21:54

I have tried reading the docs for Bottle, however, I am still unsure about how static file serving works. I have an index.tpl file, and within it it has a css f

5条回答
  •  不思量自难忘°
    2020-12-23 22:01

    I've used Sanketh's template in the past but over time condensed it to an extension agnostic function. You just have to add extension-folder mappings in the ext_map dictionary. It defaults to static/ folder if an extension is not mapped explicitly.

    import os.path    
    
    # Static Routes
    @get('/')
    def serve_static_file(filename):
        ext = os.path.splitext(filename)[1][1:]
        ext_map = {'image':['png','gif','jpg','ico'],'js':['js']}
        sub_folder = next((k for k, v in ext_map.items() if ext in v),'')
        return static_file(filename, root='static/'+sub_folder)
    

提交回复
热议问题