Ok, I have a MongoDB database, using a Python module to perform CRUD functions, and passing the resulting info into a Dash app.
Here is the relevant Python method:
def read(self, query):
results = self.database.animals.find(query)
return results
and the callback function I need help with:
@app.callback(Output('datatable-id','data'),
[Input('rescue-dropdown', 'value')],
prevent_initial_call=True)
def update_dashboard(value):
#start case
df = pd.DataFrame.from_records(shelter.read({}))
# filter interactive data table with MongoDB queries
if (value == 'Water Rescue'):
df = pd.DataFrame.from_records(shelter.read({'animal_type': 'Dog'}))
df.drop(columns=['_id'],inplace=True)
data=df.to_dict('records')
return (data)
All of this works as intended so far. The issue comes when I try to add more query filters as arguments in shelter.read(). How do I either amend the Python method to accept multiple arguments or change something in the callback function to allow for it?
For this dropdown value (‘Water Rescue’), the query filters are:
{'animal_type': 'Dog'}, {'breed': {'$in':['Labrador Retriever Mix', 'Chesapeake Bay Retriever', 'Newfoundland']}}, {'sex_upon_outcome': 'Intact Female'}, {'age_upon_outcome_in_weeks': {'$gte': '26'}}, {'age_upon_outcome_in_weeks': {'$lte': '156'}}
I tried changing the query parameter in the Python method to **kwargs or *args, but either did it incorrectly or that’s not the answer.
I can include more code if necessary but since there are no issues until I try to add more filters I assume this is where the problem occurs.
Chelaine Echols is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.