Owebview

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.

Two libraries

The owebview package installs two libraries, and the split is deliberate.

Depend on the first alone for a window whose content is an inline HTML string; add the second as soon as the page comes from files on disk:

  (executable
   (name main)
   (libraries owebview owebview.desktop))

Quickstart

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 w

Call 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)

Load a real page, with its CSS and images, from a directory next to the executable:

  let index =
    Filename.concat (Webview_desktop.Locate_assets.web_dir ()) "index.html"
  in
  Webview.navigate w ("file://" ^ index)

A step-by-step tutorial builds this up from a first window to a complete application.

owebview: the webview binding

The whole binding is the Webview module. A tour of it:

owebview.desktop: native desktop integration

Four independent modules under Webview_desktop:

Each module's own page documents what differs between platforms, which is where the real detail lives: macOS runs the menu bar and the Dock icon application-globally, Linux needs a .desktop file under Wayland, and GTK 4 supports neither the dialogs nor the menu bar.

Native dev dependencies

webview uses the system web engine, so building against it needs that engine's development files. Only Windows asks anything of you:

owebview.desktop needs strictly less — the platform toolkit without the web engine — so it adds nothing to install. The platform-specific compile and link flags of both libraries are detected automatically at build time, and there is nothing to tweak by hand.