Basic CRUD in Dash DataTable and saving into DB - example in Python/Postgres or SQL

I am on the verge of giving up on Dash totally since I struggle so much to implement basic add row/delete row/update row(table) and to get database updated automatically. I don’t know why is it so difficult to this basic thing. My table is editable and on click or enter value in a cell it is updating DB automatically, also I need to delete row and update it. I am using Python/Postgres DB.

Is there any basic example on how to do this or does anyone here have a basic working example? The documentation Editable DataTable | Dash for Python Documentation | Plotly of adding new rows/columns etc isn’t complete, I don’t see how to figure out what value user inserted into cells. Any help is appreciated.

If anyone can help in any way, that would be great, it is very urgent.

You need to provide a minimum example of code that is not working for you but you expect it to be. There could be dozens of things that do not work in the way you expect them to work, provide any error you are getting or expected behavior vs. actual behavior. It’s impossible to guess what your problem is :).

Also it’s worth noting the DataTable is currently in Alpha, make sure you have the latest version installed as bugs are being constantly fixed.

There are two main problems:

  1. I have two events that happen when user arrives at the app. The first event is ‘Add New Row’ (button) click event which should insert new data into a database when user starts typing into a Datatable cells in a newly added row.

    The second event is update table which should update database if user starts typing anywhere except on a newly added row. I can’t distinguish between the two, between the newly added row that should do insert on typing and all other rows which should do update on user typing. I tried with n_clicks and everything I could think of but nothing works.

  2. I created a ‘delete selected rows’ link which should remove rows from the Datatable and from the database also. That doesn’t seem to be possible at all or I couldn’t find how to do it, no ideas for that at all.

It is almost 7pm here and if I go and change code now it would take me at least an hour because I am not allowed to share the original code and I have a deadline tomorrow. If you know what I could do in a few sentences or a link that I could look at, that would be great.

You can try to write some pseudo code or just some example app with a table, it is very hard to help you without seeing some code

If it’s helpful to anyone. here’s a simple crud app I created with Dash DataTable connected to PostgreSQL. (takes 8 seconds to load). And here’s the code.

3 Likes

This looks great! Thanks for sharing!

Hi, is there a more reliable way to save data other than pd to sql ? overwriting the entire table sometimes craches the database especially in production

Of course, you can use ORM tools such as SQLAlchemy to manipulate the database. Using pd is just an example.

1 Like

Hi, thanks for your response.

what i meant by that is to keep using dash datatable in the frontend and find a more reliable way to update the table other than pd to sql. in this case, it is necessary to keep track of the changes to know which data to update, create or/and delete.

Yes, I know. And you may check this below. I would say that they have taken these into consideration and are continuing to improve.

link is here: Dash-by-Plotly/Dash_More_Advanced_Stuff/CRUD_app/crud_dash_postgresql.py at master · Coding-with-Adam/Dash-by-Plotly · GitHub