Controlling the order of output

I was wondering how to control the order of elements in “Out”. For example,

using Plots
const xs = [-pi + i*(pi/10) for i in 0:20]
function print_xs()
p = plot()
for j = 1:3
  ys = sin.(j*xs)
  plot!(p, xs,ys)

This code prints out numbers to the standard output and then makes an image (plot). If you look at the “Out” field, the text appears first but the image appears later and pushes down the text.

I use CoCalc. I’ve started to use Jupyter last week and know very little about it.

1 Like

There’s a lot of ways you could tackle this depending on what aesthetics you seek. For example, you could just move the display of plot to the next cell.

At this point you are saying you are new to Jupyter. Are you familiar with Julia/IJulia though? (I’m guess from your other posts that is Julia code.) Because the solutions may actually more involve Julia than Jupyter. The way I do this sort of thing in Python backed notebooks is using IPython’s abilities to mix and match using display & HTML abilities. Not really Jupyter’s. It may be something to keep in mind as you look around at other Julia notebooks. I think the bottom section of the notebook I referred you to before in another post starts showing you the possibilities under ‘Multimedia display in IJulia’. You can see this section rendered in static form here. It looks like using display that you can control how you arrange display the collected text and plot, I suspect. For example, with IPython I use the following to add a title to a dataframe display:

from IPython.display import display, HTML
display(HTML(f'<b>Interface interactions for {pdbs_to_use[num_to_display]}:</b>'))

You can probably use the IJuila variation of that to arrange the output as you prefer.

Another idea is to collect the text and add it into your plot as annotation or a legend.

You may want to look into what Julia uses to build in pauses. I don’t think that will help in this case; however, combined with collecting the text and not printing it out immediately, it is sometimes useful in delaying some steps while others occur when running Python cells. Sometimes you can use that to delay printing, I cannot tell from your description though if that will help in this case.

1 Like

@fomightez That’s all good to know. It has helped me to understand better how Julia interact with Jupyter.

Yes, I now understand this is a question more about Julia than about Jupyter.

Your post has made me look at the display function of Julia. It turns out that as in Python, display can show not only images but also values. So, the simplest solution to my problem is

display(xs) # show the numbers in the array xs
# . . . 
display(p) # show the image

The cause of my original problem is that the ordinary print function println doesn’t coordinate with display.

Another solution is to flush stdout after printing:

# . . . 

I don’t know this is supposed to always work, but in my particular case, the problem seems to be just the buffering of stdout.

As a note for Julia users, the output format of display is different from that of println. I don’t know how to adjust the formatting for the display function.

1 Like

It seems that this was already reported for IJulia in Display/println order incorrect · Issue #998 · JuliaLang/IJulia.jl · GitHub and fixed by adding auto-flushing for display in flush stdio before display by stevengj · Pull Request #999 · JuliaLang/IJulia.jl · GitHub but there was no new IJulia release since February. If I were you I would see if using the version from master branch works well and maybe lend a helping hand to the maintainers to accelerate release if needed.

It sounds like display and println behave similar to display and print in IPython (where the output is also slightly different). I would search IJulia repo for any workarounds and hints in the issues if you want to adjust the display output :slight_smile:


Thanks for the information and advice. I didn’t find the report you mention. Perhaps I used the wrong keywords to search Google.

When/if I start to use Jupyter in earnest, I’ll perhaps poke them. Currently I’m using Jupyter as an online service, for which I’d need to pay to install modules for myself.