New Component: streamlit-sqlalchemy, a new way to template sqlalchemy classes

Streamlit SQLAlchemy Integration

Overview

streamlit_sqlalchemy is a Python module that provides seamless integration between Streamlit and SQLAlchemy models. It simplifies the process of creating, updating, and deleting database objects through Streamlit’s user-friendly interface.

streamlit-example-2024-01-06-13-01-36

Features

  • Easy Initialization: Initialize the SQLAlchemy connection with a simple method call.
  • CRUD Operations: Create, read, update, and delete operations are streamlined with minimal code.
  • Dynamic Forms: Automatically generate forms for creating and updating database objects.
  • SQLTypes Support: String, Text, Integer, Float, Boolean, Date, DateTime, Time.
  • Foreign Key Support: Easily handle foreign key relationships in forms.

Installation

pip install streamlit_sqlalchemy

Usage

  1. Initialize the Engine:

    from streamlit_sqlalchemy import StreamlitAlchemyMixin
    
    # Create your SQLAlchemy model
    class YourModel(Base, StreamlitAlchemyMixin):
        __tablename__ = "your_model"
    
        id = Column(Integer, primary_key=True)
        # Other columns...
    
    # Initialize the connection
        StreamlitAlchemyMixin.st_initialize(connection=conn)
    
  2. CRUD Tabs:

    YourModel.st_crud_tabs()
    
  3. Create Form:

    YourModel.st_create_form()
    
  4. Edit Button:

    your_model_instance.st_edit_button("Edit", {"field": "value"})
    
  5. Delete Button:

    your_model_instance.st_delete_button()
    

Advanced Usage

Learn more: GitHub - artygo8/streamlit-sqlalchemy: Templating for streamlit and sqlalchemy

3 Likes

Hi, i found your project through github researching exactly how to be more efficient with crud/sqlalchemy etc. I think this is nice playbuddy to the streamlit-pydantic library.

Many exciting expansions come to mind.

  • Interacting with (or supercharging) the editable data_editor.
  • multi row editing (as someone also mentioned on the github)

Anyway, beautiful, I will work on some experiments.

I was wondering if it should also support UUIDs? (Given we are talking CRUDs)

1 Like

Hey,
Thank you very much for your feedback, it means a lot! :smile:

I felt like the streamlit-pydantic library was not doing enough for my use case, but the tool is far from being perfect.

The expansions that you mentioned sound like very good ideas! :balloon:

  • Interacting with the editable data_editor might be a little bit tricky with the current implementation, as everything is based on a naive CRUD approach for now.
  • Multi row editing is definitely a “must have”.
  • I don’t know why I didn’t implement the UUIDs yet, tbh. Might be because it is easy to add with a lambda.

As I said, it is far from being perfect, and I even wonder at times if it is good enough to be useful… If you struggle with anything or have a different opinion about how it should be interacted with, I’d be interested to know.

Do not hesitate to contribute if you feel like it. :sunglasses: