✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

How to use @lru_cache when passing parameters through callback?

Hi, all, I need to link several charts, so I pass parameters through callback like this:

@app.callback(
    Output('table_products','figure'),
    [Input('gross_margin','selectedData'),
     Input('table_province','selectedData'),
     Input('table_city','selectedData'),
     Input('table_store','selectedData'),
     Input('table_category','selectedData'),
    ]
)
def calc_category_trace():
    process

It run correctly, but when I use decorator @lru_cache, I got an error:

TypeError: unhashable type: 'list'

how can I use @lru_cache or maybe can I pass parameters with a turple?
Thanks for the help!

For lru_cache to work all of the inputs need to be hashable, which means the nested lists and dictionaries you get from selectedData is not going to work. I would recommend not applying lru_cache to the whole callback, but rather defining a separate function that you decorate, then calling that from inside the callback with arguments pulled out from selectedData.

Something like

@lru_cache()
def slow_computation(...):
    # do something slowly
    pass

@app.callback(
    Output('table_products','figure'),
    [Input('gross_margin','selectedData'),
     Input('table_province','selectedData'),
     Input('table_city','selectedData'),
     Input('table_store','selectedData'),
     Input('table_category','selectedData'),
    ]
)
def calc_category_trace(...):
    # extract relevant, hashable data, e.g.
    state = selected_data["points"][0]["x"]

    # pass hashable, extracted data to decorated function
    slow_computation(state, ...)

OK, I got it. Thank you very much @tcbegley for your replying.