Running Python function from button click

802 views
Skip to first unread message

Jacob Fullerton

unread,
Jul 29, 2016, 5:07:58 PM7/29/16
to Tethys Platform
So has anyone ever successfully ran a python function from a button click? I know that with HTML buttons I can call the javascript function and then from there I could use an ajax request to take advantage of python, but what if I wanted to skip the ajax and go directly to the python function from my HTML button click? How can I make the HTML see the function that would live inside of my controller.py?

The scenario is that I am trying to take advantage of the message_box gizmo that tethysplatform has provided by giving the user feedback about what they need to do in order to run the program. If there is an incorrect value in the input, I want to be able to pass the whole form with several fields from the HTML into python using a button so that I can verify things there. If there is something wrong I would then want to be able to toggle on the message box gizmo and have the error message updated depending on what error the user has thrown. If everything passes, then I would be able to move on and perform another function/model simulation.

Any ideas?

alansnow21

unread,
Jul 29, 2016, 5:55:01 PM7/29/16
to Tethys Platform
What you are talking about is a form: https://github.com/erdc-cm/tethysapp-dam_break/wiki/Intermediate-Tutorial#3-create-form-to-submit-to-hydrograph-page which you would then have to redirect back to the original page with new data in the request.

Personally, I would recommend using AJAX to talk to the controller and set an event with the button with jQuery so you don't have to reload the entire page. But, what you are talking about is possible.

Good luck!

alansnow21

unread,
Jul 29, 2016, 5:56:33 PM7/29/16
to Tethys Platform
Also, I would use the GET method for the form to do what you are talking about, not POST.

Jacob Fullerton

unread,
Jul 29, 2016, 6:06:10 PM7/29/16
to Tethys Platform
Thanks Alan, I'll poke around with this and see what I find out. The purpose of not using the AJAX request is to reduce the need to create my own js library for calling a modal window to provide feedback to the user. Though the AJAX request does eliminate the need for a js library, it also requires more code than it would to directly verify my variables. I'd much rather use the python message_box gizmo and I may have a nice way of doing it, but I was stuck on making the linkage work out. I'll post my success/failure in the next week.

One last question before I dive in though, is there any possible way for my HTML to pull information from the map_view (e.g. point locations) before pushing stuff to python?

alansnow21

unread,
Jul 29, 2016, 6:40:58 PM7/29/16
to Tethys Platform
Here are my reasons why I still recommend using some JS/AJAX:
1) The AJAX request does not need that much code: https://api.jquery.com/jquery.get 
2) There is no requirement to validate the code on the JS side. You can reload a subset HTML page with AJAX with only your request and replace that HTML in your form with the errors generated from your controller. 

Here is an example for loading HTML from AJAX combined with gizmos:

To answer your next question:
A) If you load in points before the page loads (i.e. from the controller/template) you just pass the info to the template from the controller
B) If you need to load points in from input from the user or other information after the page loads, I don't think you are going to be able to eliminate a JS script for your page for the custom stuff you will need to do.  The data for the map view gizmo is stored using openlayers.js, so I don't think you can get to it without using some JS and dumping it into your HTML (https://api.jquery.com/data).

Reply all
Reply to author
Forward
0 new messages