sightglass 0.2.0__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.
- sightglass/GUIDE.md +332 -0
- sightglass/__init__.py +117 -0
- sightglass/__main__.py +7 -0
- sightglass/cli.py +336 -0
- sightglass/client.py +225 -0
- sightglass/decoders.py +314 -0
- sightglass/demo.py +99 -0
- sightglass/elements.py +628 -0
- sightglass/formatting.py +172 -0
- sightglass/launch/__init__.py +168 -0
- sightglass/launch/__main__.py +44 -0
- sightglass/launch/ground.py +96 -0
- sightglass/launch/mission.py +43 -0
- sightglass/launch/page.html +185 -0
- sightglass/launch/screen.py +649 -0
- sightglass/launch/static/fonts/B612-Bold.woff2 +0 -0
- sightglass/launch/static/fonts/B612-LICENSE.txt +93 -0
- sightglass/launch/static/fonts/B612-Regular.woff2 +0 -0
- sightglass/launch/static/page.css +1363 -0
- sightglass/launch/vehicle.py +213 -0
- sightglass/launch/weather.py +59 -0
- sightglass/monitor.py +658 -0
- sightglass/plc.py +866 -0
- sightglass/signals.py +178 -0
- sightglass/sources/__init__.py +1 -0
- sightglass/sources/_thread.py +61 -0
- sightglass/sources/ads.py +680 -0
- sightglass/sources/serialport.py +113 -0
- sightglass/sources/simulated.py +29 -0
- sightglass/sources/stdin.py +89 -0
- sightglass/sources/zeromq.py +95 -0
- sightglass/terminal.py +133 -0
- sightglass/web/__init__.py +409 -0
- sightglass/web/static/auto.html +320 -0
- sightglass/web/static/fonts/DSEG-LICENSE.txt +95 -0
- sightglass/web/static/fonts/DSEG14Modern-Bold.ttf +0 -0
- sightglass/web/static/fonts/DSEG14Modern-Regular.ttf +0 -0
- sightglass/web/static/fonts/DSEG7ModernMini-Regular.ttf +0 -0
- sightglass/web/static/fonts/Micro5-LICENSE.txt +93 -0
- sightglass/web/static/fonts/Micro5-latin.woff2 +0 -0
- sightglass/web/static/fonts/Silkscreen-LICENSE.txt +93 -0
- sightglass/web/static/fonts/Silkscreen-latin.woff2 +0 -0
- sightglass/web/static/fonts/VT323-LICENSE.txt +93 -0
- sightglass/web/static/fonts/VT323-latin.woff2 +0 -0
- sightglass/web/static/panel.css +134 -0
- sightglass/web/static/sightglass.js +255 -0
- sightglass/web/static/signals.css +459 -0
- sightglass/web/static/signals.js +361 -0
- sightglass/web/static/terminal.css +228 -0
- sightglass-0.2.0.dist-info/METADATA +510 -0
- sightglass-0.2.0.dist-info/RECORD +55 -0
- sightglass-0.2.0.dist-info/WHEEL +5 -0
- sightglass-0.2.0.dist-info/entry_points.txt +2 -0
- sightglass-0.2.0.dist-info/licenses/LICENSE +21 -0
- sightglass-0.2.0.dist-info/top_level.txt +1 -0
sightglass/GUIDE.md
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# Building monitors with sightglass: a guide for AI agents
|
|
2
|
+
|
|
3
|
+
`sightglass` shows live values from a program, a device, or anything that
|
|
4
|
+
can make an HTTP request, in a browser (default) or the terminal. Values are
|
|
5
|
+
sent by id (`"progress"`, `"X.velocity"`); the first value sent for an id
|
|
6
|
+
creates its display. Print this guide from any project with
|
|
7
|
+
`sightglass --guide`; full reference: `sightglass --help` and the README.
|
|
8
|
+
|
|
9
|
+
## 1. Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install "sightglass[web]" # add serial / zmq / ads extras as needed, or [all]
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Python 3.11+. The package has no required dependencies; `[web]` adds
|
|
16
|
+
aiohttp for the browser dashboard, `[serial]` pyserial, `[zmq]` pyzmq,
|
|
17
|
+
`[ads]` pyads (Beckhoff PLCs).
|
|
18
|
+
|
|
19
|
+
## 2. Pick the integration
|
|
20
|
+
|
|
21
|
+
| Situation | Do this |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| You can edit the Python program | `monitor = start()`, then `monitor.set(id, value)` anywhere |
|
|
24
|
+
| Another language, a shell script, or a program you can't edit | run `sightglass`, then `POST /update` (curl) |
|
|
25
|
+
| The program can print lines | print `key=value` pairs; run `program \| sightglass` |
|
|
26
|
+
| A separate Python process should report to a shared dashboard | `Client().set(id, value)` (standard library only) |
|
|
27
|
+
| A microcontroller on a serial port | `sightglass --serial auto --csv a,b,c` (or `--json`, `--binary`) |
|
|
28
|
+
| A ZeroMQ publisher | `sightglass --zmq ENDPOINT` (`--pull` for PUSH senders: nothing lost) |
|
|
29
|
+
| A Beckhoff TwinCAT PLC (ADS) | `sightglass --ads NETID --vars 'MAIN.*'`, or an interface file: `sightglass --ads plc.toml` |
|
|
30
|
+
| An asyncio program | `await monitor.run(outputs=[WebDashboard()])` in a task |
|
|
31
|
+
| Just show the user what it looks like | `sightglass --demo --open` (every display), `sightglass --demo launch --open` (a hand-built page) |
|
|
32
|
+
|
|
33
|
+
Choose `start()` when the dashboard should live and die with the program.
|
|
34
|
+
Choose a separate `sightglass` process (fed by `Client`, curl or a pipe) when
|
|
35
|
+
several programs report to one dashboard, or it should outlive them.
|
|
36
|
+
|
|
37
|
+
## 3. Recipes
|
|
38
|
+
|
|
39
|
+
### Inside a Python program
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import time
|
|
43
|
+
|
|
44
|
+
from sightglass import start
|
|
45
|
+
|
|
46
|
+
monitor = start() # prints "Dashboard: http://127.0.0.1:8080/", returns at once
|
|
47
|
+
monitor.set("status", "loading")
|
|
48
|
+
for i in range(1, 51):
|
|
49
|
+
time.sleep(0.1) # the program's real work
|
|
50
|
+
monitor.set("progress", i)
|
|
51
|
+
monitor.set("rate", i / (0.1 * i))
|
|
52
|
+
monitor.set("status", "done")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- `set()` never blocks and is safe from any thread.
|
|
56
|
+
- `start()` raises `OSError` if the port is taken; pass
|
|
57
|
+
`outputs=[WebDashboard(port=0)]` for any free port (the address is printed,
|
|
58
|
+
and is `web.url` on the `WebDashboard` object).
|
|
59
|
+
- When the program exits, open pages keep the final values, marked
|
|
60
|
+
disconnected.
|
|
61
|
+
|
|
62
|
+
### Choosing how values look
|
|
63
|
+
|
|
64
|
+
Declare elements for bars, charts, lamps, units and number formats. Anything
|
|
65
|
+
not declared still appears, as text.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from sightglass import (
|
|
69
|
+
IndicatorLamp,
|
|
70
|
+
Monitor,
|
|
71
|
+
ProgressBar,
|
|
72
|
+
RangeBar,
|
|
73
|
+
Sparkline,
|
|
74
|
+
TextFormat,
|
|
75
|
+
WebDashboard,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
monitor = Monitor()
|
|
79
|
+
monitor.add(
|
|
80
|
+
ProgressBar("progress", total=50, label="Progress"),
|
|
81
|
+
Sparkline("rate", label="Items/s", format=TextFormat(precision=1)),
|
|
82
|
+
IndicatorLamp("healthy", label="Healthy"),
|
|
83
|
+
)
|
|
84
|
+
monitor.add_group(
|
|
85
|
+
"X", [RangeBar("velocity", min_value=-10, max_value=10, units="mm/s")]
|
|
86
|
+
)
|
|
87
|
+
monitor.start(outputs=[WebDashboard(title="Batch job")])
|
|
88
|
+
monitor.set("healthy", True)
|
|
89
|
+
monitor.set("X.velocity", 2.5)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### From any language
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
sightglass --title "Build farm" &
|
|
96
|
+
curl -d 'progress=5 status=running' http://127.0.0.1:8080/update
|
|
97
|
+
curl -d '{"job": {"progress": 5, "ok": true}}' http://127.0.0.1:8080/update
|
|
98
|
+
curl http://127.0.0.1:8080/values # read back what is shown, as JSON
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The body is a JSON object (or a list of objects), or lines of `key=value`
|
|
102
|
+
pairs / `id value`. Nested JSON objects address groups: `{"job": {"progress":
|
|
103
|
+
5}}` sets `job.progress`. `204` means accepted; `400` says what was wrong;
|
|
104
|
+
`401` means the dashboard has a `--token` and the request didn't send
|
|
105
|
+
`Authorization: Bearer <token>`.
|
|
106
|
+
|
|
107
|
+
### Piping a program's output
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
my_program | sightglass
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
A line made entirely of `key=value` pairs (`progress=5 status="two words"`) or
|
|
114
|
+
a JSON object becomes values; every other line is printed through unchanged.
|
|
115
|
+
In Python: `print(f"progress={i} rate={rate:.1f}", flush=True)`.
|
|
116
|
+
|
|
117
|
+
### From a separate Python process
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from sightglass import Client
|
|
121
|
+
|
|
122
|
+
dashboard = Client("http://127.0.0.1:8080") # token="..." if the dashboard has one
|
|
123
|
+
dashboard.set("progress", 5)
|
|
124
|
+
dashboard.update({"status": "running", "errors": 0})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Updates are sent in order in the background, retried until the dashboard is
|
|
128
|
+
reachable, and flushed when the program exits. `from sightglass.client import
|
|
129
|
+
send; send({"status": "done"})` sends one message immediately and raises
|
|
130
|
+
`ConnectionError` if it can't.
|
|
131
|
+
|
|
132
|
+
### A serial device
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
sightglass --serial auto --csv temperature,humidity # device prints "21.5,48\n"
|
|
136
|
+
sightglass --serial /dev/ttyUSB0 --json # {"temperature": 21.5}
|
|
137
|
+
sightglass --serial auto --binary 1=X,2=Y,10=velocity # binary frames, see README
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`auto` picks the first USB serial device. The source waits for the device and
|
|
141
|
+
reconnects after it is unplugged. In Python:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from sightglass import CsvDecoder, Monitor, SerialSource
|
|
145
|
+
|
|
146
|
+
monitor = Monitor()
|
|
147
|
+
device = SerialSource("auto", 115200, decoder=CsvDecoder(["temperature", "humidity"]))
|
|
148
|
+
monitor.serve(sources=[device]) # runs until Ctrl+C
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### A Beckhoff TwinCAT PLC (ADS)
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
sightglass --ads 5.12.34.56.1.1 --vars 'MAIN.*,GVL.fTemperature' # :851 by default
|
|
155
|
+
sightglass --ads plc.toml # an interface file
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Ids are the PLC's variable names; structs and arrays give one id per member
|
|
159
|
+
(`MAIN.stAxis.fPosition`, `GVL.aTemps[1]`). `*` in a name matches top-level
|
|
160
|
+
variables only; name struct members exactly, or declare the struct. An
|
|
161
|
+
interface file names the PLC, the variables and their types:
|
|
162
|
+
|
|
163
|
+
```toml
|
|
164
|
+
target = "5.12.34.56.1.1:851"
|
|
165
|
+
types = """
|
|
166
|
+
TYPE ST_Axis :
|
|
167
|
+
STRUCT
|
|
168
|
+
fPosition : LREAL;
|
|
169
|
+
bEnabled : BOOL;
|
|
170
|
+
END_STRUCT
|
|
171
|
+
END_TYPE
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
[variables]
|
|
175
|
+
"MAIN.stAxis1" = "ST_Axis"
|
|
176
|
+
"GVL.fTemperature" = {}
|
|
177
|
+
"GVL.nSetpoint" = { write = true }
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`types` is Structured Text pasted from the PLC project (structs, enums,
|
|
181
|
+
aliases, `pack_mode` attributes). In Python: `AdsSource("5.12.34.56.1.1",
|
|
182
|
+
["MAIN.*"])` or `AdsSource.from_file("plc.toml")`, passed in `sources=[...]`.
|
|
183
|
+
|
|
184
|
+
- **Writes are off unless all four hold:** `write = true` on the variable,
|
|
185
|
+
`--allow-writes` (`allow_writes=True`), `SIGHTGLASS_ALLOW_WRITES=1` in the
|
|
186
|
+
environment, and `--token` on the dashboard (writes over HTTP need it). Then
|
|
187
|
+
`curl -H 'Authorization: Bearer TOKEN' -d 'GVL.nSetpoint=75' .../update`
|
|
188
|
+
writes; 204 means the PLC took it, 403/400/409 say why not. Never enable
|
|
189
|
+
writes unless the user asked for them.
|
|
190
|
+
- **Linux and macOS:** the PLC needs a route to this machine. If it doesn't
|
|
191
|
+
answer, the warning names the AMS net id and IP address to add.
|
|
192
|
+
- **Windows:** needs TwinCAT's ADS router installed (XAE, XAR or TC1000).
|
|
193
|
+
- Warnings name every variable that couldn't be read and why (unknown name,
|
|
194
|
+
undeclared struct type, size mismatch with the declaration); read them.
|
|
195
|
+
|
|
196
|
+
## 4. Ids and values
|
|
197
|
+
|
|
198
|
+
- An id is any string. Dots group: `job.rate` is shown as `rate` under a `job`
|
|
199
|
+
heading. Ids are case-sensitive.
|
|
200
|
+
- A new id creates an element: a lamp for `True`/`False`, text otherwise.
|
|
201
|
+
Declare the element first to get a bar, chart, units or format.
|
|
202
|
+
- curl, serial and pipes send text, so `pump=on` shows the text `on`. Declare
|
|
203
|
+
`IndicatorLamp("pump")` to show a lamp; it accepts `on`/`off`, `1`/`0`,
|
|
204
|
+
`true`/`false`.
|
|
205
|
+
- Multi-part elements take fields: `table.row.column`, `machine.estop`,
|
|
206
|
+
`position.x`.
|
|
207
|
+
- Floats that need more than 6 significant digits are shortened; use
|
|
208
|
+
`TextFormat(precision=2)` for a fixed number of decimals.
|
|
209
|
+
- `Monitor(strict=True)` rejects ids that weren't declared (logged once per
|
|
210
|
+
id); by default at most `max_elements=500` are created.
|
|
211
|
+
|
|
212
|
+
| To show | Element | Update with |
|
|
213
|
+
| --- | --- | --- |
|
|
214
|
+
| a value or message | `TextElement(id, units="°C", format=TextFormat(precision=1))` | anything |
|
|
215
|
+
| a trend | `Sparkline(id, points=60, scale=1 / 1024, units="KB/s")` | numbers |
|
|
216
|
+
| a long trend of a fast value | `Sparkline(id, points=300, interval=1)`: one point a second | numbers |
|
|
217
|
+
| progress | `ProgressBar(id, total=100)` | a number |
|
|
218
|
+
| a reading within limits | `RangeBar(id, min_value=0, max_value=100, units="mm")` | a number |
|
|
219
|
+
| on/off | `IndicatorLamp(id)` | bool, 1/0, on/off |
|
|
220
|
+
| many flags in one integer | `MachineState(id, states=[...])` (bit 0 first) | an integer |
|
|
221
|
+
| a position | `Coordinate(id, axes=("x", "y", "z"))` | a list, or `id.x` |
|
|
222
|
+
| a grid | `Table(id, rows=[...], columns=[...])` | `id.row.column` |
|
|
223
|
+
| recent events | `LogMonitor(id, lines=10, timestamp=True)` | a string |
|
|
224
|
+
|
|
225
|
+
## 5. A custom page
|
|
226
|
+
|
|
227
|
+
For a branded or hardware-style panel, write plain HTML and bind elements by
|
|
228
|
+
id; no JavaScript is needed. `sightglass --demo launch` runs a complete
|
|
229
|
+
example (gauges, tanks, lamps, several feeds) whose source ships with the
|
|
230
|
+
package: `python -c "import sightglass.launch as m; print(m.HERE)"` prints
|
|
231
|
+
where. The repository's `examples/robot_dashboard/` is another (segment
|
|
232
|
+
displays, LEDs), and its `examples/README.md` has a runnable example of each
|
|
233
|
+
route in this guide (one command each), plus a Docker deployment.
|
|
234
|
+
|
|
235
|
+
```html
|
|
236
|
+
<link rel="stylesheet" href="/_sightglass/panel.css">
|
|
237
|
+
<span data-bind="temperature"></span>
|
|
238
|
+
<div class="led" data-bind="pump" data-mode="state"></div>
|
|
239
|
+
<div data-bind="rate.values" data-mode="sparkline" style="height: 24px"></div>
|
|
240
|
+
<div class="needle" data-bind="pressure.ratio" data-mode="var"></div>
|
|
241
|
+
<span data-bind="wind" data-stale-after="3"></span>
|
|
242
|
+
<script src="/_sightglass/sightglass.js"></script>
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Serve it with `WebDashboard("page.html", static_dir="static")` (files in
|
|
246
|
+
`static_dir` are at `/static/`). Bars bind `id.text` and `id.ratio`
|
|
247
|
+
(`data-mode="width"`); sparklines `id.text` and `id.values`. A
|
|
248
|
+
`data-mode="state"` element is off for `false`, `0`, `"off"`, `"0"`,
|
|
249
|
+
`"false"`, `"no"` and empty, and on for anything else.
|
|
250
|
+
|
|
251
|
+
- **Gauges without JavaScript:** `data-mode="var"` sets the CSS variable
|
|
252
|
+
`--value` to the number (unset if it isn't one), e.g.
|
|
253
|
+
`.needle { rotate: calc(var(--value) * 270deg - 135deg) }` for a `RangeBar`
|
|
254
|
+
ratio, or `height: calc(var(--value) * 100%)` for a level.
|
|
255
|
+
- **Several feeds:** `data-stale-after="3"` gives an element
|
|
256
|
+
`data-stale="true"` while its value hasn't arrived for 3 s (or ever), so
|
|
257
|
+
CSS can show which source went quiet, e.g.
|
|
258
|
+
`section:has([data-stale]) { opacity: 0.3 }`. Bind it to a value the source
|
|
259
|
+
sends every time; resending the same value counts as arriving.
|
|
260
|
+
- **Themes:** pages come in `classic` and `terminal` (phosphor green,
|
|
261
|
+
cockpit-style instruments, optional CRT effects); `--theme` /
|
|
262
|
+
`WebDashboard(theme=...)` sets the default and a switch on the page lets
|
|
263
|
+
viewers change it. For your own page:
|
|
264
|
+
`<script src="/_sightglass/theme.js"></script>` and
|
|
265
|
+
`<link rel="stylesheet" href="/_sightglass/terminal.css">` in `<head>`,
|
|
266
|
+
buttons with `data-theme-switch` / `data-crt-switch`, and CSS under
|
|
267
|
+
`html[data-theme="terminal"]` using the `--crt-*` colours and fonts
|
|
268
|
+
(`--crt-display-font` for a big clock-like display, at multiples of 11px;
|
|
269
|
+
`--crt-readout-font` for readings, at multiples of 8px). The CRT effects
|
|
270
|
+
cover any such page; animate your panels with its `crt-draw` and
|
|
271
|
+
`crt-type` keyframes, only under `prefers-reduced-motion: no-preference`.
|
|
272
|
+
- **Checking the ids:** press `` ` `` on the page for the signals panel: every
|
|
273
|
+
id the dashboard receives, its value as sent and its last minute. A
|
|
274
|
+
`data-bind` that stays empty names an id that isn't in that list.
|
|
275
|
+
- **In a terminal:** `TerminalDisplay(screen=fn)` draws your own layout:
|
|
276
|
+
`fn(monitor, width, height)` returns the whole frame, reading
|
|
277
|
+
`monitor[id].text` and coloured with `Style(fg=214)` (xterm's 256
|
|
278
|
+
colours). `sightglass --demo launch --terminal` runs a full one, in the
|
|
279
|
+
package's `sightglass/launch/screen.py`.
|
|
280
|
+
|
|
281
|
+
## 6. Deploying
|
|
282
|
+
|
|
283
|
+
- **Other machines:** `WebDashboard(host="0.0.0.0")` / `--host 0.0.0.0`.
|
|
284
|
+
Anyone who can reach the port can then view the values, and post values
|
|
285
|
+
unless `token` / `--token` is set. Viewing is never authenticated or
|
|
286
|
+
encrypted: on untrusted networks use `ssh -L 8080:127.0.0.1:8080 host` or a
|
|
287
|
+
reverse proxy with TLS and auth.
|
|
288
|
+
- **Continuous sources:** `stale_after=2` / `--stale-after 2` dims values
|
|
289
|
+
when no data has arrived for 2 s.
|
|
290
|
+
- **Ports:** 8080 by default. On macOS avoid 5000 (AirPlay).
|
|
291
|
+
- **As a Linux service** (a dashboard that outlives the programs feeding it):
|
|
292
|
+
|
|
293
|
+
```ini
|
|
294
|
+
# /etc/systemd/system/sightglass.service
|
|
295
|
+
[Unit]
|
|
296
|
+
Description=Live dashboard
|
|
297
|
+
After=network-online.target
|
|
298
|
+
|
|
299
|
+
[Service]
|
|
300
|
+
ExecStart=/opt/sightglass/venv/bin/sightglass --host 0.0.0.0 --token change-me
|
|
301
|
+
Restart=on-failure
|
|
302
|
+
|
|
303
|
+
[Install]
|
|
304
|
+
WantedBy=multi-user.target
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## 7. Verify before saying it works
|
|
308
|
+
|
|
309
|
+
1. The program printed `Dashboard: http://...`; use that address.
|
|
310
|
+
2. `curl -s http://127.0.0.1:8080/values` lists the ids with their current
|
|
311
|
+
values (what the page shows).
|
|
312
|
+
3. For pushed values, `curl -s -o /dev/null -w '%{http_code}' -d 'check=1'
|
|
313
|
+
http://127.0.0.1:8080/update` prints `204`.
|
|
314
|
+
4. Warnings (bad values, unknown ids with `strict=True`, a missing serial
|
|
315
|
+
device, PLC variables left out) go to stderr; read them.
|
|
316
|
+
5. Look at the page in a browser if you can: values change live and the
|
|
317
|
+
status says "Live". Press `` ` `` for the signals panel: every id received,
|
|
318
|
+
as sent, with a chart of its last minute.
|
|
319
|
+
|
|
320
|
+
## 8. Gotchas
|
|
321
|
+
|
|
322
|
+
- A program that exits takes a `start()` dashboard with it. For a dashboard
|
|
323
|
+
that stays up, run `sightglass` separately and send with `Client` or curl.
|
|
324
|
+
- `TerminalDisplay` / `--terminal` redraws the whole terminal: don't print
|
|
325
|
+
while it runs (the command logs to `sightglass.log` instead).
|
|
326
|
+
- ZeroMQ PUB/SUB drops messages sent before the subscriber connects; use
|
|
327
|
+
PUSH with `ZmqSource(endpoint, pattern="pull")` / `--zmq ... --pull`.
|
|
328
|
+
- In asyncio code use `await monitor.run(...)`; `start()` runs its own loop in
|
|
329
|
+
a thread and `serve()` blocks.
|
|
330
|
+
- In tests: `WebDashboard(port=0, announce=False)`, `monitor.start(...)`,
|
|
331
|
+
`web.url`, then `monitor.stop()`. `SimulatedSource(fn, rate)` generates
|
|
332
|
+
data without hardware.
|
sightglass/__init__.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Live dashboards for values from your program, a device, or any process.
|
|
2
|
+
|
|
3
|
+
Quickest start, from inside a program::
|
|
4
|
+
|
|
5
|
+
from sightglass import start
|
|
6
|
+
|
|
7
|
+
monitor = start() # prints the dashboard's address
|
|
8
|
+
monitor.set("progress", 0.5) # from any thread; elements appear as used
|
|
9
|
+
|
|
10
|
+
A :class:`Monitor` holds elements (text, bars, lamps, charts, tables, ...).
|
|
11
|
+
Sources (serial, ZeroMQ, PLCs over ADS, stdin, simulated) and HTTP posts feed it updates
|
|
12
|
+
keyed by element id, and outputs (web dashboard, terminal) display it. Other
|
|
13
|
+
programs can send values with :class:`~sightglass.client.Client`, or with
|
|
14
|
+
``curl -d 'progress=5' http://127.0.0.1:8080/update``.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import importlib
|
|
18
|
+
from typing import TYPE_CHECKING, Any
|
|
19
|
+
|
|
20
|
+
from .client import Client
|
|
21
|
+
from .decoders import (
|
|
22
|
+
BinaryFrameDecoder,
|
|
23
|
+
CsvDecoder,
|
|
24
|
+
DecodeError,
|
|
25
|
+
Decoder,
|
|
26
|
+
JsonDecoder,
|
|
27
|
+
KeyValueDecoder,
|
|
28
|
+
LineDecoder,
|
|
29
|
+
)
|
|
30
|
+
from .elements import (
|
|
31
|
+
Coordinate,
|
|
32
|
+
Element,
|
|
33
|
+
IndicatorLamp,
|
|
34
|
+
LogMonitor,
|
|
35
|
+
MachineState,
|
|
36
|
+
ProgressBar,
|
|
37
|
+
RangeBar,
|
|
38
|
+
Sparkline,
|
|
39
|
+
Table,
|
|
40
|
+
TextElement,
|
|
41
|
+
)
|
|
42
|
+
from .formatting import Style, TextFormat
|
|
43
|
+
from .monitor import Group, Monitor, Output, Source, Update, WriteError, start
|
|
44
|
+
from .sources.simulated import SimulatedSource
|
|
45
|
+
from .sources.stdin import StdinSource
|
|
46
|
+
from .terminal import TerminalDisplay
|
|
47
|
+
|
|
48
|
+
if TYPE_CHECKING:
|
|
49
|
+
from .sources.ads import AdsSource
|
|
50
|
+
from .sources.serialport import SerialSource, find_serial_port
|
|
51
|
+
from .sources.zeromq import ZmqSource
|
|
52
|
+
from .web import WebDashboard
|
|
53
|
+
|
|
54
|
+
__version__ = "0.2.0"
|
|
55
|
+
|
|
56
|
+
# Classes that need an optional dependency are imported on first use.
|
|
57
|
+
_OPTIONAL = {
|
|
58
|
+
"AdsSource": (".sources.ads", "ads"),
|
|
59
|
+
"SerialSource": (".sources.serialport", "serial"),
|
|
60
|
+
"find_serial_port": (".sources.serialport", "serial"),
|
|
61
|
+
"ZmqSource": (".sources.zeromq", "zmq"),
|
|
62
|
+
"WebDashboard": (".web", "web"),
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def __getattr__(name: str) -> Any:
|
|
67
|
+
if name not in _OPTIONAL:
|
|
68
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
69
|
+
module, extra = _OPTIONAL[name]
|
|
70
|
+
try:
|
|
71
|
+
value = getattr(importlib.import_module(module, __name__), name)
|
|
72
|
+
except ModuleNotFoundError as error:
|
|
73
|
+
raise ImportError(
|
|
74
|
+
f"{name} needs an optional dependency: "
|
|
75
|
+
f"pip install 'sightglass[{extra}]' ({error})"
|
|
76
|
+
) from error
|
|
77
|
+
globals()[name] = value
|
|
78
|
+
return value
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
__all__ = [
|
|
82
|
+
"AdsSource",
|
|
83
|
+
"BinaryFrameDecoder",
|
|
84
|
+
"Client",
|
|
85
|
+
"Coordinate",
|
|
86
|
+
"CsvDecoder",
|
|
87
|
+
"DecodeError",
|
|
88
|
+
"Decoder",
|
|
89
|
+
"Element",
|
|
90
|
+
"Group",
|
|
91
|
+
"IndicatorLamp",
|
|
92
|
+
"JsonDecoder",
|
|
93
|
+
"KeyValueDecoder",
|
|
94
|
+
"LineDecoder",
|
|
95
|
+
"LogMonitor",
|
|
96
|
+
"MachineState",
|
|
97
|
+
"Monitor",
|
|
98
|
+
"Output",
|
|
99
|
+
"ProgressBar",
|
|
100
|
+
"RangeBar",
|
|
101
|
+
"SerialSource",
|
|
102
|
+
"SimulatedSource",
|
|
103
|
+
"Source",
|
|
104
|
+
"Sparkline",
|
|
105
|
+
"StdinSource",
|
|
106
|
+
"Style",
|
|
107
|
+
"Table",
|
|
108
|
+
"TerminalDisplay",
|
|
109
|
+
"TextElement",
|
|
110
|
+
"TextFormat",
|
|
111
|
+
"Update",
|
|
112
|
+
"WebDashboard",
|
|
113
|
+
"WriteError",
|
|
114
|
+
"ZmqSource",
|
|
115
|
+
"find_serial_port",
|
|
116
|
+
"start",
|
|
117
|
+
]
|