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

Update plot in loop #27

Open
KronosTheLate opened this issue Oct 5, 2023 · 4 comments
Open

Update plot in loop #27

KronosTheLate opened this issue Oct 5, 2023 · 4 comments

Comments

@KronosTheLate
Copy link

Is it currently possible to do something like I asked for in https://discourse.julialang.org/t/toggle-indefinite-loop-in-pluto-notebook/104495/? The function react has me hopeful, but it is not documented at all. When I try to use it, it says that it takes a variable number of argument with type any, but errored when I gave it two vectors.

@KronosTheLate
Copy link
Author

Or are you perhaps saying that running the code that originally drew the plot currently does a redraw internally, akin to Makie's observables? And that the following notebook redraws, as opposed to making a completely new plot each time?
image

@disberd
Copy link
Member

disberd commented Oct 5, 2023

The react function from PlotlyBase is only re-exported by PlutoPlotly but it's not used internally (in the current non-published version it is also forwarded from PlutoPlot objects to its internal Plot object but just for completeness of API).

In the README of this package there is a mention to the Plotly.react function which is the Javascript functions that is used internally by the package to show the plot in Pluto.
You can see more details on the javascript function here https://plotly.com/javascript/plotlyjs-function-reference/#plotlyreact but indeed it just updates an existing plot if the provided HTML node is a plotly plot.
This saves some time on the JS side as opposed to recreating the full plot from scratch.

This together with the utilization of publish_to_js/published_to_js which speeds up data transfer between julia and pluto, allows to get plots that update more rapidly upon cell re-run compared to the ones normally obtained with PlotlyBase.

To come back to your example above, in PlutoPlotly you can achieve what I think you want with the following code.

# ╔═╡ c22b0535-cb6c-4264-b27c-13361fa06b87
@bind toggle Clock(1e-2)

# ╔═╡ 5f28ca6d-f3df-451e-8953-28ab22b853c3
let
	toggle
	phase = 5time()
	x = range(0,2π, 100)
	y = sin.(x .+ phase)
	plot(scatter(;y,x))
end
db5eea84-ce5a-4b54-9996-515683ad9f16.mp4

That being said I do agree that we need more documentation in this package.
I am currently going through a substantial rewrite and also wanted to include some documentation but I only do this in my free time and that is not in very high availability in the past weeks :P

@disberd
Copy link
Member

disberd commented Oct 5, 2023

On the note of updating an existing plot from another cell, while this is in theory possible with PlutoPlotly (and will give you even more snappyness compared to the one in the example above), it requires going the Javascript route (meaning writing functions that create and return javascript code, either with Base.HTML or @htl), which I guess is not what you want.
I had some ideas on how to provide some sort of API for this but it's not trivial so I didn't go down that road yet.

Consider that I believe there is also some throttling/overhead with the clock function (I don't think it was designed for very short periods) so you can't too fast by relying on that.

For much better capabilities of updating plots on Pluto in the backround we would need something like fonsp/Pluto.jl#2392 to be finished and merged.
I am planning to start drafting a PR for Pluto to provide an API that allows triggering recomputation/updates of the plot from the javascript side (so that the cell output/plot itselfs tells pluto/julia that it has to compute some new values and send them back to the front-end)

@KronosTheLate
Copy link
Author

That sounds awesome! Using Pluto and reactive plots allows some incredible things in terms of using a headless raspberry pi for streaming data collection and visualization, and I am super-psyced about the prospect. This package appears to be the best effort, and it is already very capable. Thank you for it.

The documentation was a bit hard to find my way through, so I am very glad to hear that you are in the process of a rewrite. For now, I think I will settle with the reactiveness seen in the example we provided, as it is already really impressive. I am not at all capable of contributing in this space, but again thank you for your efforts. I think it allows some really really cool stuff, implemented in very little time by people like me with very little (no) knowledge about Javascript and HTML. Thanks.

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