[help] how to call python func from JS

Dear friends.

I'm trying to make a new plugin, but I need to call a python function from my javascript code. Whats is the correct way to do this thing?
My phyton function will trigger one relay, with the GPIO ports.

I try use the octoprint.comm.protocol.gcode.queuing, with one custom gcode, but this does not work instantly. So, this way dont work to me.

Thank you.


You'll want to look into the SimpleApiPlugin for this. I use it in several of my plugins to transfer data/instruct python to do stuff from the javascript viewmodel side. So from your javascript side, you'd have a jquery ajax request post to your python back-end.

An example in my TPLinkSmartPlug plugin. You'll see here in my javascript code where it sends the api call back to the python. Then in my python code I receive the command here.

Hope that helps.



Thank you!!!