Hi,
Yesterday, I posted a link to a new package
StringInterpolation. That was a warmup for this one :)
JuliaJS started life as
PlotlyJS providing an interface from Julia to the recently open-sourced
plotly.js. By the time I finished PlotlyJS, there wasn't really much "Plotly"-specific about it and I changed the name to make way for Spencer's cool
PlotlyJS package (which I think these two packages can work happily together btw).
JuliaJS is HEAVILY inspired by the awesome
Blink package, but there is no dependence on Atom/Electron.
One thing I like about JuliaJS, other than being able to use string interpolation with JS commands, is the ease at which the two-way communication works. For example, you can define a callback in Julia like so:
callback["Run My Code Please"] = begin
println("Ok! I will run your code.")
# Your code here.
end
Then, on the JavaScript side:
JuliaJS.message("Run My Code Please")
and guess what it does? :)
I'm still learning Julia so I'm sure there are plenty of bugs and a ton of ways to do things better, so feedback, issues, PRs are most welcome.
From the README:
Warning: This package is still very early in development. Feedback, issues and PRs are most welcome.
JuliaJS.jl
This is simple package that allows Julia to interact with JavaScript in a browser.
Installation
JuliaJS is not registered and it requires another unregistered package: StringInterpolation. To install, you'll need to run the following commands:
Usage Example:
After installation, running the following from the Julia REPL
julia> using JuliaJS
Listening on 0.0.0.0:8000...
will start an HttpServer. Open your browser to
http://localhost:8000/julia and you should see a blank page with "JuliaJS" in the browser tab.
With the browser open, run the following:
julia> include(Pkg.dir("JuliaJS","examples","plotly.jl"))
If the stars are aligned, you should see several sample charts appear in the browser window.
Notes:
Interpolation
The non-standard string literal `js` supports interpolation, but at the moment, the Julia expression needs to be enclosed in parentheses, e.g. instead of
julia> msg = "Hello World"
julia> js"""
console.log("$msg")
"""
you will need to enclose `msg` in parentheses as illustrated below:
julia> msg = "Hello World"
julia> js"""
console.log("$(msg)")
"""
WebSockets
JuliaJS creates an active link between Julia and your browser via WebSockets so you can update your charts from Julia without reloading the page. To see this, try rerunning the above example several times:
julia> include(Pkg.dir("JuliaJS","examples","plotly.jl"))
julia> include(Pkg.dir("JuliaJS","examples","plotly.jl"))
julia> include(Pkg.dir("JuliaJS","examples","plotly.jl"))
julia> include(Pkg.dir("JuliaJS","examples","plotly.jl"))
Each time you run from the REPL, the charts are updated without having to reload.
A fun artifact of using WebSockets is that you can "broadcast" your charts to several browsers and they will all get updated interactively. Here is a
silly video demonstrating three browsers (including an iPhone) with charts being interactively controlled from the Julia REPL.