Hi everyone,
I am trying to implement a dash app to explore datasets and one of the steps is being able to make cuts of a continuous variable to transform it to categorical.
To do this, I have one tab for each dataset column and when you select a column a slider is shown with the following properties set via callback:
-min: minimum value of the column
-max: maximum value of the column
-value (ticks): 10%, 25%, 50%, 75% and 90% quantiles.
However after changing columns multiple times you will see that the ticks of the slider often appear outside the bounds. I am honestly lost on how to fix this, can anyone help me?
See the attached picture with an example on how the slider is showing after clicking on different columns 4-5 times.
Full working code for a test:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])
app = dash.Dash()
app.layout = html.Div([
dcc.Tabs(
id="select-assumption", value=df.columns[0],
children=[dcc.Tab(label=col, value=col) for col in df.columns]
),
dcc.RangeSlider(id='slider-keeper', min=-10, max=10, value=[-5,5],
dots=False, step=0.01, updatemode='drag', allowCross=False),
], style={'maxWidth': '500px', 'margin': 'auto'})
@app.callback(Output('slider-keeper', 'min'),
[Input('select-assumption', 'value')])
def update_slider_example_min(input):
min_value = np.min(df[input])
print('Fired update MIN: {}'.format(min_value))
return min_value
@app.callback(Output('slider-keeper', 'max'),
[Input('select-assumption', 'value')])
def update_slider_example_max(input):
max_value = np.max(df[input])
print('Fired update MAX: {}'. format(max_value))
return max_value
@app.callback(Output('slider-keeper', 'value'),
[Input('slider-keeper', 'max'),
Input('slider-keeper', 'min')],
[State('select-assumption', 'value')])
def update_slider_example_value(input1, input2, column):
q = df[column].quantile([.1, .25, .5, .75, .9]).values
print('Fired update TICKS: {}'.format(q))
return q
if __name__ == '__main__':
app.run_server(debug=True)
Any insight on what’s happening is appreciated.
Thank you