# Progress bar, or printing during execution, in Jupyter/IJulia?

**URL:** <https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916>\
**Category:** General Usage\
**Tags:** question, jupyter, ijulia, functions\
**Created:** [November 8, 2022, 1:04am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916 "2022-11-08T01:04:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [November 8, 2022, 1:04am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/1 "2022-11-08T01:04:41Z")

</div>

I’m not sure whether this is a Julia question, or a Jupyter question, but anyway … I’m working with Julia in a Jupyter notebook (within VSCode on Windows). This is an environment I’m very happy with, and it suits me well. The trouble is that I have some functions that - for particular values of their parameters - take a long time to compute. And a progress bar, or even an occasional `println` statement during execution would be helpful. But in Jupyter (at least in VSCode), there is no output from the cell until it is completed.

Is there any nice way of getting information about execution of a function actually during its execution?

Thanks!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [November 8, 2022, 1:18am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/2 "2022-11-08T01:18:57Z")

</div>

You can use Pluto and this in it, maybe it works in Jupyther too:

> [@ProgressMeter in Pluto](https://discourse.julialang.org/t/progressmeter-in-pluto/69190):
>
> I vaguely remember this being addressed in one of the many Pluto videos, but I am not sure. So my question is: can I use ProgressMeter, or something similar to show the progress of a loop inside a Pluto cell? Right now, there is no output at all from the code below. Ideally one would want to see a dynamic progress bar… e.g.: function test() @showprogress 1 "Computing..." for i in 1:50 sleep(0.1) end end

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 8, 2022, 1:30am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/3 "2022-11-08T01:30:17Z")

</div>

[ProgressMeter.jl](https://github.com/timholy/ProgressMeter.jl) works fine in Jupyter notebooks.

![progress](https://global.discourse-cdn.com/julialang/original/3X/2/2/220fa1815bee4de597705ef2964f3638dfc353b6.webp)

(You can also `flush(stdout)` to force `println` output to appear immediately.)

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [November 8, 2022, 1:35am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/4 "2022-11-08T01:35:42Z")

</div>

I still expect a more interactive tool for Julia. Interact.jl is great, but cannot always work stably.

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [November 8, 2022, 1:49am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/5 "2022-11-08T01:49:03Z")

</div>

Thank you - I’d quite forgotten about `flush(stdout)`. And I’ll try ProgressMeter.

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [November 8, 2022, 2:02am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/6 "2022-11-08T02:02:26Z")

</div>

Thank you - yes ProgressMeter works well on an indexed loop. The trouble is that I’m using a function from the Graphs package, which is known to be slow (requires a lot of computation), and I’m not sure how to meter it. I might ask on its GitHub page.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 8, 2022, 2:08am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/7 "2022-11-08T02:08:57Z")

</div>

> [@amca01](#):
>
> The trouble is that I’m using a function from the Graphs package, which is known to be slow (requires a lot of computation), and I’m not sure how to meter it. I might ask on its GitHub page.

See [Progress meters for tasks with an unknown number of steps](https://github.com/timholy/ProgressMeter.jl#progress-meters-for-tasks-with-an-unknown-number-of-steps).

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [November 8, 2022, 2:48am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/8 "2022-11-08T02:48:27Z")

</div>

Actually it was easy in the end: rather than compute the eccentricities of all vertices of the graph G in one go as in `eccentricity(G)`, I can do it one vertex at a time, building up the list as I go:

```
eccs = []
@showprogress for v in vertices(G)
      eccs = append!(eccs, [eccentricity(G,v)])
end

```

This is very slightly slower, but it does allow the use of a progress bar.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 8, 2022, 3:20am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/9 "2022-11-08T03:20:53Z")

</div>

> [@amca01](#):
>
> ```julia
> eccs = []
> @showprogress for v in vertices(G)
> eccs = append!(eccs, [eccentricity(G,v)])
> end
> 
> ```

Offtopic, there are some problems here that I should point out:

1. If you compare to the [`eccentricities(g)` implementation](https://github.com/JuliaGraphs/Graphs.jl/blob/06669054ed470bcfe4b2ad90ed974f2e65c84bb6/src/distance.jl#L79-L81), you can see that you are calculating `weights(G)` over and over (or is this function just a “free” view?). Possibly better to compute `distmx = weights(G)` outside the loop, and then call `eccentricity(G,v,distmx)`.

2. `eccs = []` is an `Any[]` array, whereas [concretely typed collections](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container) are generally preferred, e.g. `eccs = eltype(distmx)[]` where `distmx = weights(G)` as above.

3. `append!(eccs, [eccentricity(G,v)])` allocates a 1-element array just to append it … better to do `push!(eccs, eccentricity(G,v))`. (You could also pre-allocate the array `eccs` and then just assign to it, since the length is known in advance, but probably not worth the trouble since `eccentricity` is expensive and `push!` is cheap. You could also use `ProgressMeter.progress.map`)

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [November 8, 2022, 3:44am UTC](https://discourse.julialang.org/t/progress-bar-or-printing-during-execution-in-jupyter-ijulia/89916/10 "2022-11-08T03:44:47Z")

</div>

Many thanks indeed! That is excellent advice - you will see I’m somewhat of a Julia/Graphs newbie. (I’ve worked with Julia before, but I’ve done very little computations with graphs, in any language.) Using `push` instead of `append!` is something I guess I should have known … but bad habits from other languages die hard. And in fact most of my previous computations have been small enough that efficiency tweaks are unnecessary. I’m usually quite satisfied if something just works!

Thank you again - I very much appreciate your detailed and generous response.
