Introduction to PYUI Python Interface
PYUI comes with PYUI standard libary. It provides the functionalities for:
- DOM Manipulation
- Using Element Instance
- Using proceudral Legacy
PYUIcalls
- Event callbacks setting , resetting and stopping
- Thread Management Model (
group,@background,commit) - Hooks and Pipelines
- Registering and Unregistering
Custom Syscallsto execute other JS functions - Sending
Custom Syscallsand handling there callbacks from JS and Python side. - Adding and Removing Components dynamically from Python Runtime
- Other functions
Architecture Summary
Before jumping into the libary it is important to understand a bit of how PYUI works under the hood.
The structure of Message
As PYUI works using sending messages from PYTHON to JS and vice versa all messages share a common structure. Example
{'uuid':'','type':'','other_data':'' .... , 'sign':''}
uuid: It is in a unique identifier to identify a singular message. It is also used to make callback tables and dispatch callbacks to correct queue.
type: This stores the SYSCALL code (see below). To identify what to do and what instruction to execute in both JS and Python side.
other_data: This contains other data specific to the SYSCALL type.
sign: It contains a HMAC sign for the whole message JSON leaving the sign key itself. Helps in verifiying the authenticity of the message.
One important termology: SYSCALLS
All message that are going from PYTHON to JS or JS to PYTHON contains a Message code that is SYSCALL this helps both runtime understand what to do.
For example EXECUTE_JS syscall is used to execute some JS script. This is sent from Python to JS runtime with required data about what to execute. JS unpacks the JSON reads the syscall and sends it to the handleExecuteJS function that reads the TYPE from the same JSON and it can be REGISTER_CALLBACK SET_... etc.. and performs the event.
How Python to JS Communication happens
- Once you set a property in Python side. Your request is packaged into a JSON Procedure call like shown above.
- The
PYUIlibary using a threadsafe queueSEND_QUEUEdispatches this call to thewebsocketthread. - Websocket thread
getfrom the queue continuously and sends it toJSclient. JSside recives the message. Parses theJSONand sends it to ahandleMessagefunction.handleMessageunpacks theJSONand finds out theSYSCALLand executes required information.
How JS to Python Communication Happens
- Similarly
JSONis sent fromJStoPythonside. Python'swebsocket thread captures it and reads theuuidandJS SYSCALL TYPE.- It selects the correct queue to dispatch the message to using the uuid (The uuid used while sending data from Python to JS side for requesting a callback or data is sent back), and dispatches it.
- The thread (in case of callback) or the user thread (in case for getting DOM data) waits for the message using
queue.get()once it recives the message.- In case of
Callbacksthe acutal callback function is executed with thee.detail(sent from JS side) - In case of
DOM getthe value is returned to user/developer. Developer then can use it in the program.
- In case of
Final words
This were the important things required for clear understanding of PYUI Liabary from next page we will cover the important libary functionalites.