compleathorseplayer / Neptune.jl

Simple (Pluto-based) non-reactive notebooks for Julia
Other
67 stars 5 forks source link

@printf? #22

Open dpo opened 3 years ago

dpo commented 3 years ago

Is there a convenient way to print in Neptune (contrary to Pluto)?

dpo commented 3 years ago
using PlutoUI
with_terminal() do
    println("BLA")
end

outputs nothing at all in a fresh Neptune notebook.

dpo commented 2 years ago

Put this together recently, which does the job

using IOCapture
import HypertextLiteral: @htl

function display_output(value, output)
    # parts of this scavenged from PlutoUI
    t = """<div style='display: inline; white-space: normal;'><pre style="
                            max-height: 300px;
                            overflow: auto;
                            white-space: pre;
                            color: white;
                            background-color: black;
                            border-radius: 3px;
                            margin-top: 4px;
                            margin-bottom: 4px;
                            padding: 5px;
                            display: block;
                            font-family: monospace;
                            font-size: 9pt;
                        ">"""
    t *= value == nothing ? "" : "<span>$(value)<br></span>"
    for line in split(output, "\n")[1:(end-1)]
        t *= "<span>$(line)<br></span>"
    end
    t *= "</pre></div>"
    ex = :(@htl($t))
    ex
end

macro show_output(cmd)
  return quote
    c = IOCapture.capture(color = true) do
      eval($cmd)
    end
    ex = display_output(c.value, c.output)
    eval(ex)
  end
end

The above allows you to print in your functions, including logging, and showing the output in the Neptune notebook with the macro @show_output.

Screen Shot 2021-11-04 at 15 42 52

Colors don't appear to be working though.

kpa28-git commented 2 years ago

@dpo Thanks for this

I'm curious why loops like

for i in 1:5
    @show_output i
end

don't work? It says i is undefined