dccSlider

dccSlider is a component for rendering a slider. Users interact with a dccSlider by selecting areas on the rail or by dragging the handle.

The points displayed on a slider are called marks. They are autogenerated if not explicitly provided or turned off.

Examples

Find a few usage examples below.

Simple Slider Example

An example of a simple slider tied to a callback. The callback takes the slider’s currently selected value and outputs it to a html.Div.

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()

app$layout(
  htmlDiv(
    list(
      dccSlider(
        id='my-slider',
        min=0,
        max=20,
        step=0.5,
        value=10
      ),
      htmlDiv(id='slider-output-container')
    )
  )
)

app$callback(
  output(id = 'slider-output-container', property = 'children'),
  params=list(input(id = 'my-slider', property = 'value')),
  function(value) {
    sprintf("you have selected %0.1f", value)
  })

app$run_server()

Marks and Steps

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. marks is a list
where the keys represent the numerical values and the values represent their labels.

library(dashCoreComponents)

dccSlider(
  min=0,
  max=10,
  marks = list(
    "0" = "0°F",
    "3" = "3°F",
    "5" = "5°F",
    "7.65" = "7.65°F",
    "10" = "10°F"
  ),
  value=5
)

Included and Styling Marks

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)

dccSlider(
  min=0,
  max=100,
  value = 65,
  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"))
  )
)
library(dashCoreComponents)
dccSlider(
    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"))
    ),
    included=FALSE
)

Non-Linear Slider and Updatemode

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

transform_value = function(value){
  return(10**value)
}

app <- Dash$new()

app$layout(
  htmlDiv(
    list(
      dccSlider(
        id='slider-updatemode',
        marks=unlist(lapply(list(1:4), function(x){10**x})),
        max=3,
        value=2,
        step=0.01,
        updatemode='drag'
      ),
      htmlDiv(id='updatemode-output-container', style=list('marginTop' = 20))
    )
  )
)

app$callback(
  output(id = 'updatemode-output-container', property='children'),
  params=list(input(id='slider-updatemode', property='value')),
  function(value) {
    sprintf('Linear Value: %g | Log Value: %0.2f', value, transform_value(value))
  })

app$run_server()

Slider Tooltips

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’. If 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.Slider(0, 10, 1, value=5, marks=None,
    tooltip={"placement": "bottom", "always_visible": True})

Styling Tooltips

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.Slider(0, 10,
    1,
    value=5,
    marks=None,
    tooltip={
        "always_visible": True,
        "style": {"color": "LightSteelBlue", "fontSize": "20px"},
    },
),

Transforming Tooltip Values

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.Slider(
    0,
    10,
    step=None,
    marks={0: "0°F", 3: "3°F", 5: "5°F", 7.65: "7.65°F", 10: "10°F"},
    value=5,
    tooltip={"always_visible": True, "transform": "temperatureInCelsius"},
)

Formatting Tooltips

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.Slider(0, 10,
    1,
    value=5,
    marks=None,
    tooltip={
        "always_visible": True,
        "template": "$ {value}"
    },
),

Slider Properties

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 (numeric; optional):
The value of the input.

drag_value (numeric; optional):
The value of the input during a drag.

disabled (logical; optional):
If TRUE, the handles can’t be moved.

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 value.

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. If
you want different actions during and after drag, leave updatemode
as mouseup and use drag_value for the continuously updating value.

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.