mpl-help 0.1.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.
- mpl_help/__init__.py +89 -0
- mpl_help/_config.py +255 -0
- mpl_help/_figure.py +863 -0
- mpl_help/py.typed +0 -0
- mpl_help-0.1.0.dist-info/METADATA +397 -0
- mpl_help-0.1.0.dist-info/RECORD +9 -0
- mpl_help-0.1.0.dist-info/WHEEL +5 -0
- mpl_help-0.1.0.dist-info/licenses/LICENSE.txt +28 -0
- mpl_help-0.1.0.dist-info/top_level.txt +1 -0
mpl_help/__init__.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (C) 2026 by Brendt Wohlberg <brendt@ieee.org>
|
|
3
|
+
# All rights reserved. BSD 3-clause License.
|
|
4
|
+
# This file is part of the mpl-help package. Details of the copyright
|
|
5
|
+
# and user license can be found in the 'LICENSE.txt' file distributed
|
|
6
|
+
# with the package.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
mpl-help
|
|
10
|
+
========
|
|
11
|
+
Backend-independent help windows for matplotlib figures.
|
|
12
|
+
|
|
13
|
+
Quickstart — programmatic API
|
|
14
|
+
------------------------------
|
|
15
|
+
::
|
|
16
|
+
|
|
17
|
+
import matplotlib.pyplot as plt
|
|
18
|
+
from mpl_help import HelpManager, HelpSection, KeyBinding
|
|
19
|
+
|
|
20
|
+
fig, ax = plt.subplots()
|
|
21
|
+
help_mgr = HelpManager(fig, title="My App", theme="dark")
|
|
22
|
+
help_mgr.add_section(HelpSection(
|
|
23
|
+
title="Navigation",
|
|
24
|
+
accent="blue",
|
|
25
|
+
bindings=[
|
|
26
|
+
KeyBinding("?", "Show this help window"),
|
|
27
|
+
KeyBinding("q / Escape", "Close figure"),
|
|
28
|
+
],
|
|
29
|
+
))
|
|
30
|
+
help_mgr.attach()
|
|
31
|
+
plt.show()
|
|
32
|
+
|
|
33
|
+
Quickstart — config file API
|
|
34
|
+
-----------------------------
|
|
35
|
+
::
|
|
36
|
+
|
|
37
|
+
from mpl_help import load_manager
|
|
38
|
+
|
|
39
|
+
help_mgr = load_manager(fig, "my_app_help.yaml", theme="light")
|
|
40
|
+
help_mgr.attach()
|
|
41
|
+
plt.show()
|
|
42
|
+
|
|
43
|
+
Theme and palette
|
|
44
|
+
-----------------
|
|
45
|
+
Two built-in themes are available: ``"dark"`` (default) and ``"light"``.
|
|
46
|
+
Individual palette colours can be overridden per-instance::
|
|
47
|
+
|
|
48
|
+
from mpl_help import HelpManager, DEFAULT_PALETTE
|
|
49
|
+
|
|
50
|
+
help_mgr = HelpManager(
|
|
51
|
+
fig,
|
|
52
|
+
theme="light",
|
|
53
|
+
palette={
|
|
54
|
+
"blue": ("#1a5fb4", "#6ab0f5"), # (light_hex, dark_hex)
|
|
55
|
+
"bg": ("#fafafa", "#0a0d12"),
|
|
56
|
+
},
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
See :data:`DEFAULT_PALETTE` for all available keys.
|
|
60
|
+
Valid accent colour names are listed in :data:`ACCENT_NAMES`.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
from mpl_help._config import load_manager, loads_manager, manager_from_dict
|
|
64
|
+
from mpl_help._figure import (
|
|
65
|
+
ACCENT_NAMES,
|
|
66
|
+
DEFAULT_PALETTE,
|
|
67
|
+
HelpManager,
|
|
68
|
+
HelpSection,
|
|
69
|
+
KeyBinding,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
__version__ = "0.1.0"
|
|
73
|
+
__author__ = "Brendt Wohlberg"
|
|
74
|
+
__email__ = "brendt@ieee.org"
|
|
75
|
+
|
|
76
|
+
__all__ = [
|
|
77
|
+
# Core data classes
|
|
78
|
+
"HelpSection",
|
|
79
|
+
"KeyBinding",
|
|
80
|
+
# Manager
|
|
81
|
+
"HelpManager",
|
|
82
|
+
# Config loaders
|
|
83
|
+
"load_manager",
|
|
84
|
+
"loads_manager",
|
|
85
|
+
"manager_from_dict",
|
|
86
|
+
# Palette introspection
|
|
87
|
+
"DEFAULT_PALETTE",
|
|
88
|
+
"ACCENT_NAMES",
|
|
89
|
+
]
|
mpl_help/_config.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Copyright (C) 2026 by Brendt Wohlberg <brendt@ieee.org>
|
|
3
|
+
# All rights reserved. BSD 3-clause License.
|
|
4
|
+
# This file is part of the mpl-help package. Details of the copyright
|
|
5
|
+
# and user license can be found in the 'LICENSE.txt' file distributed
|
|
6
|
+
# with the package.
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
mpl_help._config
|
|
10
|
+
================
|
|
11
|
+
Format-agnostic configuration loader for mpl-help.
|
|
12
|
+
|
|
13
|
+
Supported formats
|
|
14
|
+
-----------------
|
|
15
|
+
YAML (.yaml / .yml) requires PyYAML (``pip install pyyaml``)
|
|
16
|
+
TOML (.toml) stdlib ``tomllib`` — always available
|
|
17
|
+
JSON (.json) stdlib ``json`` — always available
|
|
18
|
+
|
|
19
|
+
The theme and palette are presentation concerns and are therefore
|
|
20
|
+
*not* part of the config file format. Pass them as arguments to
|
|
21
|
+
the loader functions or directly to :class:`~mpl_help.HelpManager`.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import json
|
|
27
|
+
import tomllib
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
from typing import Any
|
|
30
|
+
|
|
31
|
+
from mpl_help._figure import (
|
|
32
|
+
_DEFAULT_KEY_COL,
|
|
33
|
+
_DEFAULT_TRIGGER_KEY,
|
|
34
|
+
HelpManager,
|
|
35
|
+
HelpSection,
|
|
36
|
+
KeyBinding,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# ---------------------------------------------------------------------------
|
|
40
|
+
# Format parsers
|
|
41
|
+
# ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _parse_yaml(text: str) -> dict:
|
|
45
|
+
try:
|
|
46
|
+
import yaml
|
|
47
|
+
except ImportError as exc:
|
|
48
|
+
raise ImportError(
|
|
49
|
+
"PyYAML is required for YAML config files.\n"
|
|
50
|
+
"Install it with: pip install 'mpl-help[yaml]'"
|
|
51
|
+
) from exc
|
|
52
|
+
return yaml.safe_load(text)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _parse_toml(text: str) -> dict:
|
|
56
|
+
return tomllib.loads(text)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _parse_json(text: str) -> dict:
|
|
60
|
+
return json.loads(text)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
_PARSERS = {
|
|
64
|
+
"yaml": _parse_yaml,
|
|
65
|
+
"yml": _parse_yaml,
|
|
66
|
+
"toml": _parse_toml,
|
|
67
|
+
"json": _parse_json,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
# ---------------------------------------------------------------------------
|
|
72
|
+
# dict → HelpManager
|
|
73
|
+
# ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def manager_from_dict(
|
|
77
|
+
fig,
|
|
78
|
+
config: dict[str, Any],
|
|
79
|
+
theme: str = "dark",
|
|
80
|
+
palette: dict[str, tuple[str, str]] | None = None,
|
|
81
|
+
key_column: float | None = None,
|
|
82
|
+
close_with_figure: bool | None = None,
|
|
83
|
+
):
|
|
84
|
+
"""
|
|
85
|
+
Build a :class:`~mpl_help.HelpManager` from a plain Python dict.
|
|
86
|
+
|
|
87
|
+
Parameters
|
|
88
|
+
----------
|
|
89
|
+
fig :
|
|
90
|
+
The matplotlib Figure to attach to.
|
|
91
|
+
config :
|
|
92
|
+
Parsed configuration dictionary.
|
|
93
|
+
theme :
|
|
94
|
+
``"dark"`` (default) or ``"light"``.
|
|
95
|
+
palette :
|
|
96
|
+
Optional palette overrides — ``{name: (light_hex, dark_hex)}``.
|
|
97
|
+
See :data:`~mpl_help.DEFAULT_PALETTE` for valid key names.
|
|
98
|
+
key_column :
|
|
99
|
+
Right edge of the key badge column as a fraction of figure width
|
|
100
|
+
(e.g. ``0.24`` for short keyboard labels, ``0.45`` for longer
|
|
101
|
+
mouse-event descriptions). Overrides any value in *config*.
|
|
102
|
+
If ``None``, the value from *config* is used, falling back to
|
|
103
|
+
the package default (``0.24``).
|
|
104
|
+
close_with_figure :
|
|
105
|
+
Whether closing the monitored figure also closes the help
|
|
106
|
+
figure. Overrides any value in *config*. If ``None``, the
|
|
107
|
+
value from *config* is used, falling back to ``True``.
|
|
108
|
+
"""
|
|
109
|
+
title = config.get("title", "Keyboard Shortcuts")
|
|
110
|
+
trigger_key = config.get("trigger_key", _DEFAULT_TRIGGER_KEY)
|
|
111
|
+
footer = config.get("footer") or None
|
|
112
|
+
|
|
113
|
+
# key_column: programmatic argument takes priority, then config file, then default
|
|
114
|
+
if key_column is None:
|
|
115
|
+
key_column = float(config.get("key_column", _DEFAULT_KEY_COL))
|
|
116
|
+
if close_with_figure is None:
|
|
117
|
+
close_with_figure = bool(config.get("close_with_figure", True))
|
|
118
|
+
|
|
119
|
+
mgr = HelpManager(
|
|
120
|
+
fig,
|
|
121
|
+
title=title,
|
|
122
|
+
trigger_key=trigger_key,
|
|
123
|
+
footer=footer,
|
|
124
|
+
theme=theme,
|
|
125
|
+
palette=palette,
|
|
126
|
+
key_column=key_column,
|
|
127
|
+
close_with_figure=close_with_figure,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
for sec_raw in config.get("sections", []):
|
|
131
|
+
bindings = [
|
|
132
|
+
KeyBinding(
|
|
133
|
+
key=str(b.get("key", "")),
|
|
134
|
+
description=str(b.get("desc", b.get("description", ""))),
|
|
135
|
+
)
|
|
136
|
+
for b in sec_raw.get("bindings", [])
|
|
137
|
+
]
|
|
138
|
+
# Store accent as-is (name or hex); resolution happens at render time.
|
|
139
|
+
accent = sec_raw.get("accent") or None
|
|
140
|
+
|
|
141
|
+
mgr.add_section(
|
|
142
|
+
HelpSection(
|
|
143
|
+
title=sec_raw.get("title", ""),
|
|
144
|
+
bindings=bindings,
|
|
145
|
+
note=sec_raw.get("note") or None,
|
|
146
|
+
accent=accent,
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
return mgr
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# ---------------------------------------------------------------------------
|
|
154
|
+
# Public loaders
|
|
155
|
+
# ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def loads_manager(
|
|
159
|
+
fig,
|
|
160
|
+
text: str,
|
|
161
|
+
fmt: str,
|
|
162
|
+
theme: str = "dark",
|
|
163
|
+
palette: dict[str, tuple[str, str]] | None = None,
|
|
164
|
+
key_column: float | None = None,
|
|
165
|
+
close_with_figure: bool | None = None,
|
|
166
|
+
):
|
|
167
|
+
"""
|
|
168
|
+
Build a :class:`~mpl_help.HelpManager` by parsing *text*.
|
|
169
|
+
|
|
170
|
+
Parameters
|
|
171
|
+
----------
|
|
172
|
+
fig :
|
|
173
|
+
The matplotlib Figure to attach to.
|
|
174
|
+
text :
|
|
175
|
+
Raw config text (YAML, TOML, or JSON string).
|
|
176
|
+
fmt :
|
|
177
|
+
``"yaml"`` / ``"yml"``, ``"toml"``, or ``"json"``.
|
|
178
|
+
theme :
|
|
179
|
+
``"dark"`` (default) or ``"light"``.
|
|
180
|
+
palette :
|
|
181
|
+
Optional palette overrides.
|
|
182
|
+
key_column :
|
|
183
|
+
Right edge of key badge column as a fraction of figure width.
|
|
184
|
+
Overrides any value in the config text.
|
|
185
|
+
close_with_figure :
|
|
186
|
+
Whether closing the monitored figure also closes the help
|
|
187
|
+
figure. Overrides any value in the config text.
|
|
188
|
+
"""
|
|
189
|
+
fmt = fmt.lower().lstrip(".")
|
|
190
|
+
parser = _PARSERS.get(fmt)
|
|
191
|
+
if parser is None:
|
|
192
|
+
raise ValueError(
|
|
193
|
+
f"Unsupported format {fmt!r}. " f"Choose from: {', '.join(_PARSERS)}"
|
|
194
|
+
)
|
|
195
|
+
return manager_from_dict(
|
|
196
|
+
fig,
|
|
197
|
+
parser(text),
|
|
198
|
+
theme=theme,
|
|
199
|
+
palette=palette,
|
|
200
|
+
key_column=key_column,
|
|
201
|
+
close_with_figure=close_with_figure,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def load_manager(
|
|
206
|
+
fig,
|
|
207
|
+
path: str | Path,
|
|
208
|
+
theme: str = "dark",
|
|
209
|
+
palette: dict[str, tuple[str, str]] | None = None,
|
|
210
|
+
key_column: float | None = None,
|
|
211
|
+
close_with_figure: bool | None = None,
|
|
212
|
+
encoding: str = "utf-8",
|
|
213
|
+
):
|
|
214
|
+
"""
|
|
215
|
+
Build a :class:`~mpl_help.HelpManager` from a config file.
|
|
216
|
+
|
|
217
|
+
The format is auto-detected from the file extension
|
|
218
|
+
(``.yaml``/``.yml``, ``.toml``, ``.json``).
|
|
219
|
+
|
|
220
|
+
Parameters
|
|
221
|
+
----------
|
|
222
|
+
fig :
|
|
223
|
+
The matplotlib Figure to attach to.
|
|
224
|
+
path :
|
|
225
|
+
Path to the config file.
|
|
226
|
+
theme :
|
|
227
|
+
``"dark"`` (default) or ``"light"``.
|
|
228
|
+
palette :
|
|
229
|
+
Optional palette overrides.
|
|
230
|
+
key_column :
|
|
231
|
+
Right edge of key badge column as a fraction of figure width.
|
|
232
|
+
Overrides any value in the config file.
|
|
233
|
+
close_with_figure :
|
|
234
|
+
Whether closing the monitored figure also closes the help
|
|
235
|
+
figure. Overrides any value in the config file.
|
|
236
|
+
encoding :
|
|
237
|
+
File encoding (default ``"utf-8"``).
|
|
238
|
+
"""
|
|
239
|
+
path = Path(path)
|
|
240
|
+
suffix = path.suffix.lstrip(".").lower()
|
|
241
|
+
if suffix not in _PARSERS:
|
|
242
|
+
raise ValueError(
|
|
243
|
+
f"Cannot infer format from extension {path.suffix!r}. "
|
|
244
|
+
f"Supported: {', '.join(f'.{k}' for k in _PARSERS)}"
|
|
245
|
+
)
|
|
246
|
+
text = path.read_text(encoding=encoding)
|
|
247
|
+
return loads_manager(
|
|
248
|
+
fig,
|
|
249
|
+
text,
|
|
250
|
+
fmt=suffix,
|
|
251
|
+
theme=theme,
|
|
252
|
+
palette=palette,
|
|
253
|
+
key_column=key_column,
|
|
254
|
+
close_with_figure=close_with_figure,
|
|
255
|
+
)
|