pyloid 0.26.2__py3-none-any.whl → 0.26.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyloid/ipc.py ADDED
@@ -0,0 +1,142 @@
1
+ from PySide6.QtCore import (
2
+ QObject,
3
+ Slot,
4
+ )
5
+ from typing import (
6
+ TYPE_CHECKING,
7
+ )
8
+
9
+ if TYPE_CHECKING:
10
+ from pyloid.pyloid import (
11
+ Pyloid,
12
+ )
13
+ from pyloid.browser_window import (
14
+ BrowserWindow,
15
+ )
16
+
17
+
18
+ class PyloidIPC(QObject):
19
+ """
20
+ PyloidIPC class.
21
+ It enables communication between JavaScript and Python via IPC.
22
+
23
+ Usage Example
24
+ -------------
25
+ (Python)
26
+ ```python
27
+ from pyloid import (
28
+ Pyloid,
29
+ )
30
+ from pyloid.ipc import (
31
+ PyloidIPC,
32
+ Bridge,
33
+ )
34
+
35
+ app = Pyloid('Pyloid-App')
36
+
37
+
38
+ class CustomIPC(PyloidIPC):
39
+ @Bridge(
40
+ str,
41
+ result=str,
42
+ )
43
+ def echo(
44
+ self,
45
+ message,
46
+ ):
47
+ return f'Message received in Python: {message}'
48
+
49
+
50
+ # Create main window
51
+ window = app.create_window(
52
+ title='Pyloid Browser',
53
+ ipc=[CustomIPC()],
54
+ )
55
+
56
+ window.load_file('index.html')
57
+
58
+ window.show()
59
+ window.focus()
60
+
61
+ app.run()
62
+ ```
63
+ ---
64
+ (JavaScript)
65
+ ```javascript
66
+ let result = await window.pyloid.CustomIPC.echo('Hello, Pyloid!');
67
+ console.log(result);
68
+ ```
69
+ """
70
+
71
+ def __init__(
72
+ self,
73
+ ):
74
+ super().__init__()
75
+ self.window_id: str = None
76
+ self.window: 'BrowserWindow' = None
77
+ self.app: 'Pyloid' = None
78
+
79
+
80
+ def Bridge(
81
+ *args,
82
+ **kwargs,
83
+ ):
84
+ """
85
+ Bridge function creates a slot that can be IPC from JavaScript.
86
+
87
+ Parameters
88
+ ----------
89
+ *args : tuple
90
+ Variable length argument list.
91
+ **kwargs : dict
92
+ Arbitrary keyword arguments.
93
+
94
+ Usage Example
95
+ -------------
96
+ (Python)
97
+ ```python
98
+ from pyloid import (
99
+ Pyloid,
100
+ PyloidIPC,
101
+ Bridge,
102
+ )
103
+
104
+ app = Pyloid('Pyloid-App')
105
+
106
+
107
+ class CustomIPC(PyloidIPC):
108
+ @Bridge(
109
+ str,
110
+ result=str,
111
+ )
112
+ def echo(
113
+ self,
114
+ message,
115
+ ):
116
+ return f'Message received in Python: {message}'
117
+
118
+
119
+ # Create main window
120
+ window = app.create_window(
121
+ title='Pyloid Browser',
122
+ ipc=[CustomIPC()],
123
+ )
124
+
125
+ window.load_file('index.html')
126
+
127
+ window.show()
128
+ window.focus()
129
+
130
+ app.run()
131
+ ```
132
+ ---
133
+ (JavaScript)
134
+ ```javascript
135
+ let result = await window.pyloid.CustomIPC.echo('Hello, Pyloid!');
136
+ console.log(result);
137
+ ```
138
+ """
139
+ return Slot(
140
+ *args,
141
+ **kwargs,
142
+ )