Using dict for setting plotly Dash Slider marks results in syntax error

Viewed 536

I'm building a dashboard based on plotly Dash. One of the dcc is a Slider. If I want to show marks on the slider that are within a certain range of numbers, then this works just fine:

dcc.Slider(min=-10, max=20, step=1, value=0, marks={i: str(i) for i in range(-10, 20)})

But Dash documentation prefers to use dict notation. But if I do this:

dcc.Slider(min=-10, max=20, step=1, value=0, marks=dict(i = str(i) for i in range(-10,20)))

then I receive a Syntax Error

How can I implement this functionality using dict notation?

1 Answers

dict() can take in a list of key-value pairs. So you can do a list-comprehension inside your dict notation:

dcc.Slider(min=-10, max=20, step=1, value=0, marks=dict([(i,str(i)) for i in range(-10,20)]))

However I would still prefer the dict literal way, just for the slightly better readability and operation.

Related