0
votes

I have a input box that accepts a search query string and a dropdown that populates based on response from the API call.

Instead of a call for every query stroke, I'd like to limit the api calls to only when user has entered 6 or more characters.

Is this a optional parameter that I can pass? Below is my code.

dbc.InputGroup(
           [
               dbc.InputGroupAddon("Property Address"),
               dcc.Input(id='address_autocomplete'),
               dcc.Dropdown(id='address_dropdown',style={'width':'60%'})
           ],
           style={'margin-top':'30px', 'width': '53%', 'float': 'left'},
       ),



# Property address autocomplete
@app.callback(Output('address_dropdown', 'options'),
              [Input('address_autocomplete', 'value')])

    def autocomplete_address(value):
    
        print(value)
    
        addr = {}
    
        # Call mapbox API and limit Autocomplete address suggestions
        ret_obj = geocoder.forward(value, lon=-112.0913905, lat=33.4514652, limit=3)
        response = ret_obj.json()
    
        for i in range(len(response['features'])):
    
            addr["res_{}".format(i)] = response['features'][i]['place_name']
    
        if value:
    
            return [{'label': addr['res_0'], 'value': addr['res_0']},
                    {'label': addr['res_1'], 'value': addr['res_1']},
                    {'label': addr['res_2'], 'value': addr['res_2']}]


# Function call
r = geocoder.forward('Washington Park', lon=-112.0913905, lat=33.4514652, limit=3)
rj = r.json()

# Return

Washington Park, Phoenix, Arizona 85015, United States
Washington Park Playground, Phoenix, Arizona 85015, United States
Washington Park, Chicago, Illinois 60637, United States

I am using the python client for mapbox web services : https://github.com/mapbox/mapbox-sdk-py

1

1 Answers

1
votes

You could just check the length of the arg, and only make the call if it's over 5. Otherwise prevent update.

@app.callback(Output('address_dropdown', 'options'),
              [Input('address_autocomplete', 'value')])
    def autocomplete_address(value):
        if len(value) < 6:
            raise dash.exceptions.PreventUpdate
        # the rest of the function
        ...