Ejemplo n.º 1
0
def register_swagger(app, server_base):
    """
    Registers Swagger docs to be accessible from non root server base.
    For example https://localhost:5000/todoapp/api/v1, where /todoapp is server
    base in this example and is behind a reverse proxy. Please note that the
    function is safe to call even with standard

    Args:
        app: FLASK application instance
        server_base: non root server base
    """
    if server_base is not None and server_base != "":
        custom_apidoc = apidoc.Apidoc(
            'restplus_custom_doc',
            __name__,
            template_folder='templates',
            static_folder=os.path.dirname(apidoc.__file__) + '/static',
            static_url_path='/swaggerui')
    else:
        # There is nothing to do as we are serving from root
        return

    @custom_apidoc.add_app_template_global
    def swagger_static(filename):
        url = url_for('restplus_custom_doc.static', filename=filename)
        return url

    _logger.info("Setting swagger assets to be served from %s", server_base)
    app.register_blueprint(custom_apidoc, url_prefix=server_base)
def create_custom_apidoc(url=None):
    """
    Create API document object for SwaggerUI generator
    :param url: URL to store static files for documentation
    :return: APIdoc object
    """
    if url is None:
        url = '/'

    relative_name = f'restplus_relative_doc{url}'
    custom_apidoc = apidoc.Apidoc(relative_name, __name__,
                                  template_folder='templates',
                                  static_folder=f'{os.path.dirname(apidoc.__file__)}/static',
                                  static_url_path='/swaggerui')

    @custom_apidoc.add_app_template_global
    def swagger_static(filename):
        """
        Change Swagger static file path
        :param filename: Filename from Swagger
        :return:
        """
        return url_for(f'{relative_name}.static', filename=filename)

    return custom_apidoc
Ejemplo n.º 3
0
    def _register_apidoc(self, app):
        patched_api = apidoc.Apidoc(
            "restx_doc",
            "flask_restx.apidoc",
            template_folder="templates",
            static_folder="static",
            static_url_path="/api",
        )

        @patched_api.add_app_template_global
        def swagger_static(filename):
            return url_for("restx_doc.static", filename=filename)

        app.register_blueprint(patched_api)
Ejemplo n.º 4
0
    def _register_apidoc(self, app: Flask) -> None:
        conf = app.extensions.setdefault('restplus', {})
        custom_apidoc = apidoc.Apidoc('restplus_doc',
                                      'flask_restplus.apidoc',
                                      template_folder='templates',
                                      static_folder='static',
                                      static_url_path=url_prefix)

        @custom_apidoc.add_app_template_global
        def swagger_static(filename: str) -> str:
            return url_for('restplus_doc.static', filename=filename)

        if not conf.get('apidoc_registered', False):
            app.register_blueprint(custom_apidoc)
        conf['apidoc_registered'] = True
Ejemplo n.º 5
0
from flask import Flask, url_for
from {{cookiecutter.pkg_name}}.api import api
from flask_restx import apidoc
from os.path import dirname, join
from urllib.parse import urlparse, parse_qs
from sqlalchemy import create_engine


custom_apidoc = apidoc.Apidoc(
    'custom_restx_doc',
    __name__,
    template_folder='templates',
    static_folder=join(dirname(apidoc.__file__), 'static'),
    static_url_path='/api/swaggerui',
)


DATABASE_URL = 'sqlite:///db.sqlite'


@custom_apidoc.add_app_template_global
def swagger_static(filename):
    return url_for("custom_restx_doc.static", filename=filename)


def create_database(config=None):
    engine = create_engine(DATABASE_URL, convert_unicode=True)
    Base.metadata.create_all(engine)


def bind_database():