Clash between plots and printed text in Julia notebook

Viewed 129

I have a Julia Jupyter notebook in which I generate plots with Plots, e.g., something like

thxpplot=plot(title=title,xlabel="T (K)",ylabel="V/V0")
thxpplot=plot!(Tplt,Vplt/V0,label=mat,color="red")
display((thxpplot))

and also text output, e.g.,

@printf("%s: %s (%d atoms)\n","Material",mat,N)

Annoyingly, both output streams don't seem to be synchronized. It frequently happens, especially in larger examples than this, that a part of the printed text appears before the plot and the rest after it, even though the succession of instructions as given by the code clearly separates both. How can I ensure proper synchronization between these output streams, or in other words, how can I flush the output buffers immediately? I don't have the impression that this is an issue in Python notebooks.

Thomas

1 Answers

you can flush the standard output with

flush(stdout)

However, if you mix plots with text be aware that plots use the display command rather than println and sometimes you might still run into synchronization issues because those streams are handled separately by Jupyter.

The solution in that case is to use:

display("some text you want to display")

In this way you will be able to control the order of output.

In your case instead of @printf you might want to use:

display(@sprintf("whatever you want to print"))

This however uses a default text/plain MIME type (and adds quotes etc).

Your results could be nicer via different MIME type:

display("text/json", @sprintf("%s: %s (%d atoms)\n","Material",4,5))

Or you can even use HTML

display("text/html", @sprintf("%s: <b>%s</b> (<b>%d</b> atoms)\n","Material",4,5))
Related