dccRangeSlider
is a component for rendering a range slider. Users interact with a dccRangeSlider
by selecting areas on the rail or by dragging handles.
The points displayed on a dccRangeSlider
are called marks
. In Dash 2.1 and later, they are autogenerated if not explicitly provided or turned off.
Find a few usage examples below.
An example of a simple dccRangeSlider
tied to a callback. The callback takes the dccRangeSlider
‘s currently selected range and outputs it to a html.Div
.
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccRangeSlider(
id='my-range-slider',
min=0,
max=20,
step=0.5,
value=list(5, 15)
),
htmlDiv(id='output-container-range-slider')
)
)
)
app$callback(
output(id = 'output-container-range-slider', property='children'),
params=list(input(id='my-range-slider', property='value')),
function(value) {
sprintf('You have selected [%0.1f, %0.1f]', value[1], value[2])
})
app$run_server()
If slider marks
are defined and step
is set to NULL
then the slider will only be able to select values that have been predefined by the marks
.
library(dashCoreComponents)
dccRangeSlider(
min=0,
max=10,
marks=list(
"0" = "0°F",
"3" = "3°F",
"5" = "5°F",
"7.65" = "7.65°F",
"10" = "10°F"
),
value=list(3, 7.65)
)
By default, included=TRUE
, meaning the rail
trailing the handle will be highlighted. To have the handle act as a
discrete value, set included=False
.
To style marks
, include a style CSS attribute alongside the key value.
library(dashCoreComponents)
dccRangeSlider(
min=0,
max=100,
marks = list(
"0" = list("label" = "0°C", "style" = list("color" = "#77b0b1")),
"26" = list("label" = "26°C"),
"37" = list("label" = "37°C"),
"100" = list("label" = "100°C" , "style" = list("color" = "#FF4500"))
)
)
To create multiple handles, define more values for value
.
library(dashCoreComponents)
dccRangeSlider(
min=0,
max=30,
value=list(1, 3, 4, 5, 12, 17)
)
The pushable
property is either a boolean
or a numerical value.
The numerical value determines the minimum distance between
the handles. Try moving the handles around!
library(dashCoreComponents)
dccRangeSlider(
min=0,
max=30,
value=list(1, 3, 4, 5, 12, 17),
pushable = 2
)
To prevent handles from crossing each other, set allowCross=FALSE
.
library(dashCoreComponents)
dccRangeSlider(
min=0,
max=30,
value=list(10,15),
allowCross = FALSE
)
Create a logarithmic slider by setting marks
to be logarithmic and
adjusting the slider’s output value
in the callbacks. The updatemode
property allows us to determine when we want a callback to be triggered.
The following example has updatemode='drag'
which means a callback is
triggered everytime the handle is moved.
Contrast the callback output with the first example on this page to see
the difference.
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
transform_value = function(value){
return(10 ** value)
}
app <- Dash$new()
transform_value = function(value){
10 ** value
}
app$layout(
htmlDiv(
list(
dccRangeSlider(
id='non-linear-range-slider',
marks=unlist(lapply(list(1:4), function(x){10**x})),
max=3,
value=list(0.1, 2),
dots=FALSE,
step=0.01,
updatemode='drag'
),
htmlDiv(id='output-container-range-slider-non-linear', style=list('marginTop' = 20))
)
)
)
app$callback(
output(id = 'output-container-range-slider-non-linear', property='children'),
params=list(input(id='non-linear-range-slider', property='value')),
function(value) {
transformed_value = lapply(value, transform_value)
sprintf('Linear Value: %g, Log Value: [%0.2f, %0.2f]', value[2],transformed_value[1], transformed_value[2])
})
app$run_server()
The tooltips
property can be used to display the current value. The placement
parameter
controls the position of the tooltip i.e. ‘left’, ‘right’, ‘top’, ‘bottom’ and always_visible=True
is used, then
the tooltips will show always, otherwise it will only show when hovered upon.
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import dcc
dcc.RangeSlider(0, 30, value=[10, 15],
tooltip={"placement": "bottom", "always_visible": True})
New in Dash 2.15
You can customize the style of tooltips with the tooltip.style
parameter. This accepts a dictionary of styles to apply. In this example, we set the text color and font size.
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import dcc
dcc.RangeSlider(0, 30,
value=[5, 15],
marks=None,
tooltip={
"placement": "bottom",
"always_visible": True,
"style": {"color": "LightSteelBlue", "fontSize": "20px"},
},
),
New in Dash 2.15
You can transform the value displayed on a tooltip using a JavaScript function by specifying the function name with the tooltip.transform
parameter.
To make a custom function available in your app, add it to a file in your app’s assets folder. The function needs to be available in the window.dccFunctions
namespace.
In this example, we have a function that converts temperatures in Fahrenheit to Celsius. This function is saved in assets/tooltip.js
:
window.dccFunctions = window.dccFunctions || {};
window.dccFunctions.temperatureInCelsius = function(value) {
return ((value - 32) * 5/9).toFixed(2);
}
We then pass this function name to the tooltip.transform
parameter:
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import dcc
dcc.RangeSlider(
0,
10,
value=[3, 7.65],
marks={0: "0°F", 3: "3°F", 5: "5°F", 7.65: "7.65°F", 10: "10°F"},
tooltip={"always_visible": True, "transform": "temperatureInCelsius"},
)
New in Dash 2.15
You can customize the text displayed on tooltips using the tooltip.template
parameter. This accepts a string, which must contain {value}
. The {value}
will be replaced with the string representation of the tooltip value, or with the value returned by a transform function if one was specified using tooltip.transform
(see the previous section).
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import dcc
dcc.RangeSlider(0, 30,
value=[5, 15],
marks=None,
tooltip={
"always_visible": True,
"template": "$ {value}"
},
),
Our recommended IDE for writing Dash apps is Dash Enterprise’s
Data Science Workspaces,
which has typeahead support for Dash Component Properties.
Find out if your company is using
Dash Enterprise.
min
(numeric; optional):
Minimum allowed value of the slider.
max
(numeric; optional):
Maximum allowed value of the slider.
step
(numeric; optional):
Value by which increments or decrements are made.
marks
(named list; optional):
Marks on the slider. The key determines the position (a number), and
the value determines what will show. If you want to set the style of a
specific mark point, the value should be an object which contains
style and label properties.
marks
is a named list with characters as keys and values of type
character | named list with keys:
label
(character; optional)
style
(named list; optional)
value
(unnamed list of numerics; optional):
The value of the input.
drag_value
(unnamed list of numerics; optional):
The value of the input during a drag.
allowCross
(logical; optional):
allowCross could be set as TRUE to allow those handles to cross.
pushable
(logical | numeric; optional):
pushable could be set as TRUE to allow pushing of surrounding handles
when moving an handle. When set to a number, the number will be the
minimum ensured distance between handles.
disabled
(logical; optional):
If TRUE, the handles can’t be moved.
count
(numeric; optional):
Determine how many ranges to render, and multiple handles will be
rendered (number + 1).
dots
(logical; optional):
When the step value is greater than 1, you can set the dots to TRUE if
you want to render the slider with dots.
included
(logical; optional):
If the value is TRUE, it means a continuous value is included.
Otherwise, it is an independent value.
tooltip
(named list; optional):
Configuration for tooltips describing the current slider values.
tooltip
is a named list with keys:
always_visible
(logical; optional):
Determines whether tooltips should always be visible (as opposed
to the default, visible on hover).
placement
(a value equal to: ‘left’, ‘right’, ‘top’, ‘bottom’, ‘topLeft’, ‘topRight’, ‘bottomLeft’ or ‘bottomRight’; optional):
Determines the placement of tooltips See
https://github.com/react-component/tooltip#api top/bottom{*} sets
the origin of the tooltip, so e.g. topLeft
will in reality
appear to be on the top right of the handle.
style
(named list; optional):
Custom style for the tooltip.
template
(character; optional):
Template string to display the tooltip in. Must contain {value}
,
which will be replaced with either the default string
representation of the value or the result of the transform
function if there is one.
transform
(character; optional):
Reference to a function in the window.dccFunctions
namespace.
This can be added in a script in the asset folder. For example,
in assets/tooltip.js
: window.dccFunctions =
window.dccFunctions || {}; window.dccFunctions.multByTen =
function(value) { return value * 10; }
Then in the
component tooltip={'transform': 'multByTen'}
.
updatemode
(a value equal to: ‘mouseup’ or ‘drag’; default 'mouseup'
):
Determines when the component should update its value
property. If
mouseup
(the default) then the slider will only trigger its value
when the user has finished dragging the slider. If drag
, then the
slider will update its value continuously as it is being dragged. Note
that for the latter case, the drag_value
property could be used
instead.
vertical
(logical; optional):
If TRUE, the slider will be vertical.
verticalHeight
(numeric; default 400
):
The height, in px, of the slider if it is vertical.
className
(character; optional):
Additional CSS class for the root DOM node.
id
(character; optional):
The ID of this component, used to identify dash components in
callbacks. The ID needs to be unique across all of the components in
an app.
loading_state
(named list; optional):
Object that holds the loading state object coming from dash-renderer.
loading_state
is a named list with keys:
component_name
(character; optional):
Holds the name of the component that is loading.
is_loading
(logical; optional):
Determines if the component is loading or not.
prop_name
(character; optional):
Holds which property is loading.
persistence
(logical | character | numeric; optional):
Used to allow user interactions in this component to be persisted when
the component - or the page - is refreshed. If persisted
is truthy
and hasn’t changed from its previous value, a value
that the user
has changed while using the app will keep that change, as long as the
new value
also matches what was given originally. Used in
conjunction with persistence_type
.
persisted_props
(unnamed list of values equal to: ‘value’; default ['value']
):
Properties whose user interactions will persist after refreshing the
component or the page. Since only value
is allowed this prop can
normally be ignored.
persistence_type
(a value equal to: ‘local’, ‘session’ or ‘memory’; default 'local'
):
Where persisted user changes will be stored: memory: only kept in
memory, reset on page refresh. local: window.localStorage, data is
kept after the browser quit. session: window.sessionStorage, data is
cleared once the browser quit.