Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 📝 Summary Followup to #3755 for #2293 allowing for "top level imports" For completion of #2293, I thin UI changes and needed for enabling this behavior. Notably: - Indicate when in function mode (maybe top level import too) - Provide hints when pushed out of function mode - Maybe allow the user to opt out of function mode? \+ docs This also increases security risk since code is run outside of runtime. This was always possible, but now marimo can save in a format that could skip the marimo runtime all together on restart. There are opportunities here. marimo could lean into this, and leverage external code running as a chance to hook in (almost a plugin system for free) But also issues, since a missing dep could stop the notebook from running at all (goes against the "batteries included" ethos). This can be mitigated with static analysis over just an import (markdown does this for instance), or marimo can re-serialize the notebook in the "safe" form, if it comes across issues in import. ## 🔍 Description of Changes Includes a bit of a refactor to codegen since there were a fair amount of changes. Allows top level imports of "import only" cells. The contents are pasted at the top of the file, with a bit of care not to break header extraction. ```python # Normal headers are retained # Use a notice to denote where generated imports start # Notice maybe needs some copy edit # 👋 This file was generated by marimo. You can edit it, and tweak # things- just be conscious that some changes may be overwritten if opened in # the editor. For instance top level imports are derived from a cell, and not # the top of the script. This notice signifies the beginning of the generated # import section. # Could also make this app.imports? But maybe increasing surface area for no reason import numpy # Note, import cells intentionally do not have a `return` # for static analysis feature below import marimo __generated_with = "0.11.2" app = marimo.App(_toplevel_fn=True) @app.cell def import_cell(): # Could also make this app.imports? But maybe increasing surface area for no reason import numpy # Note, import cells intentionally do not have a `return` # for static analysis feature below ``` Top level refs (this includes `@app.function`s) are ignored in the signatures. E.g. ```python import marimo as mo # ... @app.cell def md_cell(): mo.md("Hi") return ``` Since I was also in there, I added static analysis to ignore returning dangling defs. ```python @app.cell def cell_with_dangling_def(): a = 1 b = 2 return (a,) # No longer returns b since it's not used anywhere. Allowing for linters like ruff to complain. @app.cell def ref_cell(a): a + 1 return ``` LMK if too far reaching and we can break it up/ refactor. A bit more opinionated than the last PR Test border more on being more smoke tests than unit tests, but hit the key issues I was worried about. I can break them down more granularly if needed. Also LMK if you can think of some more edgecases. ## 📜 Reviewers <!-- Tag potential reviewers from the community or maintainers who might be interested in reviewing this pull request. Your PR will be reviewed more quickly if you can figure out the right person to tag with @ --> @akshayka OR @mscolnick --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information