Skip to content

Instantly share code, notes, and snippets.

@aviks
Created November 6, 2019 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviks/7db8fa22e23f8d80f07bb77d5f040938 to your computer and use it in GitHub Desktop.
Save aviks/7db8fa22e23f8d80f07bb77d5f040938 to your computer and use it in GitHub Desktop.
Calculate Pi with Dash
using HTTP
using Dashboards
using PlotlyJS
function compute_pi(N::Int)
n_landed_in_circle = 0
for i = 1:N
x = rand() * 2 - 1
y = rand() * 2 - 1
r2 = x*x + y*y
if r2 < 1.0
n_landed_in_circle += 1
end
end
return n_landed_in_circle / N * 4.0
end
app = Dash("Pi App", external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]) do
html_div() do
html_h2("Calculate the Value of Pi"),
dcc_slider(id="num_iterations", min=10000, max=100000, step=1000, marks=Dict(Symbol(i)=>Symbol(i) for i in 10000:10000:100000), value=20000),
html_br(),
html_button("Pi!", id="submit_button", n_clicks=0),
html_div() do
html_span("Pi = "),
html_span("0.0", id="pi")
end,
dcc_graph(id="pi_graph")
end
end;
callback!(app, callid"{num_iterations.value} submit_button.n_clicks => pi.children, pi_graph.figure") do N, c
a = [compute_pi(n) for n in 1:1000:20000]
return a[end], Plot(a,
Layout(;xaxis_title = "Iterations",
yaxis_title = "Pi",
legend_x = 0,
legend_y = 1
);
x = collect(1:1000:10000),
)
end
h = make_handler(app, debug=true)
HTTP.serve(h, "127.0.0.1", 8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment