Flask-restful: definitive guide to using flask-restful with Blueprints

Created on 4 Mar 2014  ·  4Comments  ·  Source: flask-restful/flask-restful

There are quite lot of issues, commits, and PRs relating to using blueprints, some of which conflict with each other. Is there a quickstart to using flask-restful with blueprints?

docs

Most helpful comment

This is awesome. :tada:

However, it didn't exactly worked / was clear for me.
For the lost souls that will come here, I managed to do it like this:

from flask import Flask, Blueprint
from flask_restful import Api
...

class FlaskServer:

    API_VERSION = 0.1
    API_URL_PREFIX = '/api/v%s' % API_VERSION

    api_blueprint = Blueprint('api', __name__)

    ...

    def __init__(self, host, http_port):

        self.host = host
        self.http_port = http_port
        ...

        # Flask Restuful API
        self.api = Api(
            app=self.api_blueprint,
            prefix=self.API_URL_PREFIX
        )

        # grouping all 'self.api.add_resource()' definitions
        self._init_api_resources()

        # only after the api resources where added
        self.app.register_blueprint(self.api_blueprint)

    def run_rest(self):

        self.app.run(
            host=self.host,
            port=self.http_port
        )

Thanks to craigglennie.com.

All 4 comments

There's not. The main reason is that Blueprints have been entirely added by the community and I don't personally feel comfortable writing a Blueprints quickstart/overview. I would love if someone would add one.

I wonder to know opinions on this draft
https://github.com/xmm/flask-restful-example

Draft looks good to me but could be simpler. How about making a patch to integrate it with the docs? For now, don't use multiple API versions to keep it simple.

This is awesome. :tada:

However, it didn't exactly worked / was clear for me.
For the lost souls that will come here, I managed to do it like this:

from flask import Flask, Blueprint
from flask_restful import Api
...

class FlaskServer:

    API_VERSION = 0.1
    API_URL_PREFIX = '/api/v%s' % API_VERSION

    api_blueprint = Blueprint('api', __name__)

    ...

    def __init__(self, host, http_port):

        self.host = host
        self.http_port = http_port
        ...

        # Flask Restuful API
        self.api = Api(
            app=self.api_blueprint,
            prefix=self.API_URL_PREFIX
        )

        # grouping all 'self.api.add_resource()' definitions
        self._init_api_resources()

        # only after the api resources where added
        self.app.register_blueprint(self.api_blueprint)

    def run_rest(self):

        self.app.run(
            host=self.host,
            port=self.http_port
        )

Thanks to craigglennie.com.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trnc-ck picture trnc-ck  ·  3Comments

svvitale picture svvitale  ·  8Comments

ueg1990 picture ueg1990  ·  4Comments

RealJTG picture RealJTG  ·  5Comments

lra picture lra  ·  4Comments