Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

feat(python): add optional per-line stream callback to PythonRunner::run

tilakpatel22ddd23151e9cb
diff --git a/fincept-qt/src/python/PythonRunner.cpp b/fincept-qt/src/python/PythonRunner.cpp
index 50de689a8..b9d728bb9 100644
--- a/fincept-qt/src/python/PythonRunner.cpp
+++ b/fincept-qt/src/python/PythonRunner.cpp
@@ -181,7 +181,7 @@ QString PythonRunner::find_scripts_dir() const {
// ── Run Script ───────────────────────────────────────────────────────────────
-void PythonRunner::run(const QString& script, const QStringList& args, Callback cb) {
+void PythonRunner::run(const QString& script, const QStringList& args, Callback cb, StreamCallback on_line) {
if (python_init_done_ && python_path_.isEmpty()) {
cb({false, {}, "Python not available — run first-time setup from the app", -1});
return;
@@ -194,7 +194,7 @@ void PythonRunner::run(const QString& script, const QStringList& args, Callback
}
// Queue the request and start if under concurrency limit
- queue_.enqueue({script, args, std::move(cb)});
+ queue_.enqueue({script, args, std::move(cb), std::move(on_line)});
start_next();
}
@@ -341,14 +341,42 @@ void PythonRunner::start_next() {
});
#endif
- // Initialize incremental output buffers
+ // Initialize incremental output buffers (carry stream callback from the request)
proc_buffers_[proc] = {};
+ proc_buffers_[proc].on_line = req.on_line;
+
+ // Helper: drain complete \n-terminated lines from a buffer starting at `offset`,
+ // invoke `on_line(line, is_stderr)` per line, advance `offset`. Trailing
+ // partial line (no \n yet) stays in the buffer for the next read.
+ auto drain_lines = [this, proc](bool is_stderr) {
+ auto& bufs = proc_buffers_[proc];
+ if (!bufs.on_line) return;
+ QByteArray& buf = is_stderr ? bufs.stderr_buf : bufs.stdout_buf;
+ int& off = is_stderr ? bufs.stderr_streamed : bufs.stdout_streamed;
+ while (true) {
+ int nl = buf.indexOf('\n', off);
+ if (nl < 0) break;
+ // Line is buf[off..nl) — exclude the trailing \n; also trim \r for CRLF
+ QByteArray line = buf.mid(off, nl - off);
+ if (line.endsWith('\r')) line.chop(1);
+ off = nl + 1;
+ bufs.on_line(QString::fromUtf8(line), is_stderr);
+ }
+ };
- // Buffer stdout/stderr incrementally instead of reading all at once on finish
+ // Buffer stdout/stderr incrementally. After each append, drain complete lines
+ // to the optional stream callback. The full buffer is still available for the
+ // `finished` handler's authoritative parse.
connect(proc, &QProcess::readyReadStandardOutput, this,
- [this, proc]() { proc_buffers_[proc].stdout_buf.append(proc->readAllStandardOutput()); });
+ [this, proc, drain_lines]() {
+ proc_buffers_[proc].stdout_buf.append(proc->readAllStandardOutput());
+ drain_lines(false);
+ });
connect(proc, &QProcess::readyReadStandardError, this,
- [this, proc]() { proc_buffers_[proc].stderr_buf.append(proc->readAllStandardError()); });
+ [this, proc, drain_lines]() {
+ proc_buffers_[proc].stderr_buf.append(proc->readAllStandardError());
+ drain_lines(true);
+ });
auto cb = std::move(req.cb);
auto script_name = std::move(req.script);
diff --git a/fincept-qt/src/python/PythonRunner.h b/fincept-qt/src/python/PythonRunner.h
index f9a08bb0a..a02a52084 100644
--- a/fincept-qt/src/python/PythonRunner.h
+++ b/fincept-qt/src/python/PythonRunner.h
@@ -23,12 +23,18 @@ class PythonRunner : public QObject {
Q_OBJECT
public:
using Callback = std::function<void(PythonResult)>;
+ /// Per-line streaming callback. Invoked on the thread that owns PythonRunner
+ /// (main thread in practice) as each complete `\n`-terminated line arrives.
+ /// `is_stderr` is true for stderr lines, false for stdout.
+ using StreamCallback = std::function<void(QString line, bool is_stderr)>;
static PythonRunner& instance();
/// Run a script asynchronously. Callback invoked on Qt event loop.
/// Requests are queued if max concurrency is reached.
- void run(const QString& script, const QStringList& args, Callback cb);
+ /// Optional `on_line` delivers each complete stdout/stderr line as it arrives.
+ void run(const QString& script, const QStringList& args, Callback cb,
+ StreamCallback on_line = {});
/// Run arbitrary Python code (for notebook/colab cells).
/// Creates a temp file, executes it, returns stdout/stderr.
@@ -66,6 +72,7 @@ class PythonRunner : public QObject {
QString script;
QStringList args;
Callback cb;
+ StreamCallback on_line;
};
QQueue<QueuedRequest> queue_;
@@ -73,6 +80,9 @@ class PythonRunner : public QObject {
struct ProcessBuffers {
QByteArray stdout_buf;
QByteArray stderr_buf;
+ int stdout_streamed = 0; // offset into stdout_buf already streamed
+ int stderr_streamed = 0;
+ StreamCallback on_line;
};
QHash<QProcess*, ProcessBuffers> proc_buffers_;
};