owebview

Owebview

An embedded web rendering engine.


Powered by webview. All the power of web technologies in your app. No Electron, no bundler: create a window, drop in some HTML, and you have an app.

Examples

Rainfall (D3.js) Geometry shapes (three.js) Timer
Rainfall chart 3D shapes Timer
dune exec examples/d3/d3.exe dune exec examples/three/three.exe dune exec examples/timer/timer_posix.exe

A complete app demonstrating Owebview feature : Sun notes. Packages for Debian, Fedora, macOS, Windows available for download.

Learn about Owebview

Please refer to the tutorial

You can also check the documentation

Minimal code of random number GUI

Build a tiny native desktop window with a web UI, straight from OCaml —

Here's the whole thing:

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.bind w "random" (fun id req ->
      Printf.printf "binding called <random>: id=%s req=%s\n%!" id req;
      let result = string_of_int (Random.int 100)
      in
      Webview.return w id ~error:false ~result);

  Webview.set_html w
    {|<!doctype html>
      <html>
        <body style="font-family: system-ui; text-align: center">
          <h1>Hello from OCaml 👋</h1>
          <p>Click the button to get a random number:</p>
          <button id="btn">Get random number</button>
          <p id="result"></p>
          <script>
            const btn = document.getElementById("btn");
            const result = document.getElementById("result");
            btn.addEventListener("click", () => {
              window.random().then((n) => {
                result.textContent = `Random number: ${n}`;
              });
            });
          </script>
        </body>
      </html>|};
    
  Webview.run w;
  Webview.destroy w

Once HTML rendering works, the fun part is the OCaml ↔ JavaScript bridge: Webview.bind w random (...) expose window.random() to the page; the result is a JS Promise. All the computation of random numbers is done by the OCaml back-end. While random number generation can be done in JavaScript, it gives an example of how to call native functions from the window.

Install and use in your own project

If your machine is on Windows, you must install the Microsoft Webview2 package before any OPAM installation, :

nuget install Microsoft.Web.WebView2

Otherwise, on unix system, all the dependancies are installed via OPAM.

opam install owebview

Then depend on it from your dune file:

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

The Owebview package is divided in two seperate libraries :

See the examples

Clone the repo and launch one of the bundled example, for instance hellowv:

git clone https://github.com/korkorran/Owebview.git
cd Owebview
nuget install Microsoft.Web.WebView2 # on windows only
opam install . --deps-only
dune build
dune exec examples/hellowv/hellowv.exe          # a basic application
dune exec examples/timer/timer_posix.exe        # a timer with threads
dune exec examples/timer/timer_lwt.exe          # the same, driven by Lwt
dune exec examples/d3/d3.exe                    # a D3.js chart
dune exec examples/three/three.exe              # WebGL via three.js

opam install brr                                # you will need brr to compile this example
dune build
dune exec examples/hellowv_ml/hellowv.exe      # full OCaml application

You can see the full list in the examples/ folder.

Native dev dependencies

webview uses the system web engine, so you need its native libraries:

The platform-specific compile/link flags are detected automatically at build time — via pkg-config on Linux, and from the NuGet cache on Windows — so there's nothing to tweak by hand.

Contributing

Feedback is very welcome! Reports about building and running it on Linux distributions are especially valuable.

If you give it a try on Linux, please open an issue with your distribution, what worked and what didn't (build logs welcome). Pull requests improving cross-platform support are happily accepted.

License

MIT.