question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Methods as event handlers not called?

See original GitHub issue

It seems that event handling methods on custom components are not called. It seemed only natural (given the function signature) and useful to have methods as method handlers.

This code illustrates the problem:

import justpy as jp

class MyInlineHello(jp.Div):

    def __init__(self, **kwargs):
        self.counter = 1
        super().__init__(**kwargs)
        self.classes = 'm-1 p-1 text-2xl text-center text-white bg-blue-500 hover:bg-blue-800 cursor-pointer'
        self.text = 'Inline Hello! (click me)'

        async def click(self, msg):
            print('In INLINE CLICK handler')
            self.text = f'Hello! I was clicked {self.counter} times'
            self.counter += 1

        self.on('click', click)


class MyMethodHello(jp.Div):

    async def click(self, msg):
        print('In METHOD CLICK handler')
        self.text = f'Hello! I was clicked {self.counter} times'
        self.counter += 1

    def __init__(self, **kwargs):
        self.counter = 1
        super().__init__(**kwargs)
        self.classes = 'm-1 p-1 text-2xl text-center text-white bg-orange-500 hover:bg-orange-800 cursor-pointer'
        self.text = 'Method Hello! (click me)'

        self.on('click', self.click)




def hello_test():
    wp = jp.WebPage()
    for i in range(5):
        wp.add(MyInlineHello()) # or jp.Hello(a=wp)
    for i in range(5):
        wp.add(MyMethodHello()) # or jp.Hello(a=wp)
    return wp

jp.justpy(hello_test)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
mjmarecommented, Apr 27, 2020

That construct defeats the purpose of simplicity and I’d be hesitant to use it for fear of not understanding my own code after a few months 😉 If normal methods would be possible one would get the ‘self’ reference for free. As it stands now I’ll stick to the inline version

async def button_click(comp, msg):  # using comp instead of self to prevent hiding the object's self.
        self.user_state = next_state(self.user_state)

this had the advantage of simplicity and still have acces to self (if one uses another name than self for the first arg).

0reactions
elimintzcommented, May 9, 2020

Thank you, and keep the suggestions coming!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Event handler not firing on class method - Stack Overflow
From my (winforms, c#, . net 3.5) UI form, I'm trying to get the received message(s) back to the UI. If I call...
Read more >
Non Aggregate @EventHandler methods are not getting called.
Hi All, I am facing strange issue, i have same @EventHandler method with same event class in Aggregate and in non Aggregate class,...
Read more >
Event listeners not working? 3 key areas to troubleshoot
Are your event listeners not working as you'd expect? Here are 3 key areas to troubleshoot to help you get everything triggering as...
Read more >
Handling and Raising Events | Microsoft Learn
These delegates have no return type value and take two parameters (an object ... The following example shows an event handler method named...
Read more >
4 Working with Event Handlers (Release 8)
This method takes the event type and the handler as arguments. In Example 4-1, the first handler is added to a single node...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found