goki / gipy

gipy is a Python interface to the GoGi GUI framework
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Python wrapping of GoGi

You can now run most of GoGi via Python, using a newly-updated version of the gopy tool that automatically creates Python bindings for Go packages.

Go incorporates many features found in Python, and provides a really natural "backend" language for computationally-intensive functionality such as a GUI. Python provides a nice interactive "scripting" level interface for dynamically building GUI's, and can bring Go code to a much wider audience. Thus, this represents an ideal combination of the two languages. And you can build the entire stack, from the raw Go code in GoGi to the Python bindings (which unfortunately are a bit slow because they rely on a C compiler..), in a tiny fraction of the time it takes to build something like Qt and the PySide or PyQt bindings.

Installation

Python version 3 (3.6-3.9 have been well tested) is recommended, and the instructions assume that version (you can probably get version 2 to work but it has not been tested). Also pip must be installed, as is typical. This assumes you have already installed GoGi per the Wiki Install instructions, including installing Go itself, and adding ~/go/bin to your PATH. be double-sure that goki/examples/widgets runs properly per wiki install before proceeding -- if that doesn't work, nothing else will.

On linux, you must ensure that the linker ld will look in the current directory for library files -- add this to your .bashrc file (and source that file after editing, or enter command locally):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

This assumes that you are using go modules, as discussed in the wiki install page, and that you are in the gi directory where you installed gi (e.g., git clone https://goki.dev/goki/gi and then cd gi)

NOTE: only a modules version of the build is supported, but it has some weirdness about updating the go.mod and go.sum files across the gopy and rest of the build. Pay attention to the output of the make command and if anything is downloaded during the gopy part of the build, it is a good idea to interrupt the go build process (hit Ctrl-C) and start over again, until there are no downloading messages -- then everything should be included properly.

Do NOT do a go mod tidy (which is normally the right thing to do to fix up your go.mod file and download packages) because the context in which that needs to be done involves the files generated by gopy (in the gi subdir -- safe to do there after gopy runs), not what is present in the python directory where you type make.

$ cd python  # should be in gi/python now -- i.e., the dir where this README.md is..
$ make  # if you get an error about not finding gopy, make sure ~/go/bin is on your path
$ make install  # may need to do: sudo make install -- installs into /usr/local/bin and python site-packages
$ cd ../examples/widgets
$ ./widgets.py        # start using #! comment magic code

Alternatively, you can run things more manually in the widgets directory:

$ pygi   # this was installed during make install into /usr/local/bin
$ import widgets  # this loads and runs widgets.py -- view that and compare with widgets.go
$ pygi -i widgets.py  # another way to start it

If you get something like this error:

dyld: Library not loaded: @rpath/libpython3.6m.dylib

then you need to make sure that this lib is on your LD_LIBRARY_PATH -- on mac you can do otool -L /usr/local/bin/pygi and on linux it is ldd /usr/local/bin/pygi -- that should show you where it is trying to find that library.

If pkg-config or some other misc command is not found, you can use brew install to install it using homebrew on mac, or your package manager on linux.

Windows

As usual, Microsoft Windows is different -- here's some tips for configuring the client to work on windows.

How it works

from gi import go, gi, giv, units, ki, gimain
if isinstance(ojb, go.GoClass):
def strdlgcb(recv, send, sig, data):
    dlg = gi.Dialog(handle=send) # send is a raw int64 handle -- use it to initialize
    # a python wrapper class of type gi.Dialog -- note: you must get these types
    # right and crashes are likely if you don't!