I am trying to implement a dcc.Input
component with suggestions based on text similarity. The goal is for suggestion to still appear even if a user misspells a word, for example. The minimal working example (which is modified from here) below illustrates my problem.
from dash import ALL, callback, Dash, dcc, html, Input, Output
from difflib import get_close_matches
questions = ['Why is the sky blue?',
'Why is the grass green?',
'Who is that walking over there?',
'Who is that running right here?'
'What is your name?',
'What is your hair style?',
'What is your eye color?']
app = Dash()
app.layout = html.Div([
html.Datalist(
id='datalist-questions',
children=[html.Option(value=question) for question in questions]
),
dcc.Input(id='input-questions',
type='text',
list='datalist-questions',
value='',
placeholder='Type question here...',
style={'width': '40%'}
)
])
@callback(
Output('datalist-questions', 'children'),
Input('input-questions', 'value'))
def provide_search_results(text):
if text:
matches = get_close_matches(word=text, possibilities=questions, n=3, cutoff=0.6)
print('-'*10)
print(text)
print(matches)
return [html.Option(value=match) for match in matches]
if __name__ == '__main__':
app.run_server(debug=True)
If the user types “Why is the”, then they see the possible questions based on matching text:
However, if the user misspells one of the words (in this case “thee” instead of “the”), the suggestions disappear despite there still being matching text:
The goal would be for there to still be suggestions based on text similarity.
I googled “python dash html datalist with fuzzy match” which led me to this stackoverflow question, where I learned that the behavior I am trying to achieve is not possible with html.Datalist
.
Therefore, my question is: Is there some other component/pattern that would achieve what I am trying to do?