statstudio 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.
- statstudio/__init__.py +9 -0
- statstudio/__main__.py +3 -0
- statstudio/app.py +83 -0
- statstudio/cli.py +116 -0
- statstudio/core/__init__.py +3 -0
- statstudio/core/archive.py +27 -0
- statstudio/core/commands.py +67 -0
- statstudio/core/formula_catalog.py +74 -0
- statstudio/core/formulas.py +605 -0
- statstudio/core/joins.py +123 -0
- statstudio/core/project.py +358 -0
- statstudio/core/runtime.py +321 -0
- statstudio/core/scripting.py +594 -0
- statstudio/core/table.py +796 -0
- statstudio/core/transform.py +450 -0
- statstudio/feature_help.py +595 -0
- statstudio/io/__init__.py +3 -0
- statstudio/io/docx.py +201 -0
- statstudio/io/export.py +99 -0
- statstudio/io/file_import.py +199 -0
- statstudio/io/odbc.py +334 -0
- statstudio/io/sample_data.py +164 -0
- statstudio/io/table_io.py +118 -0
- statstudio/plots/__init__.py +19 -0
- statstudio/plots/advanced.py +651 -0
- statstudio/plots/basic.py +957 -0
- statstudio/plots/bayesian_plots.py +94 -0
- statstudio/plots/common.py +334 -0
- statstudio/plots/diagnostics.py +214 -0
- statstudio/plots/doe_plots.py +235 -0
- statstudio/plots/plotly_builders.py +719 -0
- statstudio/plots/predictive_plots.py +72 -0
- statstudio/plots/registry.py +251 -0
- statstudio/plugins.py +111 -0
- statstudio/resources/help_topics.json +13195 -0
- statstudio/resources/logo.ico +0 -0
- statstudio/resources/logo.png +0 -0
- statstudio/resources/statstudio.svg +8 -0
- statstudio/stats/__init__.py +42 -0
- statstudio/stats/bayesian.py +1094 -0
- statstudio/stats/bayesian_optimization.py +590 -0
- statstudio/stats/common.py +140 -0
- statstudio/stats/correlation.py +135 -0
- statstudio/stats/custom_doe.py +1714 -0
- statstudio/stats/descriptive.py +200 -0
- statstudio/stats/desirability.py +132 -0
- statstudio/stats/distributions.py +331 -0
- statstudio/stats/doe.py +1070 -0
- statstudio/stats/hypothesis.py +1305 -0
- statstudio/stats/multivariate.py +307 -0
- statstudio/stats/optimal_doe.py +264 -0
- statstudio/stats/power.py +194 -0
- statstudio/stats/predictive.py +605 -0
- statstudio/stats/profiler.py +379 -0
- statstudio/stats/quality.py +1218 -0
- statstudio/stats/regression.py +1600 -0
- statstudio/stats/results.py +235 -0
- statstudio/stats/simulator.py +95 -0
- statstudio/stats/survival.py +637 -0
- statstudio/stats/timeseries.py +643 -0
- statstudio/stats/tolerance.py +154 -0
- statstudio/ui/__init__.py +0 -0
- statstudio/ui/analysis_hub.py +271 -0
- statstudio/ui/command_catalog.py +460 -0
- statstudio/ui/command_palette.py +55 -0
- statstudio/ui/console.py +239 -0
- statstudio/ui/dialogs/__init__.py +0 -0
- statstudio/ui/dialogs/advanced_dialogs.py +392 -0
- statstudio/ui/dialogs/base.py +537 -0
- statstudio/ui/dialogs/bayesian_dialogs.py +488 -0
- statstudio/ui/dialogs/bayesian_optimization_dialog.py +299 -0
- statstudio/ui/dialogs/custom_doe_dialog.py +467 -0
- statstudio/ui/dialogs/doe_dialogs.py +220 -0
- statstudio/ui/dialogs/extended_dialogs.py +665 -0
- statstudio/ui/dialogs/graph_builder.py +139 -0
- statstudio/ui/dialogs/help_dialog.py +207 -0
- statstudio/ui/dialogs/help_topics.py +280 -0
- statstudio/ui/dialogs/lazy.py +184 -0
- statstudio/ui/dialogs/odbc_dialog.py +311 -0
- statstudio/ui/dialogs/plot_dialog.py +1385 -0
- statstudio/ui/dialogs/report_composer.py +218 -0
- statstudio/ui/dialogs/script_dialogs.py +267 -0
- statstudio/ui/dialogs/stats_dialogs.py +1688 -0
- statstudio/ui/dialogs/table_dialogs.py +997 -0
- statstudio/ui/dialogs/window.py +58 -0
- statstudio/ui/docks/__init__.py +0 -0
- statstudio/ui/formula_editor.py +121 -0
- statstudio/ui/icons.py +257 -0
- statstudio/ui/jobs.py +179 -0
- statstudio/ui/main_window.py +3934 -0
- statstudio/ui/plot_interaction.py +178 -0
- statstudio/ui/profiler_widget.py +437 -0
- statstudio/ui/recent.py +82 -0
- statstudio/ui/reports/__init__.py +0 -0
- statstudio/ui/reports/report_view.py +1632 -0
- statstudio/ui/script_jobs.py +35 -0
- statstudio/ui/scripts_panel.py +271 -0
- statstudio/ui/table_document.py +233 -0
- statstudio/ui/table_model.py +285 -0
- statstudio/ui/table_view.py +285 -0
- statstudio/ui/theme.py +645 -0
- statstudio/ui/welcome.py +453 -0
- statstudio/ui/window_controls.py +53 -0
- statstudio/ui/workers.py +53 -0
- statstudio-0.1.0.dist-info/METADATA +261 -0
- statstudio-0.1.0.dist-info/RECORD +110 -0
- statstudio-0.1.0.dist-info/WHEEL +5 -0
- statstudio-0.1.0.dist-info/entry_points.txt +5 -0
- statstudio-0.1.0.dist-info/licenses/LICENSE +674 -0
- statstudio-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import html
|
|
4
|
+
import importlib.resources
|
|
5
|
+
import importlib.util
|
|
6
|
+
import json
|
|
7
|
+
import re
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from functools import lru_cache
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, Iterable
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class HelpCatalogError(ValueError):
|
|
15
|
+
"""Raised when the bundled help catalog is malformed."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class HelpCategory:
|
|
20
|
+
title: str
|
|
21
|
+
description: str = ""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class HelpTopic:
|
|
26
|
+
id: str
|
|
27
|
+
title: str
|
|
28
|
+
category: str
|
|
29
|
+
summary: str
|
|
30
|
+
menu_path: str = ""
|
|
31
|
+
aliases: tuple[str, ...] = ()
|
|
32
|
+
prerequisites: tuple[str, ...] = ()
|
|
33
|
+
steps: tuple[str, ...] = ()
|
|
34
|
+
field_notes: tuple[tuple[str, str], ...] = ()
|
|
35
|
+
expected: tuple[str, ...] = ()
|
|
36
|
+
troubleshooting: tuple[tuple[str, str], ...] = ()
|
|
37
|
+
next_steps: tuple[str, ...] = ()
|
|
38
|
+
example: str = ""
|
|
39
|
+
libraries: tuple[str, ...] = ()
|
|
40
|
+
capabilities: tuple[str, ...] = ()
|
|
41
|
+
limitations: tuple[str, ...] = ()
|
|
42
|
+
keywords: tuple[str, ...] = ()
|
|
43
|
+
scripting: str = ""
|
|
44
|
+
see_also: tuple[str, ...] = ()
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def search_fields(self) -> tuple[tuple[str, int], ...]:
|
|
48
|
+
return (
|
|
49
|
+
(self.title, 40),
|
|
50
|
+
(" ".join(self.keywords + self.aliases), 30),
|
|
51
|
+
(self.menu_path, 12),
|
|
52
|
+
(self.summary, 9),
|
|
53
|
+
(" ".join(self.libraries), 10),
|
|
54
|
+
(" ".join(self.steps), 5),
|
|
55
|
+
(" ".join(self.prerequisites + self.expected + self.next_steps), 4),
|
|
56
|
+
(" ".join(self.capabilities), 4),
|
|
57
|
+
(" ".join(f"{field} {guidance}" for field, guidance in self.field_notes), 4),
|
|
58
|
+
(" ".join(f"{problem} {fix}" for problem, fix in self.troubleshooting), 3),
|
|
59
|
+
(" ".join(self.limitations), 3),
|
|
60
|
+
(self.example, 3),
|
|
61
|
+
(self.scripting, 2),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class HelpCatalog:
|
|
67
|
+
version: int
|
|
68
|
+
categories: tuple[HelpCategory, ...]
|
|
69
|
+
topics: tuple[HelpTopic, ...]
|
|
70
|
+
by_id: dict[str, HelpTopic] = field(init=False, repr=False)
|
|
71
|
+
|
|
72
|
+
def __post_init__(self) -> None:
|
|
73
|
+
self.by_id = {topic.id: topic for topic in self.topics}
|
|
74
|
+
|
|
75
|
+
def topic(self, topic_id: str) -> HelpTopic | None:
|
|
76
|
+
return self.by_id.get(topic_id)
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def category_titles(self) -> tuple[str, ...]:
|
|
80
|
+
return tuple(category.title for category in self.categories)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass(frozen=True)
|
|
84
|
+
class DependencyInfo:
|
|
85
|
+
label: str
|
|
86
|
+
module: str
|
|
87
|
+
extra: str = ""
|
|
88
|
+
description: str = ""
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def install_command(self) -> str:
|
|
92
|
+
if self.extra:
|
|
93
|
+
return f'python -m pip install -e ".[{self.extra}]"'
|
|
94
|
+
return f"python -m pip install {self.module}"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True)
|
|
98
|
+
class DependencyStatus:
|
|
99
|
+
info: DependencyInfo
|
|
100
|
+
installed: bool
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
DEPENDENCIES: dict[str, DependencyInfo] = {
|
|
104
|
+
"arviz": DependencyInfo("ArviZ", "arviz", "bayesian", "Bayesian diagnostics and plotting"),
|
|
105
|
+
"keyring": DependencyInfo("keyring", "keyring", "", "Operating-system credential storage"),
|
|
106
|
+
"pymc": DependencyInfo("PyMC", "pymc", "bayesian", "Bayesian model sampling"),
|
|
107
|
+
"pyodbc": DependencyInfo("pyodbc", "pyodbc", "", "ODBC database connections"),
|
|
108
|
+
"pyreadstat": DependencyInfo("pyreadstat", "pyreadstat", "spss", "SPSS .sav import"),
|
|
109
|
+
"xgboost": DependencyInfo("XGBoost", "xgboost", "ml", "Gradient-boosted predictive models"),
|
|
110
|
+
"xlrd": DependencyInfo("xlrd", "xlrd", "excel", "Legacy .xls import"),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
_DEPENDENCY_ALIASES = {
|
|
114
|
+
"arviz": "arviz",
|
|
115
|
+
"keyring": "keyring",
|
|
116
|
+
"pymc": "pymc",
|
|
117
|
+
"pyodbc": "pyodbc",
|
|
118
|
+
"pyreadstat": "pyreadstat",
|
|
119
|
+
"xgboost": "xgboost",
|
|
120
|
+
"xlrd": "xlrd",
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_TOKEN_RE = re.compile(r"[a-z0-9]+")
|
|
124
|
+
_STOP_WORDS = {
|
|
125
|
+
"a",
|
|
126
|
+
"an",
|
|
127
|
+
"and",
|
|
128
|
+
"are",
|
|
129
|
+
"can",
|
|
130
|
+
"do",
|
|
131
|
+
"does",
|
|
132
|
+
"find",
|
|
133
|
+
"for",
|
|
134
|
+
"from",
|
|
135
|
+
"get",
|
|
136
|
+
"how",
|
|
137
|
+
"i",
|
|
138
|
+
"in",
|
|
139
|
+
"is",
|
|
140
|
+
"it",
|
|
141
|
+
"make",
|
|
142
|
+
"me",
|
|
143
|
+
"my",
|
|
144
|
+
"need",
|
|
145
|
+
"of",
|
|
146
|
+
"on",
|
|
147
|
+
"open",
|
|
148
|
+
"or",
|
|
149
|
+
"perform",
|
|
150
|
+
"run",
|
|
151
|
+
"see",
|
|
152
|
+
"show",
|
|
153
|
+
"the",
|
|
154
|
+
"to",
|
|
155
|
+
"use",
|
|
156
|
+
"want",
|
|
157
|
+
"what",
|
|
158
|
+
"with",
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_CSS = """
|
|
162
|
+
body { color: #263241; font-family: sans-serif; font-size: 10.5pt; line-height: 1.45; }
|
|
163
|
+
h1 { color: #172231; font-size: 20pt; margin: 0 0 4px 0; }
|
|
164
|
+
h2 { color: #24364b; font-size: 13pt; margin: 18px 0 6px 0; }
|
|
165
|
+
p { margin: 6px 0; }
|
|
166
|
+
.category { color: #4a6fa5; font-size: 9pt; font-weight: 600; letter-spacing: 0.4px; }
|
|
167
|
+
.crumbs { color: #647184; font-size: 9.5pt; }
|
|
168
|
+
.summary { color: #35475c; font-size: 11pt; }
|
|
169
|
+
ul, ol { margin: 4px 0 8px 20px; padding: 0; }
|
|
170
|
+
li { margin: 3px 0; }
|
|
171
|
+
.libs li { color: #314257; }
|
|
172
|
+
.badge { border-radius: 7px; color: #ffffff; font-size: 8pt; font-weight: 600; padding: 1px 6px; }
|
|
173
|
+
.ok { background: #3f7f58; }
|
|
174
|
+
.missing { background: #a45132; }
|
|
175
|
+
.callout { background: #f4eee5; border: 1px solid #d8c7ae; border-radius: 6px; margin: 10px 0; padding: 9px 11px; }
|
|
176
|
+
.callout .hint { color: #5d5141; font-size: 9pt; }
|
|
177
|
+
.checklist li { list-style: none; margin-left: -16px; }
|
|
178
|
+
.checklist li:before { content: "\2713 "; color: #3f7f58; font-weight: 700; }
|
|
179
|
+
ol.steps li { margin: 7px 0; }
|
|
180
|
+
ul.expected li { color: #29455f; }
|
|
181
|
+
table.notes { border-collapse: collapse; margin: 6px 0 12px 0; width: 100%; }
|
|
182
|
+
table.notes th, table.notes td { border: 1px solid #d8dee6; padding: 5px 7px; text-align: left; vertical-align: top; }
|
|
183
|
+
table.notes th { background: #f1f4f8; color: #2b3a4b; white-space: nowrap; }
|
|
184
|
+
.problem { background: #fbf5f2; border-left: 3px solid #a45132; margin: 7px 0; padding: 7px 9px; }
|
|
185
|
+
.problem .fix { color: #38523f; }
|
|
186
|
+
ul.next li { color: #2f553b; }
|
|
187
|
+
.limits { color: #5b3c34; }
|
|
188
|
+
code { background: #eef1f4; color: #25313e; font-family: monospace; font-size: 9.5pt; }
|
|
189
|
+
pre { background: #f2f4f6; border: 1px solid #dce1e6; color: #25313e; font-family: monospace; font-size: 9.5pt; margin: 6px 0 12px 0; padding: 9px 10px; white-space: pre-wrap; }
|
|
190
|
+
a { color: #2f64a3; text-decoration: none; }
|
|
191
|
+
.related li { margin: 5px 0; }
|
|
192
|
+
.meta { color: #6b7787; font-size: 9pt; margin-top: 18px; }
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _as_text(value: Any) -> str:
|
|
197
|
+
return "" if value is None else str(value).strip()
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _as_tuple(value: Any) -> tuple[str, ...]:
|
|
201
|
+
if value is None:
|
|
202
|
+
return ()
|
|
203
|
+
if isinstance(value, str):
|
|
204
|
+
values: Iterable[Any] = (value,)
|
|
205
|
+
elif isinstance(value, (list, tuple)):
|
|
206
|
+
values = value
|
|
207
|
+
else:
|
|
208
|
+
values = (value,)
|
|
209
|
+
return tuple(text for item in values if (text := _as_text(item)))
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _as_pairs(value: Any, first_key: str, second_key: str, label: str) -> tuple[tuple[str, str], ...]:
|
|
213
|
+
if value is None:
|
|
214
|
+
return ()
|
|
215
|
+
if not isinstance(value, (list, tuple)):
|
|
216
|
+
raise HelpCatalogError(f"Help topic {label} must be a list.")
|
|
217
|
+
pairs: list[tuple[str, str]] = []
|
|
218
|
+
for item in value:
|
|
219
|
+
if isinstance(item, dict):
|
|
220
|
+
first = _as_text(item.get(first_key))
|
|
221
|
+
second = _as_text(item.get(second_key))
|
|
222
|
+
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
|
223
|
+
first = _as_text(item[0])
|
|
224
|
+
second = _as_text(item[1])
|
|
225
|
+
else:
|
|
226
|
+
raise HelpCatalogError(f"Help topic {label} entries must be two-item objects or lists.")
|
|
227
|
+
if first and second:
|
|
228
|
+
pairs.append((first, second))
|
|
229
|
+
return tuple(pairs)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _parse_topic(raw: Any, index: int) -> HelpTopic:
|
|
233
|
+
if not isinstance(raw, dict):
|
|
234
|
+
raise HelpCatalogError(f"Help topic #{index + 1} must be an object.")
|
|
235
|
+
topic_id = _as_text(raw.get("id"))
|
|
236
|
+
title = _as_text(raw.get("title"))
|
|
237
|
+
category = _as_text(raw.get("category"))
|
|
238
|
+
summary = _as_text(raw.get("summary"))
|
|
239
|
+
if not topic_id or not title or not category or not summary:
|
|
240
|
+
raise HelpCatalogError(f"Help topic #{index + 1} is missing id, title, category, or summary.")
|
|
241
|
+
return HelpTopic(
|
|
242
|
+
id=topic_id,
|
|
243
|
+
title=title,
|
|
244
|
+
category=category,
|
|
245
|
+
summary=summary,
|
|
246
|
+
menu_path=_as_text(raw.get("menu_path")),
|
|
247
|
+
aliases=_as_tuple(raw.get("aliases")),
|
|
248
|
+
prerequisites=_as_tuple(raw.get("prerequisites")),
|
|
249
|
+
steps=_as_tuple(raw.get("steps")),
|
|
250
|
+
field_notes=_as_pairs(raw.get("field_notes"), "field", "guidance", f"{topic_id!r} field_notes"),
|
|
251
|
+
expected=_as_tuple(raw.get("expected")),
|
|
252
|
+
troubleshooting=_as_pairs(raw.get("troubleshooting"), "problem", "fix", f"{topic_id!r} troubleshooting"),
|
|
253
|
+
next_steps=_as_tuple(raw.get("next_steps")),
|
|
254
|
+
example=_as_text(raw.get("example")),
|
|
255
|
+
libraries=_as_tuple(raw.get("libraries")),
|
|
256
|
+
capabilities=_as_tuple(raw.get("capabilities")),
|
|
257
|
+
limitations=_as_tuple(raw.get("limitations")),
|
|
258
|
+
keywords=_as_tuple(raw.get("keywords")),
|
|
259
|
+
scripting=_as_text(raw.get("scripting")),
|
|
260
|
+
see_also=_as_tuple(raw.get("see_also")),
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def parse_catalog(payload: Any) -> HelpCatalog:
|
|
265
|
+
if not isinstance(payload, dict):
|
|
266
|
+
raise HelpCatalogError("Help catalog must be a JSON object.")
|
|
267
|
+
try:
|
|
268
|
+
version = int(payload.get("version", 1))
|
|
269
|
+
except (TypeError, ValueError) as exc:
|
|
270
|
+
raise HelpCatalogError("Help catalog version must be an integer.") from exc
|
|
271
|
+
raw_categories = payload.get("categories")
|
|
272
|
+
if not isinstance(raw_categories, list) or not raw_categories:
|
|
273
|
+
raise HelpCatalogError("Help catalog must define at least one category.")
|
|
274
|
+
categories: list[HelpCategory] = []
|
|
275
|
+
for index, raw in enumerate(raw_categories):
|
|
276
|
+
if isinstance(raw, str):
|
|
277
|
+
title = raw.strip()
|
|
278
|
+
description = ""
|
|
279
|
+
elif isinstance(raw, dict):
|
|
280
|
+
title = _as_text(raw.get("title"))
|
|
281
|
+
description = _as_text(raw.get("description"))
|
|
282
|
+
else:
|
|
283
|
+
raise HelpCatalogError(f"Help category #{index + 1} must be a string or object.")
|
|
284
|
+
if not title:
|
|
285
|
+
raise HelpCatalogError(f"Help category #{index + 1} has no title.")
|
|
286
|
+
categories.append(HelpCategory(title, description))
|
|
287
|
+
category_titles = {category.title for category in categories}
|
|
288
|
+
raw_topics = payload.get("topics")
|
|
289
|
+
if not isinstance(raw_topics, list) or not raw_topics:
|
|
290
|
+
raise HelpCatalogError("Help catalog must define at least one topic.")
|
|
291
|
+
topics = tuple(_parse_topic(raw, index) for index, raw in enumerate(raw_topics))
|
|
292
|
+
seen: set[str] = set()
|
|
293
|
+
for topic in topics:
|
|
294
|
+
if topic.id in seen:
|
|
295
|
+
raise HelpCatalogError(f"Duplicate help topic id: {topic.id}")
|
|
296
|
+
seen.add(topic.id)
|
|
297
|
+
if topic.category not in category_titles:
|
|
298
|
+
raise HelpCatalogError(f"Help topic {topic.id!r} uses unknown category {topic.category!r}.")
|
|
299
|
+
for topic in topics:
|
|
300
|
+
missing = [related for related in topic.see_also if related not in seen]
|
|
301
|
+
if missing:
|
|
302
|
+
raise HelpCatalogError(f"Help topic {topic.id!r} references unknown topics: {', '.join(missing)}")
|
|
303
|
+
return HelpCatalog(version=version, categories=tuple(categories), topics=topics)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _catalog_path() -> Any:
|
|
307
|
+
try:
|
|
308
|
+
return importlib.resources.files("statstudio").joinpath("resources").joinpath("help_topics.json")
|
|
309
|
+
except (AttributeError, ImportError, ModuleNotFoundError): # pragma: no cover - old Python fallback
|
|
310
|
+
return Path(__file__).with_name("resources") / "help_topics.json"
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
@lru_cache(maxsize=1)
|
|
314
|
+
def _builtin_catalog() -> HelpCatalog:
|
|
315
|
+
resource = _catalog_path()
|
|
316
|
+
text = resource.read_text(encoding="utf-8")
|
|
317
|
+
try:
|
|
318
|
+
payload = json.loads(text)
|
|
319
|
+
except json.JSONDecodeError as exc:
|
|
320
|
+
raise HelpCatalogError(f"Could not parse help catalog: {exc}") from exc
|
|
321
|
+
return parse_catalog(payload)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def load_help_catalog(path: str | Path | None = None) -> HelpCatalog:
|
|
325
|
+
if path is None:
|
|
326
|
+
return _builtin_catalog()
|
|
327
|
+
text = Path(path).read_text(encoding="utf-8")
|
|
328
|
+
try:
|
|
329
|
+
payload = json.loads(text)
|
|
330
|
+
except json.JSONDecodeError as exc:
|
|
331
|
+
raise HelpCatalogError(f"Could not parse help catalog: {exc}") from exc
|
|
332
|
+
return parse_catalog(payload)
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def _tokens(text: str) -> list[str]:
|
|
336
|
+
return _TOKEN_RE.findall(text.casefold())
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _query_tokens(query: str) -> list[str]:
|
|
340
|
+
tokens = _tokens(query)
|
|
341
|
+
useful = [token for token in tokens if token not in _STOP_WORDS]
|
|
342
|
+
return useful or tokens
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _token_forms(token: str) -> set[str]:
|
|
346
|
+
forms = {token}
|
|
347
|
+
if len(token) > 3 and token.endswith("s"):
|
|
348
|
+
forms.add(token[:-1])
|
|
349
|
+
if len(token) > 5 and token.endswith("ing"):
|
|
350
|
+
forms.add(token[:-3])
|
|
351
|
+
forms.add(token[:-3] + "e")
|
|
352
|
+
if len(token) > 4 and token.endswith("ed"):
|
|
353
|
+
forms.add(token[:-2])
|
|
354
|
+
forms.add(token[:-1])
|
|
355
|
+
return forms
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _score(topic: HelpTopic, query_tokens: list[str], query_phrase: str) -> int:
|
|
359
|
+
score = 0
|
|
360
|
+
for token in query_tokens:
|
|
361
|
+
best = 0
|
|
362
|
+
forms = _token_forms(token)
|
|
363
|
+
for text, weight in topic.search_fields:
|
|
364
|
+
if forms & set(_tokens(text)):
|
|
365
|
+
best = max(best, weight)
|
|
366
|
+
if not best:
|
|
367
|
+
return 0
|
|
368
|
+
score += best
|
|
369
|
+
title = " ".join(_tokens(topic.title))
|
|
370
|
+
keywords = " ".join(_tokens(" ".join(topic.keywords + topic.aliases)))
|
|
371
|
+
menu_path = " ".join(_tokens(topic.menu_path))
|
|
372
|
+
if title == query_phrase:
|
|
373
|
+
score += 300
|
|
374
|
+
elif query_phrase and query_phrase in title:
|
|
375
|
+
score += 120
|
|
376
|
+
if query_phrase and query_phrase in keywords:
|
|
377
|
+
score += 70
|
|
378
|
+
if query_phrase and query_phrase in menu_path:
|
|
379
|
+
score += 25
|
|
380
|
+
if topic.category == "Quick Start":
|
|
381
|
+
score -= 20
|
|
382
|
+
return score
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def search_topics(catalog: HelpCatalog, query: str, category: str = "") -> list[HelpTopic]:
|
|
386
|
+
topics = list(catalog.topics)
|
|
387
|
+
if category:
|
|
388
|
+
topics = [topic for topic in topics if topic.category == category]
|
|
389
|
+
needle = query.strip()
|
|
390
|
+
if not needle:
|
|
391
|
+
return topics
|
|
392
|
+
query_tokens = _query_tokens(needle)
|
|
393
|
+
if not query_tokens:
|
|
394
|
+
return []
|
|
395
|
+
query_phrase = " ".join(query_tokens)
|
|
396
|
+
ranked = [
|
|
397
|
+
(score, topic)
|
|
398
|
+
for topic in topics
|
|
399
|
+
if (score := _score(topic, query_tokens, query_phrase)) > 0
|
|
400
|
+
]
|
|
401
|
+
ranked.sort(key=lambda item: (-item[0], item[1].title.casefold()))
|
|
402
|
+
return [topic for _, topic in ranked]
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _dependency_key(name: str) -> str | None:
|
|
406
|
+
candidate = name.strip().casefold()
|
|
407
|
+
if not candidate:
|
|
408
|
+
return None
|
|
409
|
+
first = re.split(r"\s+[—–-]\s+|\s*:\s*|\s+", candidate, maxsplit=1)[0]
|
|
410
|
+
first = first.strip("(),[]")
|
|
411
|
+
return _DEPENDENCY_ALIASES.get(first)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
@lru_cache(maxsize=1)
|
|
415
|
+
def dependency_status() -> dict[str, DependencyStatus]:
|
|
416
|
+
statuses: dict[str, DependencyStatus] = {}
|
|
417
|
+
for key, info in DEPENDENCIES.items():
|
|
418
|
+
try:
|
|
419
|
+
installed = importlib.util.find_spec(info.module) is not None
|
|
420
|
+
except (ImportError, ModuleNotFoundError, ValueError):
|
|
421
|
+
installed = False
|
|
422
|
+
statuses[key] = DependencyStatus(info, installed)
|
|
423
|
+
return statuses
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
def _library_parts(value: str) -> tuple[str, str]:
|
|
427
|
+
for separator in (" — ", " – ", " - ", ": "):
|
|
428
|
+
if separator in value:
|
|
429
|
+
name, role = value.split(separator, 1)
|
|
430
|
+
return name.strip(), role.strip()
|
|
431
|
+
return value.strip(), ""
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _escape(text: str) -> str:
|
|
435
|
+
return html.escape(str(text), quote=True)
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def _list_html(items: Iterable[str], class_name: str = "") -> str:
|
|
439
|
+
values = [f"<li>{_escape(item)}</li>" for item in items]
|
|
440
|
+
if not values:
|
|
441
|
+
return ""
|
|
442
|
+
attribute = f' class="{class_name}"' if class_name else ""
|
|
443
|
+
return f"<ul{attribute}>{''.join(values)}</ul>"
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def _library_html(topic: HelpTopic, statuses: dict[str, DependencyStatus]) -> tuple[str, list[DependencyStatus]]:
|
|
447
|
+
items: list[str] = []
|
|
448
|
+
missing: list[DependencyStatus] = []
|
|
449
|
+
seen_missing: set[str] = set()
|
|
450
|
+
for library in topic.libraries:
|
|
451
|
+
name, role = _library_parts(library)
|
|
452
|
+
detail = f" <span style=\"color:#657386;\">— {_escape(role)}</span>" if role else ""
|
|
453
|
+
key = _dependency_key(name)
|
|
454
|
+
status = statuses.get(key) if key else None
|
|
455
|
+
badge = ""
|
|
456
|
+
if status is not None:
|
|
457
|
+
if status.installed:
|
|
458
|
+
badge = ' <span class="badge ok">installed</span>'
|
|
459
|
+
else:
|
|
460
|
+
badge = ' <span class="badge missing">not installed</span>'
|
|
461
|
+
if status.info.module not in seen_missing:
|
|
462
|
+
missing.append(status)
|
|
463
|
+
seen_missing.add(status.info.module)
|
|
464
|
+
items.append(f"<li><b>{_escape(name)}</b>{detail}{badge}</li>")
|
|
465
|
+
return f'<ul class="libs">{"".join(items)}</ul>', missing
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def _missing_html(missing: list[DependencyStatus]) -> str:
|
|
469
|
+
if not missing:
|
|
470
|
+
return ""
|
|
471
|
+
commands: list[str] = []
|
|
472
|
+
seen: set[str] = set()
|
|
473
|
+
for status in missing:
|
|
474
|
+
info = status.info
|
|
475
|
+
if info.install_command in seen:
|
|
476
|
+
continue
|
|
477
|
+
seen.add(info.install_command)
|
|
478
|
+
commands.append(
|
|
479
|
+
f"<li><b>{_escape(info.label)}</b>: <code>{_escape(info.install_command)}</code></li>"
|
|
480
|
+
)
|
|
481
|
+
return (
|
|
482
|
+
'<div class="callout"><b>Optional Python packages are missing.</b>'
|
|
483
|
+
f"<ul>{''.join(commands)}</ul>"
|
|
484
|
+
'<p class="hint">Install these into the same Python environment that runs StatStudio. '
|
|
485
|
+
"The packaged desktop app uses its bundled Python interpreter; installing packages into a "
|
|
486
|
+
"system Python does not change the app. Rebuild the bundle to include extra packages.</p></div>"
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def render_topic(
|
|
491
|
+
topic: HelpTopic,
|
|
492
|
+
catalog: HelpCatalog,
|
|
493
|
+
*,
|
|
494
|
+
statuses: dict[str, DependencyStatus] | None = None,
|
|
495
|
+
include_related: bool = True,
|
|
496
|
+
) -> str:
|
|
497
|
+
statuses = statuses if statuses is not None else dependency_status()
|
|
498
|
+
parts = [
|
|
499
|
+
"<html><head><style>",
|
|
500
|
+
_CSS,
|
|
501
|
+
"</style></head><body>",
|
|
502
|
+
f'<div class="category">{_escape(topic.category)}</div>',
|
|
503
|
+
f"<h1>{_escape(topic.title)}</h1>",
|
|
504
|
+
]
|
|
505
|
+
if topic.menu_path:
|
|
506
|
+
parts.append(f'<div class="crumbs">Menu: {_escape(topic.menu_path)}</div>')
|
|
507
|
+
parts.append(f'<p class="summary">{_escape(topic.summary)}</p>')
|
|
508
|
+
|
|
509
|
+
if topic.prerequisites:
|
|
510
|
+
parts.append(
|
|
511
|
+
'<h2>Before you begin</h2><div class="callout"><ul class="checklist">'
|
|
512
|
+
+ "".join(f"<li>{_escape(item)}</li>" for item in topic.prerequisites)
|
|
513
|
+
+ "</ul></div>"
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
if topic.steps:
|
|
517
|
+
steps = "".join(f"<li>{_escape(step)}</li>" for step in topic.steps)
|
|
518
|
+
parts.append(f"<h2>Step-by-step instructions</h2><ol class=\"steps\">{steps}</ol>")
|
|
519
|
+
|
|
520
|
+
if topic.field_notes:
|
|
521
|
+
rows = "".join(
|
|
522
|
+
f"<tr><th>{_escape(field)}</th><td>{_escape(guidance)}</td></tr>"
|
|
523
|
+
for field, guidance in topic.field_notes
|
|
524
|
+
)
|
|
525
|
+
parts.append(
|
|
526
|
+
'<h2>Settings guide</h2><table class="notes"><thead><tr>'
|
|
527
|
+
"<th>Control</th><th>What to choose</th></tr></thead><tbody>"
|
|
528
|
+
+ rows
|
|
529
|
+
+ "</tbody></table>"
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
if topic.example:
|
|
533
|
+
parts.append(f'<h2>Example</h2><div class="callout">{_escape(topic.example)}</div>')
|
|
534
|
+
|
|
535
|
+
if topic.expected:
|
|
536
|
+
parts.append(
|
|
537
|
+
'<h2>What you should see</h2><ul class="expected">'
|
|
538
|
+
+ "".join(f"<li>{_escape(item)}</li>" for item in topic.expected)
|
|
539
|
+
+ "</ul>"
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
if topic.troubleshooting:
|
|
543
|
+
problems = "".join(
|
|
544
|
+
'<div class="problem"><b>Problem:</b> '
|
|
545
|
+
f"{_escape(problem)}<br><span class=\"fix\"><b>Fix:</b> {_escape(fix)}</span></div>"
|
|
546
|
+
for problem, fix in topic.troubleshooting
|
|
547
|
+
)
|
|
548
|
+
parts.append(f'<h2>If something goes wrong</h2><div class="problems">{problems}</div>')
|
|
549
|
+
|
|
550
|
+
if topic.next_steps:
|
|
551
|
+
parts.append(
|
|
552
|
+
'<h2>Next steps</h2><ul class="next">'
|
|
553
|
+
+ "".join(f"<li>{_escape(item)}</li>" for item in topic.next_steps)
|
|
554
|
+
+ "</ul>"
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
if topic.capabilities:
|
|
558
|
+
parts.append("<h2>Capabilities</h2>" + _list_html(topic.capabilities))
|
|
559
|
+
|
|
560
|
+
if topic.libraries:
|
|
561
|
+
library_list, missing = _library_html(topic, statuses)
|
|
562
|
+
parts.append("<h2>Python libraries</h2>" + library_list)
|
|
563
|
+
parts.append(_missing_html(missing))
|
|
564
|
+
|
|
565
|
+
if topic.limitations:
|
|
566
|
+
parts.append("<h2>Limitations and notes</h2>" + _list_html(topic.limitations, "limits"))
|
|
567
|
+
|
|
568
|
+
if topic.scripting:
|
|
569
|
+
parts.append(
|
|
570
|
+
"<h2>Automation example</h2>"
|
|
571
|
+
f"<pre>{_escape(topic.scripting)}</pre>"
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
if include_related and topic.see_also:
|
|
575
|
+
links = []
|
|
576
|
+
for related_id in topic.see_also:
|
|
577
|
+
related = catalog.topic(related_id)
|
|
578
|
+
if related is not None:
|
|
579
|
+
links.append(
|
|
580
|
+
f'<li><a href="topic:{_escape(related.id)}">{_escape(related.title)}</a></li>'
|
|
581
|
+
)
|
|
582
|
+
if links:
|
|
583
|
+
parts.append('<h2>Related help</h2><ul class="related">' + "".join(links) + "</ul>")
|
|
584
|
+
|
|
585
|
+
parts.append('<p class="meta">StatStudio built-in help · searchable and available offline</p>')
|
|
586
|
+
parts.append("</body></html>")
|
|
587
|
+
return "".join(parts)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def library_install_command(name: str) -> str | None:
|
|
591
|
+
key = _dependency_key(name)
|
|
592
|
+
if not key:
|
|
593
|
+
return None
|
|
594
|
+
info = DEPENDENCIES.get(key)
|
|
595
|
+
return info.install_command if info else None
|