Skip to content

Communicate between scripts (Python, HTML JavaScript)

Yazaar edited this page May 29, 2019 · 3 revisions

Python (to HTML/JavaScript)

To communicate from Python to HTML/JavaScript use the HTTP request endpoint /CrossTalk. This is a post request and an example can be found below. The request contains the URL which never changes (except the port which is sent onload in Initialize()). It does as well include json which has a total of 2 keys. One is event has to start with p- but anything after it is up to you. This is the event that you have to listen on in your HTML/JavaScript. The other key is data which has whatever value that you wish, this is the data that gets forwarded to the eventpoint. All events that starts with p- are private eventsm which means that it is not a part of the standard events.

requests.post('http://localhost:' + str(port) + '/CrossTalk', json={'event':'p-MyEventName', 'data':DATA_TO_SEND})

HTML/JavaScript (to Python)

The process is similar for HTML/JavaScript but the socket is used instead. The event endpoint is called CrossTalk.
Lets assume that the socket is named sio (for socket-io). Look at the HTML/JavaScript structure page to get an idea about how the socket may be constructed. The data that you send have to be a dictionary with the two keys module and data. Module contains the module name that you wish to communicate to and data contains whatever data that you would like to send, in any form.

sio.emit('CrossTalk', {'module':'example.test_LSE', 'data':SEND_YOUR_DATA})

Clone this wiki locally