1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171# JAnim Stdio Interact
[](http://choosealicense.com/licenses/mit/)
[](https://pypi.org/project/janim-stdio-interact/)
<div align="center">
**> English <** | [įŽäŊ䏿](README_zh_CN.md)
</div>
A utility library for interacting with JAnim GUI and managing windows via standard input/output
## Basic Usage
Interact with the JAnim GUI by passing JSON commands via stdio to open and close specific instances.
First, start the `janim-stdio-i` program:
```sh
janim-stdio-i host
```
For example, to compile a code snippet and build the Timeline within it for display in a preview window, send the following to `stdin`:
```json
{"type": "execute", "key": "id1", "source": "from janim.examples import *\nclass A(HelloJAnimExample): pass"}
```
You will see a preview window open displaying the classic `HelloJAnimExample`.
> [!WARNING]
> All commands sent to `stdin` must be on a single line. Multi-line commands will be truncated.
Here, `"key": "id1"` is used for window reuse. If you send an `execute` command with the same `key`, the Timeline in the existing window will be replaced without opening a new window.
```json
{"type": "execute", "key": "id1", "source": "from janim.examples import *\nclass A(RotatingPieExample): ..."}
```
Sending the command above replaces the `HelloJAnimExample` in the original window with `RotatingPieExample`.
> [!NOTE]
> If a code snippet contains multiple Timelines, the first one is used for the initial build.
>
> On subsequent rebuilds (replacements), it attempts to find a Timeline with the same name as the previous one; if not found, it defaults to the first one.
If we use a different `key`, for example:
```json
{"type": "execute", "key": "id2", "source": "from janim.examples import *\nclass A(ArrowPointingExample): ..."}
```
A new window will open. Essentially, one `key` corresponds to one window.
In addition to compiling and building Timelines via `stdin`, information generated by the GUI is returned via `stdout`. See below for details.
## API Reference
### `stdin`
- `"type": "execute"`
Required parameters:
- `"key"`: Unique identifier for the window.
- `"source"`: Source code for compilation.
Compiles and builds the Timeline to display in the preview window.
If the window corresponding to `key` already exists, the original Timeline in that window is replaced with the new one.
Example:
```json
{"type": "execute", "key": "id1", "source": "from janim.examples import *\nclass A(HelloJAnimExample): pass"}
```
- `"type": "close"`
Required parameters:
- `"key"`: Unique identifier for the window.
Closes the window corresponding to `key`.
Example:
```json
{"type": "close", "key": "id1"}
```
### `stdout`
```json
{"type": "viewer-msg", "key": "...", "from": "...", "janim": { ... }}
```
| Field | Type | Description |
| :--- | :--- | :--- |
| `type` | `string` | Fixed as `"viewer-msg"`, identifying the message category |
| `key` | `string` | Corresponds to the identifier `key` used during creation |
| `from` | `string` | Sender source. Possible values: `"execute"`, `"close"`, `"gui"` |
| `janim` | `object` | The specific event payload, containing the event type and data |
When `"from": "execute"`, it represents the result of the `execute` command sent to `stdin`:
- When source code compilation fails, the payload is
```json
"janim": {"type": "error", "reason": "compile-failed"}
```
- When the source code contains no usable Timeline, the payload is:
```json
"janim": {"type": "error", "reason": "no-timeline"}
```
- When the Timeline build fails, the payload is:
```json
"janim": {"type": "error", "reason": "build-failed"}
```
- When execution is successful, the payload is:
```json
"janim": {"type": "success"}
```
When `"from": "close"`, it represents the result of the `close` command sent to `stdin`:
- When no preview interface is found for the identifier, the payload is:
```json
"janim": {"type": "error", "reason": "not-found"}
```
- When executed successfully, the payload is:
```json
"janim": {"type": "success"}
```
When `"from": "gui"`, it represents information generated by the GUI:
- When the interface is created, the payload is:
```json
"janim": {"type": "created"}
```
- When the interface content is replaced, the payload is:
```json
"janim": {"type": "rebuilt"}
```
- When the preview progress changes, the payload is:
```json
"janim": {"type": "lineno", "data": <current_line_number>}
```
- When the interface is closed, the payload is:
```json
"janim": {"type": "close_event"}
```