Example #1
0
    def test_improper_configuration(self):
        app = flask.Flask(__name__)
        app.debug = True
        BanditMiddleware().init_app(app)

        app.register_blueprint(self.bp)
        app_client = app.test_client()

        with self.assertRaises(MABConfigException):
            rv = app_client.get("/")

        with self.assertRaises(MABConfigException):
            app_client.set_cookie("localhost", "MAB",
                                  '{"some_bandit": "blue"}')
            app_client.get("/reward")
Example #2
0
    def test_proper_configuration(self):
        app = flask.Flask(__name__)
        BanditMiddleware().init_app(app)

        app.add_bandit("some_bandit",
                       makeBandit("EpsilonGreedyBandit", epsilon=1.0))
        app.register_blueprint(self.bp)
        app_client = app.test_client()

        rv = app_client.get("/")
        assert parse_cookie(rv.headers["Set-Cookie"])["MAB"]

        assert "X-MAB-Debug" in rv.headers.keys()
        chosen_arm = self.get_arm(rv.headers)["some_bandit"]
        assert app.extensions['mab'].bandits["some_bandit"][chosen_arm][
            "pulls"] > 0
        assert json.loads(parse_cookie(
            rv.headers["Set-Cookie"])["MAB"])["some_bandit"] == chosen_arm

        app_client.get("/reward")
        assert app.extensions['mab'].bandits["some_bandit"][chosen_arm][
            "reward"] > 0
Example #3
0
    def setUp(self):
        app = flask.Flask(choice(['test_app', 'some_other_app']))
        BanditMiddleware().init_app(app)
        app.debug = True
        name, bandit = self.banditFactory(app)
        self.name_to_test = name
        app.add_bandit(name, bandit)

        @app.route("/")
        def root():
            return flask.make_response("Hello!")

        @app.route("/show_btn_decorated")
        @choose_arm(self.name_to_test)
        def assign_arm_decorated(**kwargs):
            return flask.make_response("assigned an arm")

        @app.route("/reward_decorated")
        @reward_endpt(self.name_to_test, 1.0)
        def reward_decorated():
            return flask.make_response("awarded the arm")

        self.app = app
        self.app_client = app.test_client()
color_bandit.add_arm("blue", "blue")

txt_bandit = ThompsonBandit()
txt_bandit.add_arm("casual", "Hey dude, wanna buy me?")
txt_bandit.add_arm("neutral", "Add to cart")
txt_bandit.add_arm("formal", "Good day sir... care to purchase?")

from flask import Flask, render_template
from flask_mab import BanditMiddleware, choose_arm, reward_endpt

app = Flask('test_app',
            template_folder="./example/templates",
            static_folder="./example/static")
app.config['MAB_STORAGE_ENGINE'] = 'JSONBanditStorage'
app.config['MAB_STORAGE_OPTS'] = ('./example/bandit_storage.json', )
mab = BanditMiddleware()
mab.init_app(app)  #bandit storage from previous code block
app.add_bandit('color_btn',
               color_bandit)  #our bandits from previous code block
app.add_bandit('txt_btn', txt_bandit)


@app.route("/")
@choose_arm("color_btn")
@choose_arm("txt_btn")
def home(color_btn, txt_btn):
    """Render the btn using values from the bandit"""
    return render_template("ui.html", btn_color=color_btn, btn_text=txt_btn)


@app.route("/btnclick")