Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove client side cookie parse #49

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions flask_mab/bandits.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from random import random, choice, uniform, betavariate
from math import log, exp, expm1
from math import log, exp


class Bandit(object):
Expand Down Expand Up @@ -178,7 +178,7 @@ def __init__(self, tau=0.1):
def _compute_weights(self):
weights = []
total_reward = sum([exp(x / self.tau) for x in self.confidence])
for ind, n in enumerate(self.pulls):
for ind, _ in enumerate(self.pulls):
weights.append(exp(self.confidence[ind] / self.tau) / total_reward)
return weights

Expand Down
3 changes: 2 additions & 1 deletion flask_mab/debug_panels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import current_app
from flask import current_app, request
from flask_debugtoolbar.panels import DebugPanel
from jinja2 import PackageLoader, ChoiceLoader
import json
Expand Down Expand Up @@ -45,4 +45,5 @@ def content(self):
context["storage_engine"] = current_app.config.get("MAB_STORAGE_ENGINE")
context["storage_opts"] = current_app.config.get("MAB_STORAGE_OPTS", tuple())
context["bandits"] = current_app.extensions["mab"].bandits.items()
context["assigned"] = request.bandits
return self.render("panels/mab-panel.html", context)
30 changes: 10 additions & 20 deletions flask_mab/templates/panels/mab-panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#flDebug .panelContent .mab-op-table,
#flDebug .panelContent .mab-stack-trace table { display: table;}


.mab-op-table td.fifty {width: 50%; word-wrap: break-word;}
.mab-op-table td.thirty {width: 30%; word-wrap: break-word;}

Expand Down Expand Up @@ -62,7 +63,7 @@
</table>

{% for name,bandit in bandits %}
<h4>{{name}}:</h4>
<h4>{{name}}</h4>
Green table rows are values for this request
<table class="mab-op-table">
<colgroup>
Expand All @@ -80,7 +81,14 @@ <h4>{{name}}:</h4>
</tr>
</thead>
{% for arm in bandit.arms %}
<tr id="bandit-{{name}}-{{arm}}" class="{{ loop.cycle('flDebugOdd', 'flDebugEven') }}">
<tr id="bandit-{{name}}-{{arm}}"
{% if arm == assigned[name][0] %}
class="winner"
{% else %}
class="{{ loop.cycle('flDebugOdd', 'flDebugEven') }}"
{% endif %}
>

<td>{{ arm }}</td>
<td>{{bandit[arm]['pulls']}}</td>
<td>{{bandit[arm]['reward']}}</td>
Expand All @@ -89,21 +97,3 @@ <h4>{{name}}:</h4>
{% endfor %}
</table>
{% endfor %}

<script type="text/javascript" src="/_debug_toolbar/static/js/jquery.js"></script>
<script>
(function($) {

function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}

var mab_cookie = JSON.parse(eval(getCookie("{{cookie_name}}")));
for(var name in mab_cookie){
$('#bandit-'+name+'-'+mab_cookie[name]).addClass("winner");
}

})(jQuery.noConflict(true));
</script>
Loading