# How can I redirect the output from a function that calls \`ProgressMeter\` to an \`Observable\` variable?

**URL:** <https://discourse.julialang.org/t/how-can-i-redirect-the-output-from-a-function-that-calls-progressmeter-to-an-observable-variable/107092>\
**Category:** General Usage\
**Tags:** question, observables\
**Created:** [December 4, 2023, 2:13am UTC](https://discourse.julialang.org/t/how-can-i-redirect-the-output-from-a-function-that-calls-progressmeter-to-an-observable-variable/107092 "2023-12-04T02:13:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zapiano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zapiano/32/48063_2.png) [@zapiano](https://discourse.julialang.org/u/zapiano)\
**Post date:** [December 4, 2023, 2:13am UTC](https://discourse.julialang.org/t/how-can-i-redirect-the-output-from-a-function-that-calls-progressmeter-to-an-observable-variable/107092/1 "2023-12-04T02:13:59Z")

</div>

I have a function that uses `ProgressMeter`. I need the output from `@showprogress` that is called inside that function to be redirected to an `Observable`. Something like the following (except that the following code doesn’t work):

```julia
using ProgressMeter
using Observables

function my_func()
    @showprogress 1 "Computing..." for i in 1:50
        sleep(0.1)
    end
end

progress = Observable("")

redirect_stdio(my_func, stdout=progress[])

```

I know I can’t pass an observable to `redirect_stdio`, but is there a way of doing what I want somehow?

---

<div class="post-metadata">

**Author:** ![ConnectedSystems](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/connectedsystems/32/16815_2.png) [@ConnectedSystems](https://discourse.julialang.org/u/ConnectedSystems)\
**Post date:** [December 4, 2023, 3:22am UTC](https://discourse.julialang.org/t/how-can-i-redirect-the-output-from-a-function-that-calls-progressmeter-to-an-observable-variable/107092/2 "2023-12-04T03:22:10Z")

</div>

There’s an (long standing) open [issue](https://github.com/JuliaLang/julia/issues/12711) to support using IOBuffer directly, but as far as I can tell, it does not yet support it.

The `Progress` type supports specifying an [output](https://github.com/timholy/ProgressMeter.jl/blob/5b3bd1d3ed1828a43ddd477ef085f586530c5614/src/ProgressMeter.jl#L86), but given it expects an IOStream, I think it will run into the same issue.

Thankfully, there’s a package that provides with a good workaround, courtesy of the discussion [here](https://discourse.julialang.org/t/issue-redirect-output-to-an-iobuffer/17578/2)

```julia
using ProgressMeter
using Observables
using Suppressor

function my_func()
    @showprogress 1 "Computing..." for i in 1:50
        sleep(0.1)
    end
end

x = Observable("")

x[] = @capture_err begin
    my_func()
end

println(x[])
# Computing... 100%|███████████████████████████████████████| Time: 0:00:05

```

The captured output may not be “clean” (I’m not sure what happens if outputs span multiple lines for example), so that needs to be handled somehow if it’s an issue.
