-
Notifications
You must be signed in to change notification settings - Fork 34
Add support for Marimo notebooks #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8fe991f
feat: add Marimo integration files and tests, update dependencies, an…
N283T 08b87b5
feat: Improve Marimo integration by delaying widget display and retur…
N283T ae971a7
refactor: make marimo an optional dependency
N283T 44e087f
refactor: Consolidate Marimo display logic to use iframes and update …
N283T 62f3990
feat: update Marimo examples.
N283T a69f372
feat: update Marimo examples.
N283T 018fe7e
feat: Add `MolGrid.get_selection_state` for Marimo integration and up…
N283T 34f8c54
refactor: combine patch contexts in Marimo integration test
N283T 597da3c
refactor: improve Marimo integration by relocating grid overwrite war…
N283T ff3aeb0
docs: mention Marimo compatibility and add contributor
N283T File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import marimo | ||
|
|
||
| __generated_with = "0.18.4" | ||
| app = marimo.App(width="medium") | ||
|
|
||
|
|
||
| @app.cell | ||
| def import_libraries(): | ||
| import marimo as mo | ||
|
|
||
| import mols2grid | ||
| from mols2grid.datafiles import SOLUBILITY_SDF | ||
|
|
||
| return SOLUBILITY_SDF, mo, mols2grid | ||
|
|
||
|
|
||
| @app.cell | ||
| def prepare(mo): | ||
| from rdkit.Chem.Draw import rdMolDraw2D | ||
|
|
||
| def mol_to_svg(mol, width=130, height=90, opts=None): | ||
| if mol is None: | ||
| return mo.Html("") | ||
|
|
||
| drawer = rdMolDraw2D.MolDraw2DSVG(width, height) | ||
|
|
||
| if opts is not None: | ||
| drawer.SetDrawOptions(opts) | ||
|
|
||
| drawer.DrawMolecule(mol) | ||
| drawer.FinishDrawing() | ||
| return mo.Html(drawer.GetDrawingText()) | ||
|
|
||
| solubility_range = mo.ui.range_slider( | ||
| -10, | ||
| 2, | ||
| 0.5, | ||
| debounce=True, | ||
| show_value=True, | ||
| full_width=True, | ||
| label="Solubility", | ||
| ) | ||
| return mol_to_svg, solubility_range | ||
|
|
||
|
|
||
| @app.cell | ||
| def create_grid(SOLUBILITY_SDF, mols2grid): | ||
| # NOTE: | ||
| # This cell is intentionally kept independent from the sliders. | ||
| # In marimo, cells are re-executed whenever any of their dependencies change. | ||
| # Keeping grid creation here prevents MolGrid.from_sdf(...) from being | ||
| # re-run on every slider update, which would reset the widget state. | ||
| grid = mols2grid.MolGrid.from_sdf(SOLUBILITY_SDF, size=(120, 100)) | ||
| get_selection_ids = grid.get_marimo_selection() | ||
| view = grid.display(n_items_per_page=12, selection=True) | ||
| return get_selection_ids, grid, view | ||
|
|
||
|
|
||
| @app.cell | ||
| def filter_and_display(grid, mo, solubility_range, view): | ||
| mask = grid.dataframe["SOL"].between(*solubility_range.value) | ||
| results = grid.dataframe.loc[mask] | ||
| # Same as: | ||
| # grid.dataframe["SOL"] >= solubility_range.value[0]) & \ | ||
| # (grid.dataframe["SOL"] <= solubility_range.value[1]) | ||
|
|
||
| grid.filter_by_index(results.index) | ||
|
|
||
| mo.vstack([solubility_range, view]) | ||
| return (results,) | ||
|
|
||
|
|
||
| @app.cell(hide_code=True) | ||
| def display_selection(get_selection_ids, mo, mol_to_svg, results): | ||
| # This cell displays the selected molecules in a Marimo table. | ||
| # The `mol_to_svg` function is used to render the molecule images | ||
| # directly in the table. | ||
| # We filter the dataframe based on the selection state (`get_selection_ids`) | ||
| # from the grid above. | ||
|
|
||
| selected = results[results["mols2grid-id"].isin(get_selection_ids())] | ||
|
|
||
| # # If you want to use custom drawing options: | ||
| # opts = rdMolDraw2D.MolDrawOptions() | ||
| # opts.explicitMethyl = True | ||
|
|
||
| table = mo.ui.table( | ||
| selected.reset_index(drop=True).drop(columns="img"), | ||
| format_mapping={ | ||
| "mol": mol_to_svg | ||
| # "mol": lambda mol: mol_to_svg(mol, opts=opts) | ||
| # If you want to use custom drawing options | ||
| }, | ||
| freeze_columns_left=["mols2grid-id", "mol"], | ||
| freeze_columns_right=["SOL"], | ||
| label="Try selecting molecules from the grid above!!", | ||
| ) | ||
|
|
||
| table # noqa: B018 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app.run() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just me thinking out loud, definitely NOT something to address in this PR, but in my refactoring branch,
I might move this to be directly integrated in the
SelectionRegister.selection_updatedinselect.pywith some mechanism to register custom callbacks that would be triggered byselection_updated, just so that we don't have multiple observers listening to the same event, e.g.