Skip to content

Commit b752794

Browse files
committed
fix: execution order for marimo
1 parent 62d7de8 commit b752794

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

mols2grid/molgrid.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,10 @@ def get_marimo_selection(self):
804804
"""
805805
if not is_running_within_marimo():
806806
raise RuntimeError("This method is only available in a marimo notebook.")
807+
if not hasattr(self, "widget"):
808+
raise RuntimeError(
809+
"Please run the `display` method first to render the underlying widget"
810+
)
807811

808812
import marimo as mo
809813

@@ -1095,12 +1099,16 @@ def display(
10951099
-------
10961100
view : IPython.core.display.HTML
10971101
"""
1098-
use_iframe = is_jupyter if use_iframe is None else use_iframe
10991102
obj = self.render(**kwargs, use_iframe=use_iframe)
1100-
1103+
# if widget
11011104
if not isinstance(obj, str):
1105+
if is_running_within_marimo():
1106+
import marimo as mo
1107+
1108+
return mo.ui.anywidget(obj)
11021109
return obj
11031110

1111+
use_iframe = is_jupyter if use_iframe is None else use_iframe
11041112
if not use_iframe:
11051113
return HTML(obj)
11061114

scripts/marimo_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def create_grid(SOLUBILITY_SDF, mols2grid):
5151
# Keeping grid creation here prevents MolGrid.from_sdf(...) from being
5252
# re-run on every slider update, which would reset the widget state.
5353
grid = mols2grid.MolGrid.from_sdf(SOLUBILITY_SDF, size=(120, 100))
54-
get_selection_ids = grid.get_marimo_selection()
5554
view = grid.display(n_items_per_page=12, selection=True)
55+
get_selection_ids = grid.get_marimo_selection()
5656
return get_selection_ids, grid, view
5757

5858

@@ -95,7 +95,6 @@ def display_selection(get_selection_ids, mo, mol_to_svg, results):
9595
freeze_columns_right=["SOL"],
9696
label="Try selecting molecules from the grid above!!",
9797
)
98-
9998
table # noqa: B018
10099

101100

0 commit comments

Comments
 (0)