Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to hide the raw dict while printing dict as Tree #206

Closed
asbisen opened this issue Mar 8, 2023 · 2 comments
Closed

is it possible to hide the raw dict while printing dict as Tree #206

asbisen opened this issue Mar 8, 2023 · 2 comments

Comments

@asbisen
Copy link

asbisen commented Mar 8, 2023

Using the example

data = Dict(
    "a" => 1,
    "b" => Int64,
    "deep" => Dict(
            "x" => 1,
            "y" => :x
    ),
)

print(Tree(data))

The content is printed as

Dict("deep" => Dict("x" => 1, "y" => :x),
  "b" => Int64, "a" => 1)
  ├─ deep ⇒ Dict("x" => 1, "y" => :x)
  │         ├─ x ⇒ 1
  │         └─ y ⇒ x
  ├─ b ⇒ Int64
  └─ a ⇒ 1

Is it possible to just print the tree part hiding the raw output (something like this)

data
  ├─ deep ⇒ Dict()
  │         ├─ x ⇒ 1
  │         └─ y ⇒ x
  ├─ b ⇒ Int64
  └─ a ⇒ 1
@FedeClaudi
Copy link
Owner

FedeClaudi commented Mar 14, 2023

yep:

using Term
using Term.Trees

data = Dict(
    "a" => 1,
    "b" => Int64,
    "deep" => Dict(
            "x" => 1,
            "y" => :x
    ),
)

print(Tree(data; title="data"))
data                                 
  ├─ deep ⇒ Dict("x" => 1, "y" => :x)
  │         ├─ x ⇒ 1                 
  │         └─ y ⇒ x                 
  ├─ b ⇒ Int64                       
  └─ a ⇒ 1                           
          

That removes the first "raw" output replacing it with a title. For the inner deep ⇒ Dict() it will print the whole Dict there unfortunately. You can always pass your print_node function to Tree to achieve exactly what you want it to do though.

@FedeClaudi
Copy link
Owner

FedeClaudi commented Mar 14, 2023

For example:

function pn(io, node; kw...)
    if node isa AbstractDict
        print(io, string(typeof(node)))
    else
        print(io, node)
    end

end

print(Tree(data; title="data", printkeys=true, print_node_function=pn))
Dict                
  ├─ deep ⇒ Dict    
  │         ├─ x ⇒ 1
  │         └─ y ⇒ x
  ├─ b ⇒ Int64      
  └─ a ⇒ 1          

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants