Hi there,
I’d like to share with you Webviews.jl
, a Julia implementation of webview for creating web-based desktop GUIs. Hope you find it interesting and useful.
Its usage is very simple:
# Julia v1.8+ on Windows/macOS or v1.9+ on Linux
using Webviews
w = Webview(320, 320, debug=true)
navigate!(w, "https://julialang.org")
run(w)
A new window with a given size will be created and a Webview
is attached/embedded in it.
debug=true
enables the dev tools, navigate!
sets it to view Julia’s homepage and run(w)
starts the GUI event loop. @async
is used here so that it won’t block our REPL.
And then the window can be used like a normal web browser.
You can also directly give the Webview
HTML content to show with html!
:
# Since `run(w)` automatically destroys the previous one,
# a new webview instance should be created.
w = Webview()
html!(w, """<html><body>
<h1>Hello from Julia v$VERSION</h1>
<div id="data"></div>
</body></html>""")
Webviews.jl
provides bind(func, webview, name)
to bind a Julia function to a name with which you can invoke using JavaScript in the Webview
.
bind(w, "add") do (a, b)
a + b
end
JavaScript code can be executed with eval!
eval!(w, raw"""(async () => {
const result = await add(1, 2)
const div = document.getElementById("data")
div.textContent = `result = ${result}`
})()""")
# You will see `result = 3` in the window.
Note that bound functions must take a Tuple
as its only argument, and they are async functions (i.e., returns Promise
s) in JS.
Another common use case is to start an HTTP server as the backend to provide content, and simply call navigate!(w, "127.0.0.1:8000")
if the HTTP server is listening on the 8000 port.
More examples can be found here.
It’s also worth mentioning that Webviews.jl
directly uses libraries provided by the operating system, instead of JLL packages:
- Latest Windows installs
WebView2
by default. - macOS provides
WebKit
. - Most Linux distributions have a simple way to install
WebKitGTK
. (Yes, you have to install it manually, sorry.)
This way is intended since these libraries are native and efficient and Webviews.jl
is just a GUI library that will probably not mess up with the backend program. Compared to other bindings to webview, it has the advantages of Julia’s dynamic features as well as its high performance.
PS: I kinda wanted to also develop a web-based GUI framework like tauri
does for Rust, but it involves reinventing tools like those for windows creation and management on different platforms… So I haven’t started working on that.
Any feedback and comments are very welcome!
Best,
Sunoru