美文网首页
Flask-fakeStatic: url rule 伪静态扩展

Flask-fakeStatic: url rule 伪静态扩展

作者: LonelyT_T | 来源:发表于2018-04-01 17:34 被阅读0次

    Source Code:flask-fakeStatic.
    Yo...Star the repo if it does help you

    from flask import Flask, current_app, jsonify
    
    
    class FakeStatic(object):
        skip_endpoints = {"static", }
    
        def __init__(self, app=None, skip_endpoints=None):
            if skip_endpoints is not None:
                self.skip_endpoints |= set(skip_endpoints)
            if app is not None:
                self.init_app(app)
    
        def init_app(self, app):
            for rule in app.url_map.iter_rules():
                if rule.endpoint in self.skip_endpoints:
                    continue
                rule.rule += ".html"
                rule.refresh()
    
    
    app = Flask(__name__)
    
    
    @app.route("/index")
    def index():
        return "index"
    
    
    @app.route('/routes')
    def routes():
    
        arr = [rule.endpoint for rule in current_app.url_map.iter_rules()]
        return jsonify({"total": len(arr), "data": sorted(arr)})
    
    # stay here, after all rules registered
    FakeStatic(app, skip_endpoints=["routes"])
    if __name__ == "__main__":
        app.run(debug=True)
    

    相关文章

      网友评论

          本文标题:Flask-fakeStatic: url rule 伪静态扩展

          本文链接:https://www.haomeiwen.com/subject/uulacftx.html