Skip to content

Commit

Permalink
Add templates as optional parameter to rest
Browse files Browse the repository at this point in the history
  • Loading branch information
blythed committed Nov 8, 2024
1 parent bdc998c commit dc209de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Changes Since Last Release

#### Changed defaults / behaviours
#### Changed defaults / behaviours

#### New Features & Functionality

Expand Down
8 changes: 7 additions & 1 deletion superduper/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def start(
host: str = 'localhost',
headless: bool = False,
data_backend: str | None = None,
templates: str | None = None,
):
"""Start the rest server and user interface.
Expand All @@ -39,7 +40,12 @@ def start(

CFG.log_colorize = False

app = SuperDuperApp('rest', port=remote_port, data_backend=data_backend)
app = SuperDuperApp(
'rest',
port=remote_port,
data_backend=data_backend,
templates=templates.split(',') if templates else None,
)

if host == 'localhost':
# host frontend and server together
Expand Down
18 changes: 18 additions & 0 deletions superduper/rest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(
db: Datalayer = None,
prefix: str = '',
data_backend: str | None = None,
templates: t.List[str] | None = None,
):
if prefix and not prefix.startswith('/'):
prefix = f'/{prefix}'
Expand Down Expand Up @@ -106,6 +107,7 @@ def __init__(
)
self._db = db
self.data_backend = data_backend
self.templates = templates

@cached_property
def app(self):
Expand Down Expand Up @@ -190,6 +192,19 @@ def start(self):
self.print_routes()
self.run()

def _add_templates(self, db):
if self.templates:
from superduper import templates

existing = db.show('template')
for t in self.templates:
if t in existing:
logging.info(f'Found existing template: {t}')
continue
logging.info(f'Applying template: {t}')
t = getattr(templates, t)
db.apply(t, force=True)

def startup(
self,
cfg: t.Union[Config, None] = None,
Expand All @@ -212,6 +227,9 @@ def startup_db_client():
db = build_datalayer(cfg)
else:
db = self._db

self._add_templates(db)

self._app.state.pool = db
self._app.state.pool.cluster.initialize()
self._db = db
Expand Down

0 comments on commit dc209de

Please sign in to comment.