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,218 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import html
|
|
5
|
+
import re
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Callable
|
|
8
|
+
|
|
9
|
+
from PySide6.QtCore import Qt
|
|
10
|
+
from PySide6.QtWidgets import (
|
|
11
|
+
QDialogButtonBox,
|
|
12
|
+
QHBoxLayout,
|
|
13
|
+
QLabel,
|
|
14
|
+
QListWidget,
|
|
15
|
+
QListWidgetItem,
|
|
16
|
+
QMessageBox,
|
|
17
|
+
QPushButton,
|
|
18
|
+
QVBoxLayout,
|
|
19
|
+
QWidget,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
from statstudio.core.project import Project, ReportRecord
|
|
23
|
+
from statstudio.ui.dialogs.window import IndependentDialog
|
|
24
|
+
|
|
25
|
+
_TEMPLATES = {
|
|
26
|
+
"clean": "body{font-family:Arial,Helvetica,sans-serif;font-size:13px;color:#20242a;background:#fff;padding:24px;} h1{font-size:22px;} h2{font-size:16px;border-bottom:1px solid #d8dde4;padding-bottom:3px;} .cover{text-align:center;margin:80px 0 40px 0;} table.data{border-collapse:collapse;} table.data th,table.data td{border:1px solid #d8dde4;padding:4px 8px;}",
|
|
27
|
+
"compact": "body{font-family:Arial,Helvetica,sans-serif;font-size:12px;color:#20242a;padding:12px;} h1{font-size:18px;} h2{font-size:14px;margin:12px 0 4px 0;} .cover{margin:30px 0;} table.data{border-collapse:collapse;font-size:11px;} table.data th,table.data td{border:1px solid #ccd3dc;padding:2px 5px;}",
|
|
28
|
+
"branded": "body{font-family:Georgia,serif;font-size:13px;color:#1c2530;padding:28px;} h1{font-size:24px;color:#1f4e79;} h2{font-size:16px;color:#1f4e79;border-bottom:2px solid #1f4e79;padding-bottom:3px;} .cover{text-align:center;margin:90px 0 50px 0;border-top:4px solid #1f4e79;border-bottom:4px solid #1f4e79;padding:30px 0;} table.data{border-collapse:collapse;} table.data th,table.data td{border:1px solid #9fb6cd;padding:4px 8px;}",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _body_content(document: str) -> str:
|
|
33
|
+
match = re.search(r"<body[^>]*>(.*)</body>", document, re.IGNORECASE | re.DOTALL)
|
|
34
|
+
return match.group(1) if match else document
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _strip_tags(document: str) -> list[str]:
|
|
38
|
+
text = re.sub(r"<(script|style)[^>]*>.*?</\1>", "", document, flags=re.IGNORECASE | re.DOTALL)
|
|
39
|
+
text = re.sub(r"<br\s*/?>", "\n", text, flags=re.IGNORECASE)
|
|
40
|
+
text = re.sub(r"</(p|h1|h2|div|tr|table)>", "\n", text, flags=re.IGNORECASE)
|
|
41
|
+
text = re.sub(r"<[^>]+>", "", text)
|
|
42
|
+
lines = [html.unescape(line).strip() for line in text.splitlines()]
|
|
43
|
+
return [line for line in lines if line]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ReportComposerDialog(IndependentDialog):
|
|
47
|
+
def __init__(
|
|
48
|
+
self,
|
|
49
|
+
project: Project,
|
|
50
|
+
parent: QWidget | None = None,
|
|
51
|
+
html_resolver: Callable[[ReportRecord], str] | None = None,
|
|
52
|
+
) -> None:
|
|
53
|
+
super().__init__(parent)
|
|
54
|
+
self.project = project
|
|
55
|
+
self.html_resolver = html_resolver
|
|
56
|
+
self.setWindowTitle("Report Composer")
|
|
57
|
+
self.resize(720, 560)
|
|
58
|
+
layout = QVBoxLayout(self)
|
|
59
|
+
layout.addWidget(QLabel("Select and order analyses for the combined report. Drag items to reorder."))
|
|
60
|
+
|
|
61
|
+
self.list = QListWidget()
|
|
62
|
+
self.list.setDragDropMode(QListWidget.DragDropMode.InternalMove)
|
|
63
|
+
for record in project.reports:
|
|
64
|
+
item = QListWidgetItem(record.title)
|
|
65
|
+
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable)
|
|
66
|
+
item.setCheckState(Qt.CheckState.Checked)
|
|
67
|
+
item.setData(Qt.ItemDataRole.UserRole, record)
|
|
68
|
+
self.list.addItem(item)
|
|
69
|
+
layout.addWidget(self.list, 1)
|
|
70
|
+
|
|
71
|
+
buttons = QHBoxLayout()
|
|
72
|
+
for text, slot in (("Select all", lambda: self._check_all(True)), ("Select none", lambda: self._check_all(False)), ("Move up", lambda: self._move(-1)), ("Move down", lambda: self._move(1))):
|
|
73
|
+
button = QPushButton(text)
|
|
74
|
+
button.clicked.connect(slot)
|
|
75
|
+
buttons.addWidget(button)
|
|
76
|
+
buttons.addStretch(1)
|
|
77
|
+
layout.addLayout(buttons)
|
|
78
|
+
|
|
79
|
+
self.title_edit = self._line("Report title", "StatStudio Report")
|
|
80
|
+
self.author_edit = self._line("Author", "")
|
|
81
|
+
self.subtitle_edit = self._line("Subtitle", "")
|
|
82
|
+
from PySide6.QtWidgets import QComboBox
|
|
83
|
+
|
|
84
|
+
self.template_combo = QComboBox()
|
|
85
|
+
self.template_combo.addItems(list(_TEMPLATES.keys()))
|
|
86
|
+
self._row(layout, "Template", self.template_combo)
|
|
87
|
+
|
|
88
|
+
action_row = QHBoxLayout()
|
|
89
|
+
export_html = QPushButton("Build & Export HTML")
|
|
90
|
+
export_html.clicked.connect(self.export_html)
|
|
91
|
+
export_word = QPushButton("Build & Export Word")
|
|
92
|
+
export_word.clicked.connect(self.export_word)
|
|
93
|
+
action_row.addWidget(export_html)
|
|
94
|
+
action_row.addWidget(export_word)
|
|
95
|
+
close = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
|
96
|
+
close.rejected.connect(self.reject)
|
|
97
|
+
close.accepted.connect(self.accept)
|
|
98
|
+
from statstudio.ui.dialogs.help_topics import add_help_button, install_context_help
|
|
99
|
+
|
|
100
|
+
add_help_button(close, self)
|
|
101
|
+
install_context_help(self)
|
|
102
|
+
action_row.addWidget(close)
|
|
103
|
+
layout.addLayout(action_row)
|
|
104
|
+
|
|
105
|
+
def _line(self, label: str, default: str):
|
|
106
|
+
from PySide6.QtWidgets import QLineEdit
|
|
107
|
+
|
|
108
|
+
edit = QLineEdit(default)
|
|
109
|
+
self._row(self.layout(), label, edit)
|
|
110
|
+
return edit
|
|
111
|
+
|
|
112
|
+
def _row(self, layout, label: str, widget) -> None:
|
|
113
|
+
row = QHBoxLayout()
|
|
114
|
+
row.addWidget(QLabel(label))
|
|
115
|
+
row.addWidget(widget, 1)
|
|
116
|
+
layout.addLayout(row)
|
|
117
|
+
|
|
118
|
+
def _check_all(self, checked: bool) -> None:
|
|
119
|
+
state = Qt.CheckState.Checked if checked else Qt.CheckState.Unchecked
|
|
120
|
+
for index in range(self.list.count()):
|
|
121
|
+
self.list.item(index).setCheckState(state)
|
|
122
|
+
|
|
123
|
+
def _move(self, direction: int) -> None:
|
|
124
|
+
row = self.list.currentRow()
|
|
125
|
+
if row < 0:
|
|
126
|
+
return
|
|
127
|
+
target = row + direction
|
|
128
|
+
if target < 0 or target >= self.list.count():
|
|
129
|
+
return
|
|
130
|
+
item = self.list.takeItem(row)
|
|
131
|
+
self.list.insertItem(target, item)
|
|
132
|
+
self.list.setCurrentRow(target)
|
|
133
|
+
|
|
134
|
+
def _selected(self) -> list[ReportRecord]:
|
|
135
|
+
records = []
|
|
136
|
+
for index in range(self.list.count()):
|
|
137
|
+
item = self.list.item(index)
|
|
138
|
+
if item.checkState() == Qt.CheckState.Checked:
|
|
139
|
+
records.append(item.data(Qt.ItemDataRole.UserRole))
|
|
140
|
+
return records
|
|
141
|
+
|
|
142
|
+
def _record_html(self, record: ReportRecord) -> str:
|
|
143
|
+
if self.html_resolver is not None:
|
|
144
|
+
try:
|
|
145
|
+
return self.html_resolver(record)
|
|
146
|
+
except Exception:
|
|
147
|
+
pass
|
|
148
|
+
return record.html
|
|
149
|
+
|
|
150
|
+
def build_html(self) -> str:
|
|
151
|
+
records = self._selected()
|
|
152
|
+
if not records:
|
|
153
|
+
raise ValueError("Select at least one analysis.")
|
|
154
|
+
title = self.title_edit.text().strip() or "Report"
|
|
155
|
+
author = self.author_edit.text().strip()
|
|
156
|
+
subtitle = self.subtitle_edit.text().strip()
|
|
157
|
+
template = self.template_combo.currentText()
|
|
158
|
+
style = _TEMPLATES.get(template, _TEMPLATES["clean"])
|
|
159
|
+
parts = ["<html><head><meta charset='utf-8'>", f"<style>{style}</style></head><body>"]
|
|
160
|
+
parts.append("<div class='cover'>")
|
|
161
|
+
parts.append(f"<h1>{html.escape(title)}</h1>")
|
|
162
|
+
if subtitle:
|
|
163
|
+
parts.append(f"<h2>{html.escape(subtitle)}</h2>")
|
|
164
|
+
if author:
|
|
165
|
+
parts.append(f"<p>{html.escape(author)}</p>")
|
|
166
|
+
parts.append("</div>")
|
|
167
|
+
for record in records:
|
|
168
|
+
parts.append(f"<h2>{html.escape(record.title)}</h2>")
|
|
169
|
+
parts.append(_body_content(self._record_html(record)))
|
|
170
|
+
parts.append("</body></html>")
|
|
171
|
+
return "".join(parts)
|
|
172
|
+
|
|
173
|
+
def export_html(self) -> None:
|
|
174
|
+
from PySide6.QtWidgets import QFileDialog
|
|
175
|
+
|
|
176
|
+
try:
|
|
177
|
+
content = self.build_html()
|
|
178
|
+
except Exception as exc:
|
|
179
|
+
QMessageBox.warning(self, "Report Composer", str(exc))
|
|
180
|
+
return
|
|
181
|
+
path, _ = QFileDialog.getSaveFileName(self, "Export Report", "StatStudio-report.html", "HTML (*.html)")
|
|
182
|
+
if not path:
|
|
183
|
+
return
|
|
184
|
+
Path(path).write_text(content, encoding="utf-8")
|
|
185
|
+
|
|
186
|
+
def export_word(self) -> None:
|
|
187
|
+
from PySide6.QtWidgets import QFileDialog
|
|
188
|
+
|
|
189
|
+
from statstudio.io.docx import write_docx
|
|
190
|
+
|
|
191
|
+
records = self._selected()
|
|
192
|
+
if not records:
|
|
193
|
+
QMessageBox.warning(self, "Report Composer", "Select at least one analysis.")
|
|
194
|
+
return
|
|
195
|
+
path, _ = QFileDialog.getSaveFileName(self, "Export Report", "StatStudio-report.docx", "Word (*.docx)")
|
|
196
|
+
if not path:
|
|
197
|
+
return
|
|
198
|
+
blocks: list[tuple[str, object]] = []
|
|
199
|
+
for record in records:
|
|
200
|
+
document = self._record_html(record)
|
|
201
|
+
blocks.append(("heading", record.title))
|
|
202
|
+
for line in _strip_tags(document):
|
|
203
|
+
blocks.append(("paragraph", line))
|
|
204
|
+
for encoded in re.findall(r"data:image/png;base64,([A-Za-z0-9+/=\s]+)", document):
|
|
205
|
+
try:
|
|
206
|
+
blocks.append(("image_data", base64.b64decode(encoded)))
|
|
207
|
+
except Exception:
|
|
208
|
+
continue
|
|
209
|
+
try:
|
|
210
|
+
write_docx(
|
|
211
|
+
path,
|
|
212
|
+
self.title_edit.text().strip() or "StatStudio Report",
|
|
213
|
+
blocks,
|
|
214
|
+
author=self.author_edit.text().strip(),
|
|
215
|
+
subtitle=self.subtitle_edit.text().strip(),
|
|
216
|
+
)
|
|
217
|
+
except Exception as exc:
|
|
218
|
+
QMessageBox.critical(self, "Export failed", str(exc))
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from PySide6.QtGui import QFont
|
|
6
|
+
from PySide6.QtWidgets import (
|
|
7
|
+
QApplication,
|
|
8
|
+
QDialogButtonBox,
|
|
9
|
+
QFileDialog,
|
|
10
|
+
QHBoxLayout,
|
|
11
|
+
QLabel,
|
|
12
|
+
QListWidget,
|
|
13
|
+
QMessageBox,
|
|
14
|
+
QPlainTextEdit,
|
|
15
|
+
QPushButton,
|
|
16
|
+
QVBoxLayout,
|
|
17
|
+
QWidget,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from statstudio.core import scripting
|
|
21
|
+
from statstudio.core.project import Project
|
|
22
|
+
from statstudio.core.table import DataTable
|
|
23
|
+
from statstudio.io.table_io import TABLE_SUFFIX
|
|
24
|
+
from statstudio.ui.console import PythonConsole
|
|
25
|
+
from statstudio.ui.dialogs.window import IndependentDialog
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _unique_script_name(project: Project, name: str) -> str:
|
|
29
|
+
base = name.strip() or "Recorded script"
|
|
30
|
+
if base not in project.scripts:
|
|
31
|
+
return base
|
|
32
|
+
index = 2
|
|
33
|
+
while f"{base} ({index})" in project.scripts:
|
|
34
|
+
index += 1
|
|
35
|
+
return f"{base} ({index})"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _CodeDialog(IndependentDialog):
|
|
39
|
+
def __init__(self, title: str, code: str, project: Project, parent: QWidget | None = None) -> None:
|
|
40
|
+
super().__init__(parent)
|
|
41
|
+
self.setWindowTitle(title)
|
|
42
|
+
self.resize(900, 620)
|
|
43
|
+
self.project = project
|
|
44
|
+
self.code = code
|
|
45
|
+
layout = QVBoxLayout(self)
|
|
46
|
+
layout.addWidget(QLabel(title))
|
|
47
|
+
self.editor = QPlainTextEdit()
|
|
48
|
+
self.editor.setFont(QFont("monospace", 9))
|
|
49
|
+
self.editor.setPlainText(code)
|
|
50
|
+
layout.addWidget(self.editor, 1)
|
|
51
|
+
buttons = QHBoxLayout()
|
|
52
|
+
copy_button = QPushButton("Copy")
|
|
53
|
+
copy_button.clicked.connect(self._copy)
|
|
54
|
+
buttons.addWidget(copy_button)
|
|
55
|
+
save_button = QPushButton("Save to Scripts")
|
|
56
|
+
save_button.clicked.connect(self._save_to_scripts)
|
|
57
|
+
buttons.addWidget(save_button)
|
|
58
|
+
export_button = QPushButton("Export .py")
|
|
59
|
+
export_button.clicked.connect(self._export)
|
|
60
|
+
buttons.addWidget(export_button)
|
|
61
|
+
buttons.addStretch(1)
|
|
62
|
+
help_button = QPushButton("Help")
|
|
63
|
+
from statstudio.ui.dialogs.help_topics import dialog_help_topic, install_context_help, open_help
|
|
64
|
+
|
|
65
|
+
help_button.clicked.connect(lambda: open_help(self, dialog_help_topic(self)))
|
|
66
|
+
buttons.addWidget(help_button)
|
|
67
|
+
install_context_help(self)
|
|
68
|
+
close = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
|
69
|
+
close.rejected.connect(self.reject)
|
|
70
|
+
close.accepted.connect(self.accept)
|
|
71
|
+
buttons.addWidget(close)
|
|
72
|
+
layout.addLayout(buttons)
|
|
73
|
+
|
|
74
|
+
def _copy(self) -> None:
|
|
75
|
+
QApplication.clipboard().setText(self.editor.toPlainText())
|
|
76
|
+
|
|
77
|
+
def _save_to_scripts(self) -> None:
|
|
78
|
+
name = _unique_script_name(self.project, self.windowTitle())
|
|
79
|
+
self.project.scripts[name] = self.editor.toPlainText()
|
|
80
|
+
window = self.window()
|
|
81
|
+
if hasattr(window, "_mark_project_dirty"):
|
|
82
|
+
window._mark_project_dirty()
|
|
83
|
+
QMessageBox.information(self, "Save Script", f"Saved script {name!r} in the project.")
|
|
84
|
+
|
|
85
|
+
def _export(self) -> None:
|
|
86
|
+
path, _ = QFileDialog.getSaveFileName(self, "Export Python", f"{self.windowTitle()}.py", "Python (*.py)")
|
|
87
|
+
if not path:
|
|
88
|
+
return
|
|
89
|
+
try:
|
|
90
|
+
Path(path).write_text(self.editor.toPlainText(), encoding="utf-8")
|
|
91
|
+
except Exception as exc:
|
|
92
|
+
QMessageBox.critical(self, "Export failed", str(exc))
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class TableScriptsDialog(IndependentDialog):
|
|
96
|
+
def __init__(self, table: DataTable, project: Project, console: PythonConsole, parent: QWidget | None = None) -> None:
|
|
97
|
+
super().__init__(parent)
|
|
98
|
+
self.setWindowTitle(f"Linked Scripts - {table.name}")
|
|
99
|
+
self.resize(900, 620)
|
|
100
|
+
self.table = table
|
|
101
|
+
self.project = project
|
|
102
|
+
self.console = console
|
|
103
|
+
self._job = None
|
|
104
|
+
layout = QVBoxLayout(self)
|
|
105
|
+
content = QHBoxLayout()
|
|
106
|
+
side = QVBoxLayout()
|
|
107
|
+
side.addWidget(QLabel("Scripts linked to this table"))
|
|
108
|
+
self.list = QListWidget()
|
|
109
|
+
self.list.currentRowChanged.connect(self._show_current)
|
|
110
|
+
side.addWidget(self.list, 1)
|
|
111
|
+
content.addLayout(side, 1)
|
|
112
|
+
right = QVBoxLayout()
|
|
113
|
+
self.preview = QPlainTextEdit()
|
|
114
|
+
self.preview.setFont(QFont("monospace", 9))
|
|
115
|
+
self.preview.setReadOnly(True)
|
|
116
|
+
right.addWidget(self.preview, 1)
|
|
117
|
+
actions = QHBoxLayout()
|
|
118
|
+
copy_button = QPushButton("Copy")
|
|
119
|
+
copy_button.clicked.connect(self._copy)
|
|
120
|
+
save_button = QPushButton("Save to Scripts")
|
|
121
|
+
save_button.clicked.connect(self._save)
|
|
122
|
+
run_button = QPushButton("Run")
|
|
123
|
+
run_button.clicked.connect(self._run)
|
|
124
|
+
export_button = QPushButton("Export .py")
|
|
125
|
+
export_button.clicked.connect(self._export)
|
|
126
|
+
delete_button = QPushButton("Delete")
|
|
127
|
+
delete_button.clicked.connect(self._delete)
|
|
128
|
+
for button in (copy_button, save_button, run_button, export_button, delete_button):
|
|
129
|
+
actions.addWidget(button)
|
|
130
|
+
actions.addStretch(1)
|
|
131
|
+
right.addLayout(actions)
|
|
132
|
+
content.addLayout(right, 2)
|
|
133
|
+
layout.addLayout(content)
|
|
134
|
+
close = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
|
135
|
+
close.rejected.connect(self.reject)
|
|
136
|
+
close.accepted.connect(self.accept)
|
|
137
|
+
from statstudio.ui.dialogs.help_topics import add_help_button, install_context_help
|
|
138
|
+
|
|
139
|
+
add_help_button(close, self)
|
|
140
|
+
install_context_help(self)
|
|
141
|
+
layout.addWidget(close)
|
|
142
|
+
self.refresh()
|
|
143
|
+
|
|
144
|
+
def _current(self):
|
|
145
|
+
row = self.list.currentRow()
|
|
146
|
+
if row < 0 or row >= len(self.table.scripts):
|
|
147
|
+
return None
|
|
148
|
+
return self.table.scripts[row]
|
|
149
|
+
|
|
150
|
+
def refresh(self) -> None:
|
|
151
|
+
self.list.blockSignals(True)
|
|
152
|
+
self.list.clear()
|
|
153
|
+
for script in self.table.scripts:
|
|
154
|
+
self.list.addItem(script.name)
|
|
155
|
+
self.list.blockSignals(False)
|
|
156
|
+
if self.table.scripts:
|
|
157
|
+
self.list.setCurrentRow(0)
|
|
158
|
+
else:
|
|
159
|
+
self.preview.clear()
|
|
160
|
+
|
|
161
|
+
def _show_current(self, index: int) -> None:
|
|
162
|
+
script = self._current()
|
|
163
|
+
self.preview.setPlainText(script.code if script else "")
|
|
164
|
+
|
|
165
|
+
def _copy(self) -> None:
|
|
166
|
+
script = self._current()
|
|
167
|
+
if script:
|
|
168
|
+
prelude = (
|
|
169
|
+
scripting.import_prelude()
|
|
170
|
+
+ "\n"
|
|
171
|
+
+ "try:\n"
|
|
172
|
+
+ " table\n"
|
|
173
|
+
+ "except NameError as exc:\n"
|
|
174
|
+
+ " raise RuntimeError('Provide table = load_table(...) before running this linked script.') from exc\n"
|
|
175
|
+
+ "_tables = {table.name: table}\n\n"
|
|
176
|
+
)
|
|
177
|
+
QApplication.clipboard().setText(prelude + scripting.linked_script_body(script))
|
|
178
|
+
|
|
179
|
+
def _save(self) -> None:
|
|
180
|
+
script = self._current()
|
|
181
|
+
if script is None:
|
|
182
|
+
return
|
|
183
|
+
name = _unique_script_name(self.project, script.name)
|
|
184
|
+
self.project.scripts[name] = scripting.linked_script_body(script)
|
|
185
|
+
window = self.window()
|
|
186
|
+
if hasattr(window, "_mark_project_dirty"):
|
|
187
|
+
window._mark_project_dirty()
|
|
188
|
+
QMessageBox.information(self, "Save Script", f"Saved script {name!r} in the project.")
|
|
189
|
+
|
|
190
|
+
def _run(self) -> None:
|
|
191
|
+
script = self._current()
|
|
192
|
+
if script is None or self._job is not None:
|
|
193
|
+
return
|
|
194
|
+
if QMessageBox.question(self, "Run Linked Script", "Run this recorded script against the current table?") != QMessageBox.StandardButton.Yes:
|
|
195
|
+
return
|
|
196
|
+
window = self.window()
|
|
197
|
+
console_dock = getattr(self.console.window(), "console_dock", None)
|
|
198
|
+
if console_dock is not None:
|
|
199
|
+
console_dock.show()
|
|
200
|
+
console_dock.raise_()
|
|
201
|
+
from statstudio.ui.jobs import start_job
|
|
202
|
+
from statstudio.ui.script_jobs import run_script_isolated
|
|
203
|
+
|
|
204
|
+
code = scripting.linked_script_body(script)
|
|
205
|
+
project = self.project
|
|
206
|
+
table = self.table
|
|
207
|
+
self.console.history.append(code)
|
|
208
|
+
self.console.output.appendPlainText(">>> [linked script]")
|
|
209
|
+
|
|
210
|
+
def completed(outcome) -> None:
|
|
211
|
+
self._job = None
|
|
212
|
+
if outcome.output:
|
|
213
|
+
self.console.output.appendPlainText(outcome.output)
|
|
214
|
+
apply = getattr(window, "apply_script_outcome", None)
|
|
215
|
+
if outcome.ok and callable(apply):
|
|
216
|
+
apply(outcome, table)
|
|
217
|
+
if hasattr(window, "_mark_project_dirty"):
|
|
218
|
+
window._mark_project_dirty()
|
|
219
|
+
|
|
220
|
+
def failed(message: str, details: str) -> None:
|
|
221
|
+
self._job = None
|
|
222
|
+
self.console.output.appendPlainText(f"{message}\n{details}")
|
|
223
|
+
|
|
224
|
+
def cancelled() -> None:
|
|
225
|
+
self._job = None
|
|
226
|
+
self.console.output.appendPlainText("Linked script cancelled.")
|
|
227
|
+
|
|
228
|
+
self._job = start_job(
|
|
229
|
+
self,
|
|
230
|
+
"Running Linked Script",
|
|
231
|
+
lambda token, progress: run_script_isolated(project, code, table),
|
|
232
|
+
on_result=completed,
|
|
233
|
+
on_error=failed,
|
|
234
|
+
on_cancelled=cancelled,
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
def _export(self) -> None:
|
|
238
|
+
script = self._current()
|
|
239
|
+
if script is None:
|
|
240
|
+
return
|
|
241
|
+
path, _ = QFileDialog.getSaveFileName(self, "Export Python", f"{script.name}.py", "Python (*.py)")
|
|
242
|
+
if not path:
|
|
243
|
+
return
|
|
244
|
+
prelude = (
|
|
245
|
+
scripting.import_prelude()
|
|
246
|
+
+ "\n"
|
|
247
|
+
+ f"table = load_table(r{str(Path(path).with_suffix(TABLE_SUFFIX))!r})\n"
|
|
248
|
+
+ "_tables = {table.name: table}\n\n"
|
|
249
|
+
)
|
|
250
|
+
try:
|
|
251
|
+
Path(path).write_text(prelude + scripting.linked_script_body(script), encoding="utf-8")
|
|
252
|
+
except Exception as exc:
|
|
253
|
+
QMessageBox.critical(self, "Export failed", str(exc))
|
|
254
|
+
|
|
255
|
+
def _delete(self) -> None:
|
|
256
|
+
script = self._current()
|
|
257
|
+
if script is None:
|
|
258
|
+
return
|
|
259
|
+
if QMessageBox.question(self, "Delete Script", f"Delete linked script {script.name!r}?") != QMessageBox.StandardButton.Yes:
|
|
260
|
+
return
|
|
261
|
+
self.table.scripts.remove(script)
|
|
262
|
+
self.refresh()
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class JournalDialog(_CodeDialog):
|
|
266
|
+
def __init__(self, project: Project, parent: QWidget | None = None) -> None:
|
|
267
|
+
super().__init__("Analysis Journal", scripting.cumulative_script(project, str(project.path or "")), project, parent)
|