In [1]:
from IPython.display import HTML
from ipywidgets import widgets, HBox, VBox
from IPython import display
import numpy as np
import pandas as pd
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
Out[1]:
In [2]:
metrics = ('adi', 'bearing', 'area', 'orientation', 'forward_spread', 'lateral_spread', 'structure' )
simulators = ('phoenix', 'australis', 'spark', 'prometheus')
scales = []
for attrib in sorted(metrics):
scales.append(widgets.FloatSlider(
value=0.5,
min=0.0,
max=1.0,
step=0.1,
margin=10,
padding=10,
height=100,
orientation='vertical',
description=attrib))
button = widgets.Button(description='calculate', margin=70, width=100, color='blue')
In [3]:
data = pd.DataFrame(np.random.uniform(-1, 1, (len(simulators), len(metrics))), columns=sorted(metrics), index=simulators)
styles = [
dict(selector="th", props=[("font-size", "100%"),
("padding", "20px"),
("width", "200px"),
("border", '0px'),
("text-align", "center")]),
dict(selector="tr", props=[("border", '0px')]),
dict(selector="td", props=[("font-size", "100%"),
("padding", "20px"),
("text-align", "center")]),
]
(data.style
.set_table_styles(styles)
.background_gradient(cmap='seismic', low=5.0, high=5.0)
.set_precision(3))
Out[3]:
In [6]:
def comparison(args):
weights = [x.value for x in scales]
reduction = pd.DataFrame((data * weights).mean(1), columns=['aggregate score'])
display.clear_output(True)
display.display(reduction.style
.set_table_styles(styles)
.background_gradient(cmap='seismic', low=5.0, high=5.0)
.highlight_null('red')
.set_precision(2))
return None
for slider in scales:
slider.observe(comparison, 'value')
In [7]:
d = HBox(scales)
d
In [ ]: