# How to use Malt.jl and IOCapture.jl for evaluating expressions

**URL:** <https://discourse.julialang.org/t/how-to-use-malt-jl-and-iocapture-jl-for-evaluating-expressions/121045>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 8, 2024, 4:56am UTC](https://discourse.julialang.org/t/how-to-use-malt-jl-and-iocapture-jl-for-evaluating-expressions/121045 "2024-10-08T04:56:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rene\_David\_Hernandez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rene_david_hernandez/32/212491_2.png) [@Rene\_David\_Hernandez](https://discourse.julialang.org/u/Rene_David_Hernandez)\
**Post date:** [October 8, 2024, 4:56am UTC](https://discourse.julialang.org/t/how-to-use-malt-jl-and-iocapture-jl-for-evaluating-expressions/121045/1 "2024-10-08T04:56:39Z")

</div>

I would like to use Malt.jl to evaluate expressions in a workspace separate from the main thread running Julia. So far I have not been able to find a way to use Malt.jl and capture with IOCapture the different outputs of the code running in a Malt worker (output values, console outputs and errors).  
For Example

```julia
import Malt
worker = Malt.Worker.();
expr = :(print("Hi");1+1;)
Malt.remote_eval_fetch(worker,:($expr)) 

```

How to capture from the last line of code what is painted on the console and the result of the operation. Also, if it is a `plot` that is attempted to be executed, how to capture the output of the `plot`?

---

<div class="post-metadata">

**Author:** ![littlelib](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/littlelib/32/209324_2.png) [@littlelib](https://discourse.julialang.org/u/littlelib)\
**Post date:** [October 9, 2024, 5:31am UTC](https://discourse.julialang.org/t/how-to-use-malt-jl-and-iocapture-jl-for-evaluating-expressions/121045/3 "2024-10-09T05:31:25Z")

</div>

I suppose this will do?

```julia
using IOCapture, Malt, Plots
worker=Malt.Worker()
Malt.remote_eval(worker, :(using IOCapture, Plots))
results=Malt.remote_eval_feth(worker, quote
    IOCapture.capture() do 
        println("Creating plot")
        return scatter([1,2,3], [4,5,6])
    end
end)

julia> results.output
"Begin creating plot\n"

julia> results.value
# returns the plot

```

Your code is likely to be long, so it’d be better to use the quote block.  
Wrap your code inside `IOCapture.capture` and make sure you return the plot in the end, and I’m pretty sure you’ll get what you want.

---

<div class="post-metadata">

**Author:** ![Rene\_David\_Hernandez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rene_david_hernandez/32/212491_2.png) [@Rene\_David\_Hernandez](https://discourse.julialang.org/u/Rene_David_Hernandez)\
**Post date:** [October 9, 2024, 6:42am UTC](https://discourse.julialang.org/t/how-to-use-malt-jl-and-iocapture-jl-for-evaluating-expressions/121045/4 "2024-10-09T06:42:40Z")

</div>

Thank you so much, works well.
