Has anyone achieved a CRUD database interface in correspondence with parameterized objects with param and panel? I mean using param as like an ORM and panel as a front end? I’m most familiar with django models, but perhaps this can be done with sqlalchemy or duckdb or some kind of database interface. Then… making data apps becomes instantly accessible and scalable with panel from a data perspective. It’s like an auto-api through just defining param classes. And an auto-front end by serving with panel. Seems really powerful! I’m just thinking out loud, but I’m sure some more advanced community members here have achieved this.
I did come across @alitrack in another post about GitHub - alitrack/fab-dash: Flask Appbuilder Dashboard but I am not very familiar with flask and I’m wondering what some alternative methods might looks like. I would give points to the simplest or most holovizzy solutions.
import param as pm
from sqlalchemy import (
Boolean,
Column,
Date,
DateTime,
Float,
Integer,
PickleType,
Sequence,
String,
)
def param_to_sqlalchemy_type(param_type):
if isinstance(param_type, pm.String):
return String
elif isinstance(param_type, pm.Number):
return Float
elif isinstance(param_type, pm.Integer):
return Integer
elif isinstance(param_type, pm.Boolean):
return Boolean
elif isinstance(param_type, pm.Date):
return Date
elif isinstance(param_type, pm.DateTime):
return DateTime
elif isinstance(param_type, (pm.List, pm.Dict, pm.Tuple)):
return PickleType
elif isinstance(param_type, pm.Selector):
# Assuming string type for simplicity; adjust based on actual use case
return String
else:
raise TypeError(f'Unsupported param type: {type(param_type)}')
def parameterized_to_model(cls, Base):
# Define a primary key column
attrs = {'id': Column(Integer, primary_key=True)}
# Add other columns based on param attributes
attrs.update(
{
name: Column(param_to_sqlalchemy_type(param))
for name, param in cls.param.objects().items()
if isinstance(param, pm.Parameter)
}
)
attrs['__tablename__'] = cls.__name__.lower()
return type(cls.__name__ + 'Model', (Base,), attrs)
Example:
import param as pm
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from auto_crud_param import parameterized_to_model
Base = declarative_base()
# Example usage
class A(pm.Parameterized):
name = pm.String(default='Item A')
value = pm.Number(default=0)
AModel = parameterized_to_model(A, Base)
# Set up the database (for example, using SQLite)
engine = create_engine('sqlite:///mydatabase.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
hi @LinuxIsCool very cool thanks for your sharing , kindly please advise if we want to store CRUD to database which key/parameter that we need to put them to DB, or if you have any example for CRUD to mongoDB or any other DB would be glad