Capturing output of IJulia cell

I recently switched from running an optimization problem in Python to Julia, and am really enjoying the change! However, I’ve noticed that when I run a long optimization problem and close a notebook:

  1. I lose the output once it is completed - in Python I could run a cell magic (ie. %%capture output) to keep the printed output either in a file or below the cell, but the same magic does not apply to Julia. Is there an equivalent solution?
  2. I also don’t see any results during the optimization process (ie. progress bar/iteration step). How do people who use JuMP monitor progress in an IJulia notebook?

Generally I’m curious about people’s workflows and I’d love to pick up some best practices from the Julia community. Thank you for your input!

  1. If you save the notebook after the output has run, the printed output should be saved with the notebook.

  2. I think there was an issue flushing io from external solvers to the notebook. I haven’t kept up on recent developments. I use vs-code instead: https://www.julia-vscode.org

ProgressMeter is what you need.

using ProgressMeter, Flux

#define your model
....

optim = Flux.setup(Flux.Adam(0.01), model)  # will store optimiser momentum, etc.

# Training loop, using the whole data set 1000 times:
loss_v = []
@showprogress for epoch in 1:1_000
    for (x, y) in loader
        loss, grads = Flux.withgradient(model) do m
            # Evaluate model and loss inside gradient context:
            y_hat = m(x)
            Flux.crossentropy(y_hat, y)
        end
        Flux.update!(optim, model, grads[1])
        push!(loss_v, loss)  # logging, outside gradient context
    end
end