# Jupyter notebook capture stdout and stderr like python capture magic?

**URL:** <https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253>\
**Category:** New to Julia\
**Created:** [July 11, 2019, 1:39pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253 "2019-07-11T13:39:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 1:39pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/1 "2019-07-11T13:39:59Z")

</div>

Hi,  
in Jupyter one can use the python %%capture magic to redirect stdout, stderr into a variable. How do you do that in Julia with Juypter? I, most of the times, only have a pure Julia kernel running.

Maybe as a bonus: Is there a way to “tie” the output to a specific cell as well? I want to run asynchronous tasks and if I switch cells, they start to write into the standard output of other cells, which is somehow annoying 😃

---

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [July 11, 2019, 3:03pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/2 "2019-07-11T15:03:37Z")

</div>

Julia has built-in `redirect_stdout` /`redirect_stderr`. Do they suit your needs?

[https://docs.julialang.org/en/v1/base/io-network/index.html#Base.redirect\_stdout](https://docs.julialang.org/en/v1/base/io-network/index.html#Base.redirect_stdout)

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 11, 2019, 3:40pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/3 "2019-07-11T15:40:31Z")

</div>

I do not know how they work together with jupyter. I do not want to redirect ALL output, just the output of some code running in specific cells in a jupyter notebook.  
Does anyone know, how that would be done?

---

<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:** [July 11, 2019, 4:20pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/4 "2019-07-11T16:20:32Z")

</div>

> [@jamblejoe](#):
>
> I do not want to redirect ALL output, just the output of some code running in specific cells in a jupyter notebook.

```julia
let old_stdout = stdout
    rd, = redirect_stdout()
    try
        println("This output is redirected.\n")
    finally
        redirect_stdout(old_stdout) # restore original stdout
    end
    output = String(readavailable(rd))
    println("captured the output: ", output)
end

```

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 12, 2019, 11:59am UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/5 "2019-07-12T11:59:20Z")

</div>

This looks promising! I will give this a try as soon as I can!

---

<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:** [July 12, 2019, 4:24pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/6 "2019-07-12T16:24:37Z")

</div>

It’s harder than it should be, though. We should really have a `capture_stdout()` function in the standard library, so that you can do:

```julia
data = capture_stdout() do
    ...
end
String(data)

```

or similar. Or maybe

```julia
buf = IOBuffer()
redirect_stdout(buf) do
    ...
end
String(take!(buf))

```

See

> <https://github.com/JuliaLang/julia/issues/32567>
>
> Currently it's \[rather awkward to capture stdout as a string\](https://discourse.…julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253). This would be a lot easier if \`redirect\_stdout\` supported an \`IOBuffer\` stream argument (currently it only supports \`Union{IOStream, LibuvStream}\`), so that you could do e.g.:
> \`\`\`jl
> buf = IOBuffer()
> redirect\_stdout(buf) do
> ...
> end
> String(take!(buf))
> \`\`\`
> Possible implementation:
> \`\`\`jl
> function redirect\_stdout(f::Function, buf::GenericIOBuffer)
> old\_stdout = stdout
> try
> rd, = redirect\_stdout()
> ret = f()
> Libc.flush\_cstdio()
> flush(stdout)
> write(buf, readavailable(rd))
> return ret
> finally
> redirect\_stdout(old\_stdout)
> end
> end
> \`\`\`

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [July 12, 2019, 4:26pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/7 "2019-07-12T16:26:43Z")

</div>

That would be very nice! Especially if you want to hack something quickly together in a notebook 🙂

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 12, 2019, 5:15pm UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/8 "2019-07-12T17:15:35Z")

</div>

[https://github.com/JuliaIO/Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl) has an `@capture_out` macro, but in a Jupyter notebook cell it appeared to freeze forever for me with Julia 1.1.1,

---

<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:** [July 13, 2019, 2:39am UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/9 "2019-07-13T02:39:27Z")

</div>

> [@tkoolen](#):
>
> [https://github.com/JuliaIO/Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl) has a `@capture_out` macro, but in a Jupyter notebook cell it appeared to freeze forever for me with Julia 1.1.1,

Can you file an issue with Suppressor.jl?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 13, 2019, 4:06am UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/10 "2019-07-13T04:06:42Z")

</div>

> [@stevengj](#):
>
> a `@capture_out`

I see you don’t pronounce the ‘at’ 🙂. I opened [@capture\_out hangs in Jupyter notebooks · Issue #31 · JuliaIO/Suppressor.jl · GitHub](https://github.com/JuliaIO/Suppressor.jl/issues/31).

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 13, 2019, 5:02am UTC](https://discourse.julialang.org/t/jupyter-notebook-capture-stdout-and-stderr-like-python-capture-magic/26253/11 "2019-07-13T05:02:04Z")

</div>

See also [More convenient redirect\_\* methods · Issue #28679 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/28679).
