Prebuilt database editor?

What I’m trying to do: I’d like to find an anvil app that lets you edit all your database tables/records. Sort of an admin type interface I guess you would call it.

What I’ve tried and what’s not working:
Creating the editor from scratch is so tedious. It seems there should be some framwork that has this functionality built in.

1 Like

Hi @tavdog and welcome!

This may be of interest to you:

You could also work from this tutorial app (scroll to the bottom for the clone link for the final version): Edit and Delete Rows

See also this feature request: Services Admin in Apps

Here’s a little app that may do some of the hard work for you.

https://anvil.works/build#clone:TYC5RXTSDZD6IMLD=XAZM37YZSNHLIJSVG5YEXORW

It allows an editable datagrid to be created in code like:

    TE = TableEditor(table=app_tables.table_0)
    self.content_panel.add_component(TE)

The TableEditor goes through the columns of table_0 and uses the definition to add the appropriate edit component.




The general approach was to subclass from DataGrid and to override the ItemTemplate of the RepeatingPanel that was added to the DataGrid. It also assumes that the Table permissions are set such that they are editable from client code.

You could extend the clone above to be more bespoke depending on your requirements.


Some code:

class TableEditor(DataGrid):
    def __init__(self, table, **properties):
        columns = filter(lambda d: not d['type'].startswith('live'), 
                         table.list_columns()) # ignore live object columns
        columns = [{'id': get_id(), # random string
                    'title': c['name'].capitalize(), 
                    'data_key': c['name'], 
                    'type': c['type']
                   } 
                    for c in columns
                  ]
        super().__init__(auto_header=True, 
                         columns=columns, 
                         rows_per_page=10, 
                         show_page_controls=True, 
                         **properties)
        
        def item_template(item, **properties):
            drp = DataRowPanel(item=item, 
                               auto_display_data=False, 
                               **properties)
            for column in columns:
                c = get_component(item, column) 
                # get the component based on the column type
                if c is not None:
                    drp.add_component(c, column=column['id'])
            return drp
        
        rp = RepeatingPanel(item_template=item_template)
        self.add_component(rp)
        rp.items = table.search()
        
3 Likes