From 495cc33733844aa9aa82f9f667e5d6625748f69e Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 15 Nov 2023 00:02:08 +0300 Subject: Added Jupyter-support to Stream:gnuplot() * Renders SVG into a cell instead of a separate window * currently requires some not-yet-merged patch for ILua (see https://github.com/guysv/ilua/issues/5) --- applause.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/applause.lua b/applause.lua index d701330..702e1ce 100644 --- a/applause.lua +++ b/applause.lua @@ -896,11 +896,22 @@ function Stream:gnuplot() error("Cannot plot infinite stream") end + local cmd = "feedgnuplot --exit --lines --ymin -1 --ymax 1 --domain" + + local svg_file + if _G._send_display_data then + -- Some extremely crude support for plotting directly into Jupyter ILua cells. + -- NOTE: With io.popen() we cannot read and write to the pipe at the + -- same time, so we must dump the file to disk. + svg_file = os.tmpname() + cmd = cmd.." --terminal svg >"..svg_file + end + -- NOTE: We're not using Stream:pipe() here, so we can -- efficiently calculate a time index. -- FIXME: Using something like libplplot would be more -- efficient - local hnd = io.popen("feedgnuplot --exit --lines --ymin -1 --ymax 1 --domain", "w") + local hnd = io.popen(cmd, "w") hnd:setvbuf("full") local second = sec() @@ -912,6 +923,13 @@ function Stream:gnuplot() end) hnd:close() + + if _G._send_display_data then + _G._send_display_data{ + ['image/svg+xml'] = io.open(svg_file):read('*a') + } + os.remove(svg_file) + end end --- Print values of stream when they change. -- cgit v1.2.3