New component: streamlit_sql, display a CRUD interface for sqlalchemy Models

Hello,

This component is a quick an easy way to offer a User Interface to a database. In just one function, show the data as a st.dataframe with pagination, sidebar filters, autocomplete text fields and handling foreign keys with a st.selectbox and the str representation of the foreign row, instead of the id number.

The page shows a button to create a new row in a dialog and clicking the row opens a dialog to edit or delete it.

To use it, you don’t need to change your sqlalchemy Models definition, just add a str method to them and the relationships, which you should probably have done anyway.

For more details see Github repository and the documentation

Demo available here

Comments and critics are welcome.

2 Likes

That’s fantastic! One recommendation would be to add an example app, which can be linked from the docs and the README, which shows this in action.

2 Likes

Please see the demo at https://example-crud.streamlit.app/

2 Likes

This is a really cool component!

One small suggestion:
When you delete a row, it would be nice if, after showing the “successfully deleted”, it:

  1. Closed the modal
  2. Automatically reloaded the dataframe view so that the deleted row is gone. [This happens after you unclick the row, but it would be nice to have it happen automatically]

You are right, in fact I tried to do this. The problem is that if I reload, it updates but I couldn’t display a notification if the operation was successful. I decided the notification was more important than the update. But it seems this is those situations when the streamlit way of working can be a little tricky, but I plan to tackle this issue.

One option might be to use st.toast for the notification, so that it’s independent of the modal being open.

I was difficult to handle session_state, but finally I could do it. Now After saving or deleting, it closes the dialog and show a st.success or st.warning.

Demo error


Please fix.
Thanks

Hello,

This line should have been restart_db.restart_db.clear()

Thank you for your interest. Be aware that I am finishing many improvements to this

New version available with breaking changes but a lot of enhacements: Much better UI and more advanced customizations

1 Like

New version, now with one to many relations in update form and conditionally styling of rows. Check it out!

I used the code from the example but got an error
NameError: name ‘db’ is not defined

Hello,

I am not sure if I understood your problem. The example code is just to get an idea and not a complete code.

In this context, “db” is a module that holds your sqlalchemy declarative base models.

So you should create your model definitions and have an active database.

Is this your question?