Skip to content

Commit 37e9df8

Browse files
committed
Break apart callbacks to suppress noisy warning
1 parent dbbd238 commit 37e9df8

File tree

1 file changed

+46
-7
lines changed
  • crystal_toolkit/components/transformations

1 file changed

+46
-7
lines changed

crystal_toolkit/components/transformations/core.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def update_transformation(enabled, states):
250250
# TODO: move callback inside AllTransformationsComponent for efficiency?
251251

252252
kwargs = self.reconstruct_kwargs_from_state(dash.callback_context.states)
253+
# for debug
254+
# print("transformation kwargs", kwargs)
253255

254256
if not enabled:
255257
input_state = (False,) * len(states)
@@ -286,7 +288,16 @@ def __init__(
286288
*args,
287289
**kwargs,
288290
):
291+
"""
292+
Create a component that can manage multiple transformations in a
293+
user-defined order.
294+
295+
:param transformations: if provided, only offer a subset of available
296+
transformaitons, provide as a string of the given transformation name
297+
:param input_structure_component: will supply the structure to transform
298+
"""
289299

300+
# get available transformations
290301
subclasses = TransformationComponent.__subclasses__()
291302
subclass_names = [s.__name__ for s in subclasses]
292303

@@ -306,6 +317,8 @@ def __init__(
306317
self.links["input_structure"] = input_structure_component.id()
307318
self.create_store("input_structure")
308319

320+
self.create_store("enabled-transformations", initial_data=[])
321+
309322
transformations = [
310323
t(input_structure_component_id=self.id("input_structure"))
311324
for t in transformations
@@ -370,11 +383,12 @@ def apply_transformation(transformation_data, struct):
370383

371384
struct = transformation.apply_transformation(struct)
372385
except Exception as exc:
373-
error_title = (
386+
error_title = html.Span(
374387
f'Failed to apply "{transformation.__class__.__name__}" '
375388
f"transformation: {exc}"
376389
)
377390
traceback_info = Reveal(
391+
id=self.id("Error"),
378392
title=html.B("Traceback"),
379393
children=[dcc.Markdown(traceback.format_exc())],
380394
)
@@ -385,12 +399,15 @@ def apply_transformation(transformation_data, struct):
385399
@app.callback(
386400
Output(self.id("transformation_options"), "children"),
387401
[
388-
Input(self.id("choices"), "value"),
389402
Input(self.id("input_structure"), "data"),
403+
Input(self.id("choices"), "value"),
390404
],
391405
[State(t.id(), "data") for t in self.transformations.values()],
392406
)
393-
def show_transformation_options(values, structure, *args):
407+
def show_transformation_options(structure, values, *args):
408+
409+
# for debug
410+
# print(dash.callback_context.triggered)
394411

395412
values = values or []
396413

@@ -408,11 +425,27 @@ def show_transformation_options(values, structure, *args):
408425
return [transformation_options]
409426

410427
@app.callback(
411-
[Output(self.id(), "data"), Output(self.id("error"), "children")],
428+
Output(self.id("enabled-transformations"), "data"),
429+
Input(self.id("choices"), "value"),
430+
)
431+
def set_enabled_transformations(value):
432+
"""
433+
This is due to an unfortunate but noisy bug that
434+
complains that this specific input is not present
435+
in the layout on load.
436+
"""
437+
return value
438+
439+
# TODO: make an error store too
440+
441+
@app.callback(
442+
# [
443+
Output(self.id(), "data"),
444+
# Output(self.id("error"), "children")],
412445
[Input(t.id(), "data") for t in self.transformations.values()]
413446
+ [
414447
Input(self.id("input_structure"), "data"),
415-
Input(self.id("choices"), "value"),
448+
Input(self.id("enabled-transformations"), "data"),
416449
],
417450
)
418451
def run_transformations(*args):
@@ -424,6 +457,9 @@ def run_transformations(*args):
424457
user_visible_transformations = args[-1]
425458
struct = self.from_data(args[-2])
426459

460+
# for debug
461+
# print("input struct", struct)
462+
427463
errors = []
428464

429465
transformations = []
@@ -432,7 +468,7 @@ def run_transformations(*args):
432468
transformations.append(transformation)
433469

434470
if not transformations:
435-
return struct, html.Div()
471+
return struct # , html.Div()
436472

437473
for transformation_data in transformations:
438474

@@ -478,7 +514,10 @@ def run_transformations(*args):
478514
]
479515
)
480516

481-
return struct, error_msg
517+
# for debug
518+
# print("transformed struct", struct)
519+
520+
return struct # , error_msg
482521

483522
# callback to take all transformations
484523
# and also state of which transformations are user-visible (+ their order)

0 commit comments

Comments
 (0)