Register Callbacks

Hello,

I try to relatively dynamically register a data source for my chatbot.

The datasource should be an input. My idea was the following.

  1. I have a class with the method:
class ChatBot(DashboardItemResponsive):

    class ids:
        chat_input = lambda page_id, component: {
            "component": component,
            "name": "chat_input",
            "page_id": page_id,
        }
        submit_chat = lambda page_id, component: {
            "component": component,
            "name": "submit_chat",
            "page_id": page_id,
        }
        answer = lambda page_id, component: {
            "component": component,
            "name": "answer",
            "page_id": page_id,
        }

    ids = ids
    
    mapping_data_types = {
        'figure': 'graph'
    }
    
    @staticmethod
    def encode_image(image_path):
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
        
    @staticmethod
    def get_signatures(
        signature_type, signiture_dict
    ):
        signatures = {}

        signatures = {
            source: signature_type(key ,source)
            for key, source in signiture_dict.items()
        }
        return signatures
    
    @staticmethod 
    def register_data_source(data_dict):
        signiture_Dict = copy.deepcopy(data_dict)
        @callback(
            Output(ChatBot.ids.answer(MATCH, 'ChatBot'), "children"),
            inputs=dict(
            n_clicks=Input(ChatBot.ids.submit_chat(MATCH, 'ChatBot'), "n_clicks"),
            question=State(ChatBot.ids.chat_input(MATCH, 'ChatBot'), "value"),
            data=ChatBot.get_signatures(signature_type=State, signiture_dict=signiture_Dict),
            socket_id=State(GeneralIDs.socket("base"), "socketId"),
            ),
            prevent_inital_call=True
        )
        def ask_question(n_clicks, question, data, socketId):
            # First we need to make an image/png 
            
            print(data)
            raise PreventUpdate

In the file where I want to use the chatbot I would do something like this:

ChatBot.register_data_source({
    TradeActivityGraph.ids.chart(MATCH): "figure"
})

Like this, I can add for pages a Chatbot instance with different data sources, similar to the flask pattern of .init_app()

Currently, the “TradeActivityGraph.ids.chart(MATCH)” is an issue, I think. Since its unusable.

Is there a similar pattern which I don’t know of to get it working like this? :smiley: Maybe someone had a similar issue before.

Thank you