# Adding entries to Dropdowns

**URL:** <https://community.plotly.com/t/adding-entries-to-dropdowns/71908>\
**Category:** Dash Python\
**Tags:** question\
**Created:** [January 25, 2023, 2:34am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908 "2023-01-25T02:34:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 25, 2023, 2:34am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/1 "2023-01-25T02:34:28Z")

</div>

Hey,

I am creating a dashboard that is essentially a form where the user enters data which will then get saved to a database upon submitting. In the form I have multiple dropdowns that will allow the user to select options from. If an option is not in the dropdown, I would like to give the user a chance to manually type in an entry and hit enter. I have multi-selct dropdowns as well as dropdowns in Dash datatables and I would like each dropdown to have this functionality. How might I implement this?

Thanks!

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [January 25, 2023, 11:01am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/2 "2023-01-25T11:01:48Z")

</div>

Hi @Kmike,

you could add a `dcc.Input()` and a `Button` for each dropdown and update the drop-down options on click of the button.

You could create two Input() and one Button where the first Input() indicates the drop-down to change and the second Input() is the actual option you would like to add.

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 25, 2023, 1:10pm UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/3 "2023-01-25T13:10:28Z")

</div>

Hi,  
Thanks for the idea. However that seems a bit excessive for the number of drop downs I have on the page. Plus the drop  
-downs I have in the DataTables.

I think I’m going to make each drop-down contain an ‘Add new’ selection. If that selection is selected then I will create a pop up with an input box and an add button that allows the user to update the drop-down list. I haven’t tried this yet but when I do will try to post my results.

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [January 25, 2023, 1:19pm UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/4 "2023-01-25T13:19:59Z")

</div>

This sounds nice!

Please post your result here (or if you need assistance during the process)

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 27, 2023, 6:01am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/5 "2023-01-27T06:01:51Z")

</div>

I’m having trouble getting my callback to work. I narrowed it down to the if statement in my show\_modal function. For some reason this if statement is never true thus does not trigger the modal to pop up. Anyone have a reason why?

> Blockquote  
> @app.callback(  
> Output(‘dd-entry-modal’,‘is\_open’),  
> Input(‘tc\_dd’, ‘value’)  
> )  
> def show\_modal(value):  
> if value==‘Add New’:  
> return True

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 27, 2023, 6:20pm UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/6 "2023-01-27T18:20:57Z")

</div>

So I resolved my callback issue above by changing value to a list. However now I want to check the list to see if value contains the string ‘Add New’. I’ve been trying to iterate the list by checking if ‘Add new’ in value ==True but this is not working. I get no errors but the function never gets set to true. Here is my code:

> Blockquote  
> dcc.Dropdown([‘Dude1’, ‘AOB’,‘Add New’],id=‘tc\_dd’, multi=True, value=),  
> html.H6(id=‘test’),

> Blockquote  
> @app.callback(  
> Output(‘dd-entry-modal’,‘is\_open’),  
> Input(‘tc\_dd’, ‘value’),  
> )  
> def show\_modal(value):  
> if ‘Add New’ in value ==True:  
> return True

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [January 28, 2023, 11:36am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/7 "2023-01-28T11:36:19Z")

</div>

HI @Kmike,

Unfortunately I can’t help you because the code you provide does not contain enough information. Since I liked your idea of adding drop down options by selecting a certain, already existing option, I came up with a possible solution:

> <https://github.com/AIMPED/plotly_dash/blob/master/plotlyDash_add_drop_down_options_via_modal.py>

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 29, 2023, 12:33am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/9 "2023-01-29T00:33:22Z")

</div>

Thank you for this! However I have multi-selectable dropdowns as well. How should I implement this with the multi-selectable dropdowns. I have attempted to do this with my code, but for some reason I cannot get this to work.

```auto
from dash import Dash, html, dcc, dash_table
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc

app = Dash( __name__ )
app.layout = dbc.Container([
    dbc.Row(
        dbc.Col(
            (html.H6('People'),
             dcc.Dropdown(['Person 1', 'Person 2','Add New'],id='dd', multi=True, value=[]),
             html.H6(id='test'), #Used as a test output to print dropdown selection and its class
             dbc.Modal(
                     [
                         dbc.ModalHeader(children='Add New', id='modal-head'),
                         dbc.ModalBody(
                             [
                                 dbc.Label(id='modal-label'),
                                 dbc.Input(id="modal-input", type="text"),
                             ]
                         ),
                         dbc.ModalFooter(
                             [
                                 dbc.Button("Add", color="primary", id="modal-add"),
                                 dbc.Button("Cancel", id="modal-cancel"),
                             ]
                         ),
                     ],
                     id="dd-entry-modal", is_open=False
                 ),
            )
        )
    )
])
@app.callback(
    Output('dd-entry-modal','is_open'),
    Input('dd', 'value'),
)
def show_modal(value):
    if 'Add New' in value ==True:
        return True

#Callback to print dropdown input values and class
@app.callback(
    Output('test','children'),
    Input('dd', 'value'),
)

def test(value):
    return str(value)+' '+str(type(value))

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

```

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [January 29, 2023, 10:29am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/10 "2023-01-29T10:29:10Z")

</div>

HI @Kmike,

a few points:

- Since you want to use the `dbc.Modal()` you have to include a `dbc.theme` as external\_stylesheet when initiating the app.

- The reason for your modal not to show up is the line ` if 'Add New' in value ==True`. You have to delte the `==True` as the expression `if 'Add New' in value` returns True or False.

- There are variuos actions which could trigger the visibility of the modal:

In Dash you have to combine these three actions into one callback as they change the same property of a single component (`is_open`)

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [January 29, 2023, 10:49am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/11 "2023-01-29T10:49:54Z")

</div>

Hi @Kmike ,

This is the version for `multi=True`

> <https://github.com/AIMPED/plotly_dash/blob/master/plotlyDash_add_multi_drop_down_options_via_modal.py>

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [January 29, 2023, 4:04pm UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/12 "2023-01-29T16:04:39Z")

</div>

Thank you @AIMPED! You’ve been so helpful!

---

<div class="post-metadata">

**Author:** ![Kmike](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@Kmike](https://community.plotly.com/u/Kmike)\
**Post date:** [February 1, 2023, 3:56am UTC](https://community.plotly.com/t/adding-entries-to-dropdowns/71908/13 "2023-02-01T03:56:34Z")

</div>

@AIMPED  
I have one more question about this. Is there a way to implement this in a DataTable. I have multiple DataTables with dropdown entries as well. I have created a function to create the tables, but now I am trying to tie it to the callback. Here is the function that creates my DataTable.

```auto
#Draws a table with a Title, df=data, dd=list of dropdown columns
def drawTable(Title,df,dd):
    col=[] #initialize column list
    opt={} #initialize drop down options
    #Create columns and the associated dropdown options
    for i in df.columns:
        if i in dd: #dd stands for dropdown so if column is in the dropdown list execute this statement
            #ddc stands for dropdown column
            ddc={"name": [Title,i], "id": i, 'presentation':'dropdown'}
            col.append(ddc)
            #Create Dropdown options (ddo)
            ddo={i:{'options':[{'label':'Add New','value':'Add New'}]+[{'label':o, 'value':o} for o in df[i].unique()]}} 
            opt.update(ddo)       
        else:
            c={"name": [Title,i], "id": i}
            col.append(c)
    return html.Div([
        dash_table.DataTable(
        id={'type': 'tbl', 'index': Title},
        data=df.to_dict('records'),
        columns=col,
        editable=True,
        row_deletable=True,
        dropdown=opt,
        style_data={'color': 'black','backgroundColor': '#CDE5E8'},
        style_data_conditional=[{'if': {'row_index': 'odd'},'backgroundColor': 'rgb(232, 242, 244)'}],
        style_header={'backgroundColor': 'rgb(46, 179, 190)','color': 'white','fontWeight': 'bold','textAlign': 'center'},
        merge_duplicate_headers=True,
        ),
    dbc.Button('Add Row', id={'type': 'btn', 'index': Title}, n_clicks=0,style= {'backgroundColor': 'rgb(46, 179, 190'}, class_name='mt-0 mb-1')
])

```
