Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown component for Dash that supports clicking on selected items

Dash has a nice Dropdown component that supports multi-value selection. In my application, the user can do data segmentation via multiple dropdown menus to choose properties "A", "B", "C", "D", and "E". It would be useful to also allow the user to segment based on negations of properties, say "not A" and "not E".

Is there already a Dropdown component available that would support clicking on items (i.e., when a user clicks on the selected property "A", we could make the element turn red to denote "not A" is now selected)? For example, suppose the user clicks on "New York City" in the multi-value dropdown example, and the corresponding "box" containing it changes color.

The Dash documentation explains how to create your own component but this doesn't cover the scenario where you simply want to extend an existing component. If there's no such component available, how would I go about making one?

Further background:

  • There's a discussion on the plotly forum where a user explains how an existing component can be extended. The major downside with this is that one needs to update and rebuild the whole dash-core-components library every time one makes a change to the component or a new dash version is released.

  • There's an issue open on dash-docs for writing a tutorial to cover this case with no apparent progress so far.

like image 418
Juho Avatar asked Apr 15 '20 08:04

Juho


People also ask

What is the use of options in dropdown?

Dropdown is an interactive dropdown element for selecting one or more items. The values and labels of the dropdown items are specified in the 'options' property and the selected item (s) are specified with the 'value' property. Use a dropdown when you have many options (more than 5) or when you are constrained for space.

How to manage the show/hide button of a dropdown menu in react?

The behavior of the dropdown is when users click on the input, it will show the menu. And when users click outside or in the input itself, the menu will be hidden again. Line 14: We will manage show/hide by showMenu state value. Don’t forget to import useState from react Line 24–27: This is the handler for input click.

How to prevent event bubbling when users click on dropdown input?

That’s why we need to prevent this trigger ( prevent Event Bubbling to the window) when users click on the dropdown input by using e.stopPropagation (); Line 43: If the showMenu is true, the menu element will be rendered In this step, we will allow users to select an option and display the value they select in the dropdown input.

How do I trigger a dropdown menu?

Trigger dropdown menus above elements by adding direction="dropup" to the <CDropdown> component. Trigger dropdown menus at the right of the elements by adding direction="dropend" to the <CDropdown> component. Trigger dropdown menus at the left of the elements by adding direction="dropstart" to the <CDropdown> component.


Video Answer


1 Answers

There is a way to achieve this in native Dash by effectively making a drop down checklist. This is achieved by using dash_html_components.Details and dash_html_components.Summary. This is native in HTML5.

Sample code:

import datetime
import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html

app = dash.Dash(external_stylesheets = [dbc.themes.BOOTSTRAP])
server = app.server

app.layout = dbc.Container(
    children=[
        html.Details([
        html.Summary('Select city...'),
        html.Br(),
        dbc.Col([
            dcc.Checklist(
                options=[
                    {'label': 'New York City', 'value': 'NYC'},
                    {'label': 'Montréal', 'value': 'MTL'},
                    {'label': 'San Francisco', 'value': 'SF'}
                    ],
                value=['NYC', 'MTL'],
                labelStyle = {'display': 'block'}
                )  
            ])
        ])
    ])

if __name__ == '__main__':
    app.run_server(debug=True)

Dropdown closed

Dropdown open

like image 102
Tom Avatar answered Oct 20 '22 20:10

Tom