What's the well-liked GUI toolkit these days for OCaml

I’m looking to display PNG images in a window from OCaml code, and would like to put a little window around it with a button to close the window, maybe support resizing, stuff like that. I know about Lablgtk (and used it lo these many years ago). Is that still the well-liked thing?

I’m of half a mind to just call www-browser foo.png and let the local web browser deal with it, but that’d doesn’t work so well when you’re not on the same machine as the display, ah well.

3 Likes

I have very little experience in this domain with OCaml so take this answer with a grain of salt, but I would probably at least try using lablqml.

I know some developers have attempted to write a pure functional gui api in OCaml from scratch but I can’t recall what those projects are called. Someone else on this forum will hopefully dig it up for you.

Good luck and report back with which one you end up choosing!

(my guess is targeting the web browser is a lot easier than a native gui these days, unfortunately)

3 Likes

Ooooh! Ooooh! Shiny! I didn’t realize there was a Qt interface for OCaml! I used to use Qt with Perl back in the day, and boy howdy shiny! Sure, Gtk is fine and all, but honestly, I preferred Qt. At least, from Perl.

I’ll have a look (when I get to that point in my project). Thank you for the pointer!

2 Likes

if you allow me to advertise a library of mine, I think that bogue should be able to do what you want.

10 Likes

A native functionnal gui project is Revery, but its not very active if i remember well

1 Like

I’m pretty sure that raylib-ocaml by @tjammer can manage this.

5 Likes

Hi,

You could use OCaml-Canvas (documentation). It’s a new vector graphics library I’m developing for OCaml, and among its features, it can load PNGs and handle windows and mouse/keyboard events (using React). It works on Linux/macOS/Windows and also in web browsers (through HTML5 canvases, hence the similar interface).

It has not been “officially” released yet, but should be soon enough. Don’t hesitate to experiment with it !

Here’s how you can use it to load and display a PNG :

open OcamlCanvas.V1

let () =

  Backend.init ();

  let c = Canvas.createFramed "Image view" ~pos:(300, 200) ~size:(400, 400) in

  Canvas.show c;

  let e1 =
    React.E.map (fun img ->
        let size = ImageData.getSize img in
        Canvas.setSize c size;
        Canvas.putImageData c ~dpos:(0, 0) img ~spos:(0, 0) ~size
      ) (ImageData.createFromPNG "assets/image.png")
  in

  let e2 =
    React.E.map (fun _ ->
        Backend.stop ()
      ) Event.close
  in

  Backend.run (fun () -> ignore e1; ignore e2)
4 Likes

With so many good answers, I thought I’d ask about more functionality: This image is a visualization of a quantum circuit. It’s built out of a bunch of boxes-and-lines, using the Python matplotlib library. I’d like to generate such images from OCaml. Obviously, I could just call Python (and heck, just call Qiskit’s visualization library), but … I’m picky and want to do it without Python.

../../_images/tutorials_circuits_advanced_03_advanced_circuit_visualization_7_0.png

Is there a nice boxes-and-lines drawing package, that I might be able to use to draw such a thing? For reference, there’s a Latex package that uses a latex \xymatrix environment to draw passable circuits:

circuit

and I can generate the inputs to that already. So this is really a “stretch goal” grin.

You could try Vg (vg.Vg), it should have most of the primitives you’d need to compose such an image, and although it doesn’t seem to have builtin support for drawing aligned to a grid, there is a combinator to move a piece of an image, so should be possible to define something higher level that computes the coordinates needed for that. It can output SVG which will probably be useful for rendering the drawings on the web.

3 Likes

That library looks super cool and it’s very well documented

Maybe Mlpost could work for you ? See github and the paper (in french).

1 Like

it does not fully answer your question, but you might be interested in this post. The question was a bit similar, but with less sophisticated graphics. Of course, in principle you can use SDL primitives to obtain whatever you need, but it will require some work. In your case, since you don’t need animations or fancy stuff like this, I suppose it would be quite appropriate to use ocaml-cairo to produce your image. This can be integrated directly within bogue with bogue-cairo

1 Like

Coq uses lablgtk for CoqIDE. It does most of what we need, but not everything. We do run into bugs in GTK, which can be hard to work around;. Changing to something else seems like it would be a lot of work, and often you don’t find the bugs in the something else until you’ve done most of the work.

1 Like

I find that gtk is totally unusable on the Mac, unless I’m missing something big. Coqide was my preferred way of interaction with Coq until I started using the Mac, now I’m more or less restricted to Proof General.


Ian

People are using CoqIDE on mac. Perhaps you could submit a Coq issue or a request for help in the Coq forum? (I’m not on mac, so I can’t help you much myself.)