A thin OCaml binding to webview: open a native window backed by the operating system's web engine, load HTML/CSS/JS, and bridge JavaScript with OCaml — no Electron, no bundler.
A whole application is about ten lines:
let () =
let w = Webview.create () in
Webview.set_title w "My first owebview app";
Webview.set_size w ~width:480 ~height:320 Webview.Hint_none;
Webview.set_html w "<h1>Hello from OCaml</h1>";
Webview.run w;
Webview.destroy wCall OCaml from the page and answer the JavaScript promise:
Webview.bind w "add" (fun id req ->
let result =
match Scanf.sscanf_opt req "[%d,%d]" (fun a b -> a + b) with
| Some n -> string_of_int n
| None -> "null"
in
Webview.return w id ~error:false ~result)The whole binding is the Webview module. A tour of it:
Webview.create, Webview.run, Webview.terminate, Webview.destroyWebview.navigate, Webview.set_html, Webview.evalWebview.bind, Webview.unbind, Webview.returnWebview.dispatchWebview.get_native_handle, Webview.set_app_iconWebview.Utils.web_dirwebview uses the system web engine, so its native libraries are needed:
gtk+-3.0 and webkit2gtk-4.1 (-dev packages).The platform-specific compile and link flags are detected automatically at build time, so there is nothing to tweak by hand.