Update `table` option for plugins

Current

The current table option for plugins is completely undocumented, and from my experience trying to implement one, very unintuitive. Here is a quick breakdown of how they work currently:

[TABLE]
label = "Test" # Same as every other item
type = "table"
toolTip = "This is a table" # Same as every other item
tab = "Basic" # Same as every other item
# Toggle ability to select entire rows. No support to select columns.
selectable = false
# The number of columns will grow to support however many headers are here
headers = ["header1", "header2", "header3"]
# Pass in defaults column-wise, also resizes table to fit all values
columns = [
    ["row0_col0", "row1_col0"],
    ["row0_col1", "row1_col1"],
    ["row0_col2", "row1_col2"],
]
# Alternatively, pass in defaults row-wise.
# If this is provided with columns, any overlap will be overwritten by rows
# This also will resize the table if necessary
rows = [
    ["row0_col0", "row0_col1", "row0_col2"],
    ["row1_col0", "row1_col1", "row1_col2"],
]
# This overrides both rows and columns, and will resize the table if necessary
default = "row0_col0\trow0_col1\trow0_col2\nrow1_col0\trow1_col1\trow1_col2"
# Delimiter will change the format of "default"
delimiter = "\t"
# Sortable does something, but I could not figure out its function
sortable = False

The plugin then receives (in this case) a string that represents tab-separated values in the same form as default:

{
    "TABLE": "row0_col0\trow0_col1\trow0_col2\nrow1_col0\trow1_col1\trow1_col2"
}

Proposed Changes

Some discussion with @matterhorn103 led to this proposed revision.

A more intuitive way to create a table would be to pass in an array of rows/columns, which could each contain information about what that entry in the table would look like, things like the header/label, the datatype, the defaults, and whether or not that row/column is editable. Additionally, passing both rows and columns would not be allowed, and passing default would be outright removed. Additionally, within a row/column, the datatype would be homogeneous.

This is what that would look like in TOML:

[TABLE]
label = "Test"
type = "table"
toolTip = "This is a table"
tab = "Basic"

[[TABLE.columns]]
label = "col1"
type = "integer"
minimum = 0
maximum = 9
defaults = [2, 5]

[[TABLE.columns]]
label = "col2"
type = "string"
defaults = ["row0_col1", "row1_col1"]

[[TABLE.columns]]
label = "col3"
type = "boolean"
editable = false
defaults = [true, false]

This allows for a number of important things. One, you can now control the widget inside the box (QTableWidgetItem for strings, QSpinBox and QDoubleSpinBox for integers and floats, respectively, and QCheckBox for bools), just like you would in other widgets. Being able to set a row/column as editable also would serve to enable a sort of row/column label if you have already labeled the other. This is useful for situations where the plugin sets a value based on the molecule passed, and the others need to be filled in by a user.

The table produced from this TOML is shown here:

In addition to updating the table specification, the return value from the table would also be updated to match the input more closely. This means that instead of return a string, Avogadro would return a JSON array with the rows/columns inside it, like this:

{
  "TABLE": [
    {"col1": [2, 5]},
    {"col2": ["row1_col2", "row2_col2"]},
    {"col3": [false, false]}
  ]
}

This would make it much more intuitive to work with since the column labels you put in are exactly what you get back out, and the types match what you expect.

While this would break existing plugins using tables, I don’t actually think there are many plugins that use tables, since their API was quite complicated and completely undocumented, as mentioned at the beginning of this post.

I’ve already done the implementation of this in a branch, just to see if it is possible. If there is positive feedback, I’ll open a PR.

1 Like

In my opinion the most valuable thing is for the input and output formats to match. So in this version of the proposal where columns is an array (of dictionaries), I’d say the output should match it and be an array (of arrays).

Apologies that I didn’t write docs on tables (which were added in 2.1) - I hacked the feature for the use case of “log into a WebMO instance and get a list of finished jobs for the user to select and download.”

The use case was more “display a bunch of tabular data” than what you’re describing, which seems to be “provide an editable table.”

No, I’m not currently aware of anything using tables, since I haven’t released the avogadro-webmo plugin yet. I think what you’re doing would be fine - although it means anything table-related has to wait until 2.2 because that would be the first release supporting your implementation.

1 Like

We did have an earlier sketch where columns were specified in a dict keyed on an identifier (which was therefore unique) and then also passed to the plugin as a dict of arrays. This was really nice in our view, but then we remembered that dicts/hashmaps don’t maintain order outside of Python.

Having thought about it a little more, we could still do it, the order of columns would just have to be provided. One way of doing it would be like this:

[TABLE]
label = "Test"
type = "table"
toolTip = "This is a table"
tab = "Basic"
column-order = ["col1", "col2", "col3"] # Optional, otherwise order is arbitrary

[TABLE.columns.col1]
label = "Column 1"
type = "integer"
minimum = 0
maximum = 9
defaults = [2, 5]

[TABLE.columns.col2]
label = "Column 2"
type = "string"
defaults = ["row0_col1", "row1_col1"]

[TABLE.columns.col3]
type = "boolean"
editable = false
defaults = [true, false]

and then the plugin would receive the values like:


{
  "TABLE": {
    "col1": [2, 5],
    "col2": ["row1_col2", "row2_col2"],
    "col3": [false, false],
 }
}

There are alternative ways to do this e.g. have the column as key/value pairs within TABLE rather than TABLE.columns, or to have each column specify its own position/index within its dict, but this is just one example.

My argument in favour of this would be that I do think it makes life easier for plugin authors when they can get stuff by key rather than index as much as possible. Indices are faffy and very prone to errors/bugs.

I do like the idea of providing order explicitly, but I don’t like the idea of repeating column labels several times. I’d suggest instead that we pass the column order inside each column:

[TABLE]
label = "Test"
type = "table"
toolTip = "This is a table"
tab = "Basic"

[TABLE.columns.col1]
label = "Column 1"
order = 0
type = "integer"
minimum = 0
maximum = 9
defaults = [2, 5]

[TABLE.columns.col2]
label = "Column 2"
order = 1
type = "string"
defaults = ["row0_col1", "row1_col1"]

[TABLE.columns.col3]
order = 2
type = "boolean"
editable = false
defaults = [true, false]

Wouldn’t it make more sense to just have a list of column keys e.g. columns or column_keys?

[table]
label = "WebMO Jobs"
type = "table"
columns = [ "col1", "col2", "col3" ]

[table.columns.col1]
label = "Column 1"
type = "integer"
…

If this was the case, plugin authors would always have to duplicate the column names, therefore an update would need updates in two places. By having the order inside the column, code duplication is minimized.

I’ve gotten my version working, but if the opinion is strongly in favor of a separate listing to determine order, I can implement that too.

Here is what I have:
TOML:

[TABLE]
label = "Test"
type = "table"
toolTip = "This is a table"
tab = "Basic"

[TABLE.columns.col1]
label = "Column 1"
order = 0
type = "integer"
minimum = 0
maximum = 9
defaults = [2, 5]

[TABLE.columns.col2]
label = "Column 2"
order = 1
type = "string"
defaults = ["row0_col1", "row1_col1"]

[TABLE.columns.col3]
order = 2
type = "boolean"
editable = false
defaults = [true, false]

Return value:

{
  "TABLE": {
    "Column 1": [2, 5],
    "Column 2": ["row0_col1", "row1_col1"],
    "col3": [true, false]
  }
}

That doesn’t work, because table.columns is defined twice – the equivalent JSON would be:

{
  "table": {
    "label": "WebMO Jobs",
    "type": "table",
    "columns": [
      "col1",
      "col2",
      "col3"
    ],
    "columns": {
      "col1": {
        "label": "Column 1",
        "type": "integer",
      }
    }
  }
}

However, if the list of columns was called column_keys or column-keys or column-order (as in my example) then it would work fine like that, yeah.

The keys of this should be the same keys used in TABLE.columns in the definition though, not the label values.

But otherwise, looks great to me!

1 Like

This wouldn’t work because the table does not store the original column key. We could just make it so that the original column key is the header, which would make a lot more sense in my opinion.

If anyone is curious, I opened a PR for this feature.

By the way, I added support for filePath and stringList widgets in addition to the others, so now anything that could go into a regular widget can go into a table.

1 Like