Skip to content

Instantly share code, notes, and snippets.

@antholzer
Created April 5, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antholzer/3d6237b4c505d000017cbb93060c9ce3 to your computer and use it in GitHub Desktop.
Save antholzer/3d6237b4c505d000017cbb93060c9ce3 to your computer and use it in GitHub Desktop.
using Dash
using DashBootstrapComponents
using JSON3
using Plots; plotly()
function create_graph()
customdata = [JSON3.write(Dict(:x => rand())) for _=1:3]
plt = scatter([1,2,3], [1,2,3], hover=["a", "b", "c"], extra_kwargs=Dict(:series=>Dict(:customdata=>customdata)))
figure = (data=[fix_nan!(p) for p in Plots.plotly_series(plt)], layout=Plots.plotly_layout(plt))
return figure
end
# Dash uses JSON3 which does not allow NaN
function fix_nan!(d::Dict)
for (k, v) in d
if typeof(v) <: AbstractFloat && isnan(v)
d[k] = nothing
elseif typeof(v) <: Dict
fix_nan!(d[k])
end
end
return d
end
app = dash()
app.layout = html_div() do
html_h1("Hello Dash"),
dcc_graph(id="graph", figure=create_graph()),
html_pre(id = "click-info", children = "click.")
end
callback!(
app,
Output("click-info", "children"),
Input("graph", "clickData"),
) do click_data
if isnothing(click_data)
throw(PreventUpdate)
end
return string(click_data[:points][1][:customdata])
end
run_server(app, "0.0.0.0", 8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment