On 21/01/2021 12:39, Terry Coles wrote: > Hi, > > I'm building a Web page to allow users to monitor key status information on > one of the Pis at WMT. I also want to create a page where users can press a > button to carry out certain tasks, such as Start or Stop some music playing > or > select a new Playlist. The Player is written in Python. Altogether, there are > around 10 or more control functions that I want to invoke. > > I have the monitoring page working reasonably well using a suitable html > template and a Flask route which populates the status values on the rendered > page. However, the control side is making me think a bit. I've found quite a > lot of tutorials and text book examples that use forms and a 'Submit' button, > but so far nothing quite like what I want. > > What I need is for the user to be presented with set of buttons that, when > pressed, cause a bit of Python code to be executed that communicates with the > Player program. On receipt of the message, the Player program will trigger a > set of functions to carry out the requested action. The communication side > is > sorted; all I need is an example or a link to a suitable tutorial that > illustrates the Flask code that I need to make it happen and if necessary, > the > associated template. > > Here is a bit of pseudo code to illustrate what I'm trying to do: > > @app.route('/control') > def control(): > Add a Button called "Stop the Music" > ON click run: > socket.write("Stop the Music") > > Add another button... > > etc. > > Can anyone help?
I haven't looked at Flask in detail, but one way to do it would be to use a GET or POST value when the button is submitted in the form, so it eg redirects to /control.html?action=play. Then, the server side-script can examine the value of the action attribute when the page reloads, and perform the action. It could also render a little confirmation message to say whether or not it was successful. If you want to avoid page reloads, you'll likely need some JavaScript to catch the form submission and send the request to the server through something like the XHR facility. Might need a little more info on exactly how you want it to behave though. Unfortunately I don't have the knowledge to help much beyond conceptualising this at the moment, but I'm sure someone here does web development as a profession/has some time to find something for you. Hamish
signature.asc
Description: OpenPGP digital signature
-- Next meeting: Online, Jitsi, Tuesday, 2021-02-02 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk New thread, don't hijack: mailto:dorset@mailman.lug.org.uk