Click button from server module

What I’m trying to do:
My Pi has a script running that pulls data from a sensor module every 5 seconds. Every 30 seconds it pushes this data to my Anvil app. When it calls the function in the server module I want a plot on my app homepage to update with the latest data. The homepage allows for manual data entry aswell so there is an “Update Plot” button which just pulls the data from a data table and plots it. I have a function called “button_update_plot_click()” to do this last bit.

I want to be able to “click” the button when new data is uploaded from the Pi by calling this function from my server code.

What I’ve tried and what’s not working:
I’ve tried simply calling “button_update_plot_click()” from my server code but am told that the function requires the positional argument self. As I don’t have a self in my server code, I’m not sure what to pass over.

Code Sample:

# In my server module
HomeForm.button_update_plot_click(???)

# In my HomeForm code
def button_update_plot_click(self, **event_args):
    self.build_plot()

What am I doing wrong here? I feel like there’s a very easy answer but I’ve not been able to find it after searching docs and the forum.

Thanks
jon

Only code running in the browser has access to the browser’s button_update_plot_click() function. All other code is running on different computers entirely, i.e., on the Anvil server, or in your Pi.

You’re far from alone. I was used to programming desktop PCs, where everything had access to everything else. Even using database servers, the application logic was still on the PC. The recognition that, with Anvil, things were split up across multiple computers, by necessity, took awhile for me to get my head around. The lack of a way to call “back” to the browser, even more so.

This means that, polling, from the browser side, is often the only practical option. Your browser code could start a Timer, and use that to poll server-resident data at regular intervals. For that matter, it could poll the Pi directly.

Hi Jon,

Just to reiterate what @p.colbert said, a Timer is probably what you want here. See this step from the Data Dashboard tutorial for an example of updating a plot in real time using a Timer.

1 Like

Thank you @p.colbert and @brooke.
I’ve chucked a timer on the plot and it’s updating happily every 30 seconds.
When did the “timer” component get implemented? I hadn’t noticed it before. In all honesty, I’ve not touched this app for a while but as it’s coming up to greenhouse growing time again I want my monitoring system up and running.

Thanks again.
jon

I think it’s been there forever. I remember using this technique in the very early days.

Oh… I’d never noticed it as a drag and drop feature. Shows how observant I’ve been!