Some templating for streamlit and sqlalchemy
Project description
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.
Features
- Easy Initialization: Initialize the SQLAlchemy engine 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.
- Tabbed Interface: Organize CRUD operations in a tabbed interface for better user experience.
- Foreign Key Support: Easily handle foreign key relationships in forms.
Installation
pip install streamlit_sqlalchemy
Usage
-
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 fields # Initialize the engine StreamlitAlchemyMixin.st_initialize(engine)
-
CRUD Tabs:
YourModel.st_crud_tabs()
-
Customization:
Customize the behavior by overriding methods in your model.
class CustomModel(YourModel): # Override methods as needed
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:
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:", expanded=True):
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for streamlit_sqlalchemy-0.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f84c41d91fcb921e928b15c764f2593f2d3ca928a2a6fabf376ce426c003aca |
|
MD5 | 204df7c8babbc09df817f121db9154b4 |
|
BLAKE2b-256 | 931982ef2ea333327b6ac5e740d570daee58e4bfc44970c757a6264eae80439f |
Hashes for streamlit_sqlalchemy-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccbe846878c2cf2af2cddbcd4cfa4697f7317b9771de421667795a136e864720 |
|
MD5 | cdd7096efc02c0fb8bbc648ef92d2c98 |
|
BLAKE2b-256 | 97c2e297ebf59fae93393c964d4ca0c96a2e80e0225c05f93c9094875afd274d |