Skip to main content

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.

assets/crud_create.png assets/crud_update.png

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, Enum.
  • 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)
        name = Column(String)
        active = Column(Boolean, default=True)
        count = Column(Integer)
        text = Column(Text)
        created_at = Column(DateTime)
    
    # 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

  1. Customize behavior with Meta Attributes:

    class YourModel(Base, StreamlitAlchemyMixin):
        __tablename__ = "your_model"
    
        id = Column(Integer, primary_key=True)
        name = Column(String)
        active = Column(Boolean, default=True)
        count = Column(Integer)
        text = Column(Text)
        created_at = Column(DateTime)
    
        # Customize the form fields with non-defaults
        __st_input_meta__ = {
            'name': st.text_input,
            'active': lambda *a, **kw: st.checkbox(*a, **kw, value=False),
        }
    
        # Customize display of the instances in the selectbox
        __st_repr__ = lambda _self: f'{self.name} ({self.count})'
    
        # Customize the order of the instances in the selectbox
        __st_order_by__ = lambda _self: self.count
    
  2. CRUD Tabs:

    YourModel.st_crud_tabs(
        defaults={"name": "Default Name"},  # Will not appear in the create form
        filter_by={"active": True},  # Will filter the instances in the selectbox
        except_columns=["active"],  # Will not appear in the update form
        border=True,  # Will add a border around the form
    )
    
  3. Create Form:

    YourModel.st_create_form(
        defaults={"active": False},  # Will not appear in the form
        border=True,  # Will add a border around the form
    )
    
  4. Edit Button:

    your_model_instance.st_edit_button(
        "Edit",  # Button label
        {"name": "New Name"},  # Will be updated on click
        # Any other kwargs will be passed to the st.button
    )
    
  5. Delete Button:

    your_model_instance.st_delete_button(
        label="Delete",  # Button label
        # Any other kwargs will be passed to the st.button
    )
    

Simple Example

import streamlit as st
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from streamlit_sqlalchemy import StreamlitAlchemyMixin

Base = declarative_base()

class ExampleModel(Base, StreamlitAlchemyMixin):
    __tablename__ = "example"

    id = Column(Integer, primary_key=True)
    name = Column(String)

# Initialize the connection
CONNECTION = st.connection("example_db", type="sql")
Base.metadata.create_all(CONNECTION.engine)
StreamlitAlchemyMixin.st_initialize(CONNECTION)

# Create CRUD tabs
ExampleModel.st_crud_tabs()

Comprehensive Example

import logging
from pathlib import Path

import streamlit as st

from examples.models import Base, Task, User
from streamlit_sqlalchemy import StreamlitAlchemyMixin


def show_single_task(task):
    col1, col2, col3 = st.columns([1, 1, 1])
    if task.done:
        col1.write(f" - ~~{task.description}~~")
        with col2:
            task.st_delete_button()
    else:
        if task.due_date:
            date_color = "red" if task.due_date < datetime.now() else "green"
            col1.write(f" - {task.description} (:{date_color}[{task.due_date.strftime('%H:%M - %d.%m.%Y')}])")
        else:
            col1.write(f" - {task.description}")
        with col2:
            task.st_edit_button("Done", {"done": True})
        with col3:
            task.st_delete_button()


def app():
    st.title("Streamlit SQLAlchemy Demo")

    User.st_crud_tabs()

    with CONNECTION.session as session:
        for user in session.query(User).all():
            with st.expander(f"### {user.name}'s tasks:"):
                c = st.container()

                st.write("**Add a new task:**")
                Task.st_create_form(defaults={"user_id": user.id, "done": False})
                with c:
                    if not user.tasks:
                        st.caption("No tasks yet.")

                    for task in user.tasks:
                        show_single_task(task)


def main():
    if not Path("example.db").exists():
        Base.metadata.create_all(CONNECTION.engine)

    StreamlitAlchemyMixin.st_initialize(connection=CONNECTION)

    app()


if __name__ == "__main__":
    # initialize the database connection
    # (see https://docs.streamlit.io/library/api-reference/connections/st.connection)
    CONNECTION = st.connection("example_db", type="sql")
    main()

You can explore this provided example, and launch it from the root directory (because it relies on relative imports):

python -m streamlit run examples/example.py

assets/streamlit-example-2023-12-31-16-12-91.gif

Contributing

We welcome contributions! See our contribution guidelines for more details.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Metadata

Release files for streamlit-sqlalchemy 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-sqlalchemy 0.2.6
File Size Uploaded Provenance Actions
streamlit_sqlalchemy-0.2.6.tar.gz 9.7 MB Without provenance Details for streamlit_sqlalchemy-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-sqlalchemy 0.2.6
File Interpreter ABI Platform Actions
streamlit_sqlalchemy-0.2.6-py3-none-any.whl Python 3 none any Details for streamlit_sqlalchemy-0.2.6-py3-none-any.whl

Total release size: 9.7 MB

Release files / streamlit_sqlalchemy-0.2.6.tar.gz

Download URL streamlit_sqlalchemy-0.2.6.tar.gz
Size 9.7 MB
Tags Source
SHA-256 checksum
How to use checksums
b372fb8496cc083317e8df7bca1227c56bfa2dcf5b06f1a206756bc407de59f3
BLAKE2b-256 checksum
How to use checksums
e14723b86e003ca904702d2edb307b3c533b00824086ca098102488c63b9430f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / streamlit_sqlalchemy-0.2.6-py3-none-any.whl

Download URL streamlit_sqlalchemy-0.2.6-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e4240a595d689d8eb82eeb8340fe5a118b1d15a3a3782edf0aba90dac411870
BLAKE2b-256 checksum
How to use checksums
5be19a0868942f4cfe55a208362b63dbb65b3087681e4e943db0f44b1f614345
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for streamlit-sqlalchemy 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page