I am using the RangeSlider in Python Dash. This slider is supposed to allow users to select a range of dates to display, somewhere between the minimum and maximum years in the dataset. The issue that I am having is that each mark shows as 2k due to it being automatically rounded. The years range between 1784 and 2020, with a step of 10 each time. How do I get the marks to show as the actual dates and not just 2k? This is what I have below.
JavaScript
x
3
1
dcc.RangeSlider(sun['Year'].min(), sun['Year'].max(), 10,
2
value=[sun['Year'].min(), sun['Year'].max()], id='years')
3
Advertisement
Answer
You can use attribute marks
to style the ticks of the sliders as follows:
JavaScript
1
2
1
marks={i: '{}'.format(i) for i in range(1784,2021,10)}
2
The full code:
JavaScript
1
20
20
1
from dash import Dash, dcc, html
2
3
4
app = Dash(__name__)
5
6
app.layout = html.Div([
7
dcc.RangeSlider(1784, 2020,
8
id='non-linear-range-slider',
9
marks={i: '{}'.format(i) for i in range(1784,2021,10)},
10
value=list(range(1784,2021,10)),
11
dots=False,
12
step=10,
13
updatemode='drag'
14
),
15
html.Div(id='output-container-range-slider-non-linear', style={'margin-top': 20})
16
])
17
18
if __name__ == '__main__':
19
app.run_server(debug=True, use_reloader=False)
20