How do event handlers work

Heya!
So firstly I apologize if I’m asking something quite obvious, but admittedly I am still learning coding and Python, and this is the first major project I’ve embarked on.

Currently, I’m trying to build a search function for a database. After querying data tables, I receive a series of values, which I then create into a list of links. Clicking on this link should take me to a separate form(the editing form) where I can then change some stuff around.

In order to this, I want to pass a value when each link is clicked, to a function that will then open up the editing form.

What I have right now, is something like this:

def open_editor(some_id, **event_args):
      anvil.server.call("store_id", some_id)
      open_form('edit_patient_data')
      return

# Further down the line

a = Link(text="SomeText")
a.set_event_handler('click', open_editor(some_id))

So this works… Somewhat? It works in that the appropriate data is stored, but fires immediately. I’m also completely confused on what **event_args is. I don’t understand what it is, except that it seems to be some kind of catch-all for a variety of back-end arguments? Either way, it seems to be something I have to include, or face the wrath of numerous error messages.

I found one other topic relating to this, but I genuinely couldn’t understand what was going on there, so I was hoping that someone more experienced could possibly tell me what I’m doing wrong. Either way, thanks in advance :))

Hello @pranavgn and welcome to the Anvil community.

With Anvil this result can be achieved in a few different ways. From what I can tell you are approaching this from a programmatic standpoint. The beauty of Anvil is that you can build things visually in a very quick and easy fashion.

So my approach would be to return your search results to the client and populate a repeating panel with each item in the search iterator. This means you can visually build a template (ie edit_patient_data form) to display the row in which ever fashion you choose.

A great example to build to help you learn this would be the News Aggregator example. It will help you learn the basics.

https://anvil.works/learn/tutorials/database-backed-apps

https://anvil.works/build#clone:RLPRLKUK3VPTTXUH%3DQSLBZG4MRQGVNS7CZZXJKBPQ

After that if you still wish to use Events, the documentation is an easy read that will give examples of how to use the Anvil features.

https://anvil.works/docs/overview

Just use the search bar to find what you are looking for.

If you are still stuck, post an update with what you have tried and the community will help you out.

2 Likes

I second the suggestion to go through the tutorials. You actually don’t need to create your links in code, Anvil will take care of that for you and make binding to events easier in the IDE.

But, to get to the heart of your original question, you’re passing set_event_handler the wrong thing for the second argument. What it wants is a function, not the result of calling a function. e.g.:

a.set_event_handler('click', open_editor)

And your open_editor would be defined without the some_id argument. Inside open_editor, you’d use event_args[‘sender’] to get the Link object that generated the click. From there you can get any information you’d previously stored on the Link object, e.g.:

a = Link(text="SomeText")
a.tag.some_id = some_id
a.set_event_handler('click', open_editor)

But all of that is handled differently (easier, in my opinion) if you use the IDE’s built in mechanisms for populating data grids/repeating panels, which are covered in the tutorials.

4 Likes