New Component: template sqlalchemy classes with streamlit-sqlalchemy

Note: this post was originally shared in the wrong channel: 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)
  1. CRUD Tabs:
YourModel.st_crud_tabs()
  1. Create Form:
YourModel.st_create_form()
  1. Edit Button:
your_model_instance.st_edit_button("Edit", {"field": "value"})
  1. Delete Button:
your_model_instance.st_delete_button()

Advanced Usage

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

6 Likes

Good work :heart:

1 Like

I going to try this with tiangolo SQLModel. Will post the result.

1 Like