apidoc.py 966 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from flask import url_for, Blueprint, render_template
  4. class Apidoc(Blueprint):
  5. """
  6. Allow to know if the blueprint has already been registered
  7. until https://github.com/mitsuhiko/flask/pull/1301 is merged
  8. """
  9. def __init__(self, *args, **kwargs):
  10. self.registered = False
  11. super(Apidoc, self).__init__(*args, **kwargs)
  12. def register(self, *args, **kwargs):
  13. super(Apidoc, self).register(*args, **kwargs)
  14. self.registered = True
  15. apidoc = Apidoc(
  16. "restx_doc",
  17. __name__,
  18. template_folder="templates",
  19. static_folder="static",
  20. static_url_path="/swaggerui",
  21. )
  22. @apidoc.add_app_template_global
  23. def swagger_static(filename):
  24. return url_for("restx_doc.static", filename=filename)
  25. def ui_for(api):
  26. """Render a SwaggerUI for a given API"""
  27. return render_template("swagger-ui.html", title=api.title, specs_url=api.specs_url)