Gtk4DbDatasheet - POC Python library

Hi all. As promised quite a while back, I’ve been working on a “datasheet” class for Python. I finally have an initial version ready for review / comments:

Example usage:

        ds = Gtk4DbDatasheet.generator(
          , sql={
                "select": "*"
              , "from": "param"
          , box=box

Example screenshot ( viewing some metadata from my ETL framework ( GitHub - Ringerrr/Open_SDF: This Repository is for storing all the Open Source components of Smart Data Frameworks ):

Entire demo app ( minus the database, schema, data ):

import gi , psycopg2

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")

from gi.repository import Gtk , Adw
from Gtk4DbDatasheet import Gtk4DbDatasheet

class ExampleWindow(Gtk.ApplicationWindow):

    def __init__(self, app):
        super().__init__(application=app, title="Gtk4DbDatasheet Demo", default_width=300)

class ExampleApp(Adw.Application):

    def __init__(self):

        self.window = None

    def do_activate(self):

        if self.window is None:
            self.window = ExampleWindow(self)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        self.window.set_child( box )

        connection    = psycopg2.connect(
            user      = 'dankasak'
          , password  = 'dankasak'
          , host      = ''
          , port      = 5432
          , database  = 'SDF_baremetal_CONTROL'

        connection.set_session( autocommit = True )

        ds = Gtk4DbDatasheet.generator(
          , sql={
                "select": "*"
              , "from": "param"
          , box=box

app = ExampleApp()[])

Some caveats and things that don’t work:

  • I’m still learning Python - I have a Perl background, and this is a rough port of my Perl project: GitHub - dankasak/Gtk3-Ex-DBI: DB-connected Form and Datasheet objects - I may have done things in a not-very-python way, and I have no idea about things like packaging.
  • Dynamic column sizing based on percentages is currently not working: Do_size_allocate in gtk4
  • Documentation is non-existent, but the aim is to roughly work the same way as the Datasheet class in Gkt3::Ex::DBI.
  • CSS styling of Entry widgets to make the whole thing look more like an old-school liststore is not working. I have code in place, but I have little idea what I’m doing.
  • All data entry must be completed with an “Entry” keystroke. Using the TAB key or mouse to click outside the Entry will lead to data-loss. I would very much appreciate some assistance with this.

What works:

  • Inserting, updating, deleting records
  • Fetching the primary key inserted and populating the datasheet ( for auto-incrementing tables )
  • Proof-of-concept transformations for column types ( eg timestamps that need to be cast )
  • Currently works with MySQL, TiDB and Postgres. Some logic ported for Oracle, but I haven’t tested this at all yet. I aim to support all relational databases, as with the Perl project.

I’ve currently done minimal testing, and not used this in a production context. You would be very brave to do so :stuck_out_tongue: Having said that, it doesn’t done anything stupid on me yet.

The next part of the project will be to implement “Forms” functionality, which should be much easier. This will make use of a GtkBuilder object. I"m currently testing Cambalache for constructing GtkBuilder XML files.

Database people - please have a play and let me know what you think.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.