Subtree structure for use by the lazy fetching of data through the D3Trees server. Offset so that they can be easily entered into the children structure of D3Tree.

D3Tree(children, <keyword arguments>)

Construct a tree to be displayed using D3 in a browser or ipython notebook, specifying structure with lists of children indices.



  • children::Vector{Vector{Int}}: List of children for each node. E.g.
    D3Tree([[2,3], [], [4], []])
    creates a tree with four nodes. Nodes 2 and 3 are children of node 1, and node 4 is the only child of node 3. Nodes 2 and 4 are childless.


  • text::Vector{String} - text to appear under each node.
  • tooltip::Vector{String} - text to appear when hovering over each node.
  • style::Vector{String} - html style for each node.
  • link_style::Vector{String} - html style for each link.
  • title::String - html title.
  • init_expand::Integer - levels to expand initially.
  • init_duration::Number - duration of the initial animation in ms.
  • svg_height::Number - height of the svg containing the tree in px.
D3Tree(node; detect_repeat=true, lazy_expand_after_depth=typemax(Int), kwargs...)

Construct a tree to be displayed using D3 in a browser or ipython notebook with any object, node, that implements the AbstractTrees interface.

The style may be controlled by implementing the following functions, which should return Strings for the nodes:


Allows for lazy loading of large trees through the lazy_expand_after_depth keyword argument. Nodes beyonnd this depth are not intially expanded for visualization. Instead, they are cached and only expanded when requested by the visualization. The serving is done by the D3Trees HTTP server.

The server does not have information about the lifetime of different visualizations so it might keep references to past visualizations, potentially holding up a lot of memory. To reset the server and remove references to old data, run either D3Trees.reset_server() or D3Trees.shutdown_server().



  • node: an object that has AbstractTrees.children(node) and AbstractTrees.printnode(io::IO, node)


  • detect_repeat: (default: true) if true, uses a dictionary to detect whether a node has appeared previously
  • lazy_expand_after_depth::Integer: (default: typemax(Int)). Sets tree depth to at which AbstractTrees.children(node) will not be called anymore. Instead, nodes at this depth are cached and children(node) is called only when node is clicked in the D3 interactive visualization. Root has depth 0, so setting lazy_expand_after_depth=1 expands only the root.
  • lazy_subtree_depth::Integer: (default: 2) sets depth of subtrees fetched from D3Trees server
  • port::Integer: (default: 16370) specify server port for D3Trees server that will serve subtrees for visualization. Shutdown server by shutdown_server(port).
  • dry_run_lazy_vizualization::Function: (default: t -> D3Trees.dryrunserver(port, t)) function that is ran once before visualization is started to speed up first fetch in the visualization. Provide custom function if your tree's children method takes a long time on first run.
  • Also supports, the non-vector arguments of the vector-of-vectors D3Tree constructor, i.e. title, init_expand, init_duration, svg_height.

cors_middleware: handles preflight request with the OPTIONS flag If a request was recieved with the correct headers, then a response will be sent back with a 200 code, if the correct headers were not specified in the request, then a CORS error will be recieved on the client side Since each request passes throught the CORS Handler, then if the request is not a preflight request, it will simply go to the rest of the layers to be passed to the correct service function.


Runs api request against a simple tree. When ran before rendering first visualization, it leads to faster visualization responses on first click. This helps prevent errors caused by trying to fetching the same resource twice.


Calculate missing children, for use with the D3Trees server for lazyly fetching data.

inbrowser(t::D3Tree, command::Cmd)

Open an html document with the D3Tree in a program launched with the specified command.

inbrowser(t::D3Tree, browsername::String)

Open an html document with the D3Tree in a browser with a platform-specific command.


Return the html style for the link leading to the D3Trees node corresponding to AbstractTrees node n


logging_middleware logs the request before passing it on to the tree router and request handler and then logs the returned response.

serror(error::Exception) -> String

Get error and stacktrace as String, e.g. for use in warning. Useful in Jypyter notebooks where error messages are not displayed correctly (https://github.com/JuliaLang/IJulia.jl/issues/1043)


julia> try
       catch e
           @warn "Oh no, exception:
┌ Warning: Oh no, exception:
│  UndefVarError: b not defined
│ 13-element Vector{Base.StackTraces.StackFrame}:
│  top-level scope at REPL[7]:2
│  eval at boot.jl:373 [inlined]
│  eval_user_input(ast::Any, backend::REPL.REPLBackend) at REPL.jl:150
│  repl_backend_loop(backend::REPL.REPLBackend) at REPL.jl:246
│  start_repl_backend(backend::REPL.REPLBackend, consumer::Any) at REPL.jl:231
│  run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool) at REPL.jl:364
│  run_repl(repl::REPL.AbstractREPL, consumer::Any) at REPL.jl:351
│  (::Base.var"#930#932"{Bool, Bool, Bool})(REPL::Module) at client.jl:394
│  #invokelatest#2 at essentials.jl:716 [inlined]
│  invokelatest at essentials.jl:714 [inlined]
│  run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool) at client.jl:379
│  exec_options(opts::Base.JLOptions) at client.jl:309
│  _start() at client.jl:495
└ @ Main REPL[7]:4
Start serving tree from existing or new server on some port. Returns port for the server.

Shutdown D3Trees server (all servers if port is not specified) and remove associated tree data.


Return the html style for the D3Trees node corresponding to AbstractTrees node n


Return the text to be displayed at the D3Trees node corresponding to AbstractTrees node n


Return the text to be displayed in the tooltip for the D3Trees node corresponding to AbstractTrees node n


Convert datastructure to json, shift indeces to zero-indexing for use in javascript


Convert datastructure to json, shift indeces to zero-indexing for use in javascript