From 2ec1de0068c6ef25874db0ad3c502d51bac261f3 Mon Sep 17 00:00:00 2001 From: DIMITRIOS CHRYSOCHERIS Date: Sun, 6 Oct 2024 16:35:20 +0300 Subject: [PATCH] topbar -> MenuWidget || execute option's function directly through MenuWidget instead of Widget --- lightweight_charts/topbar.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lightweight_charts/topbar.py b/lightweight_charts/topbar.py index 1580c9a..2e02861 100644 --- a/lightweight_charts/topbar.py +++ b/lightweight_charts/topbar.py @@ -63,11 +63,15 @@ class MenuWidget(Widget): def __init__(self, topbar, options, default, separator, align, func): super().__init__(topbar, value=default, func=func) self.options = list(options) + self.func = func # Store the function for later use self.run_script(f''' - {self.id} = {topbar.id}.makeMenu({list(options)}, "{default}", {jbool(separator)}, "{self.id}", "{align}") + {self.id} = {topbar.id}.makeMenu({self.options}, "{default}", {jbool(separator)}, "{self.id}", "{align}"); + {self.id}.onItemClicked = function(option) {{ + // Call the Python function associated with the clicked option + py_callback("{self.id}", option); + }}; ''') - # TODO this will probably need to be fixed def set(self, option): if option not in self.options: raise ValueError(f"Option {option} not in menu options ({self.options})") @@ -75,12 +79,22 @@ def set(self, option): self.run_script(f''' {self.id}._clickHandler("{option}") ''') - # self.win.handlers[self.id](option) + + # Execute the function associated with the selected option + if self.func: + self.func(option) # Call the function with the selected option def update_items(self, *items: str): self.options = list(items) self.run_script(f'{self.id}.updateMenuItems({self.options})') + # This method receives the callback from JavaScript to trigger the function + def py_callback(self, menu_id, option): + if option in self.options: + self.func(option) # Call the function associated with the selected option + else: + print(f"No function assigned to the option: {option}") + class ButtonWidget(Widget): def __init__(self, topbar, button, separator, align, toggle, disabled: bool = False,