Skip to content

Conversation

@manzt
Copy link
Contributor

@manzt manzt commented Jul 18, 2025

Previously, marimo edit script.py would silently overwrite any Python file, potentially destroying user code (link).

#5677 introduced a prompt asking users to confirm before overwriting invalid files. This PR takes a stronger stance by making marimo edit error for non-marimo Python files and providing a conversion path.

It introduces two key changes:

  1. Protection against accidental overwrites: marimo edit now errors when attempting to open a non-marimo Python file, rather than prompting for confirmation. Users are directed to use marimo convert instead.

  2. Python script conversion support: marimo convert script.py now supports converting regular Python scripts to marimo notebooks. The converter:

    • Detects and converts py:percent format if used (requires jupytext), otherwise uses a single marimo cell
    • Transforms if __name__ == "__main__": blocks into _main_() functions to maintain marimo's variable scoping
    • Preserves file headers (docstrings/comments)

Importantly the notebook pipeline reuses the ipynb converter transformations for multiple cells.

Running marimo edit regular_script.py now shows:

Error: Python script not recognized as a marimo notebook.

  Tip: Try converting with

    marimo convert regular_script.py -o regular_script_nb.py

  then open with marimo edit regular_script_nb.py

@manzt manzt added the enhancement New feature or request label Jul 18, 2025
@manzt manzt requested a review from dmadisetti as a code owner July 18, 2025 16:47
@vercel
Copy link

vercel bot commented Jul 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marimo-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 29, 2025 2:46pm

@dmadisetti
Copy link
Collaborator

Oh no, potentially closed by #5677

@dmadisetti
Copy link
Collaborator

There's actually a fair bit of other logic in here. Will do a review on merge res if you want to keep it open

@manzt
Copy link
Contributor Author

manzt commented Jul 18, 2025

if it makes the review easier, i'm happy to break into two separate PRs:

  • one that checks and errors on marimo edit unknown_py_script.py
  • one that add marimo convert and updates the error message from above with a Tip

@manzt
Copy link
Contributor Author

manzt commented Jul 18, 2025

Most of the logic is in the second commit for adding a new converter. The error is just adding another violation that we error on inside marimo edit.

@manzt manzt force-pushed the manzt/no-override branch from b08dc01 to d154c07 Compare July 18, 2025 17:04
@dmadisetti
Copy link
Collaborator

I think splitting these out make sense, will make the merge res easier

@willingc
Copy link

Thanks @manzt @dmadisetti. Simple is fine by me. It was a bit of a surprise when it happened. As an FYI, I could see folks wanting to be able to bulk convert a directory of .py files to marimo notebooks, such as when creating examples for docs, which is how I discovered the behavior.

@dmadisetti
Copy link
Collaborator

Thanks @willingc, hope you didn't lose any important files 😬
The bulk util does make sense, a general pattern matching, or repeating arg might be reasonable

@mscolnick
Copy link
Contributor

calling it unknown python script feels a bit weird to me. especially if we introduce marimo convert pypercent, this will conflict with this code path. and in future, if/when we do add marimo convert pypercent, we can treat these "unknown scripts" as just a pypercent with 1 cell.

@willingc
Copy link

@dmadisetti I didn't lose anything since everything is under version control.

I agree with @mscolnick "unknown python script" is odd phrasing to a user. If everything is a .py file, it should behave like a .py file whether or not it is a marimo notebook.

Ultimately, I would love to see (some of which you already have):

  • A regular .py file (code or script) opened in a marimo notebook (going to call it .mpy) without data loss
  • A regular .py file (code or script) converted to a marimo notebook (.mpy)
  • A marimo notebook (.mpy) stripped and converted to a regular .py file without marimo code included (IMHO, cleaner for using in a pipeline)
  • A Jupyter notebook (.ipynb) converted to a marimo notebook (.mpy) with cells similar to the initial Jupyter notebook
  • A marimo notebook (.mpy) converted to a Jupyter notebook (.ipynb)
  • A marimo notebook exportable as a PDF
  • Bonus points: being able to do that using pandoc 😉

Copy link

@willingc willingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the effort here. I would be fine with a simple warning that the .py file does not include marimo notebook code. See this link on creating or converting... would be fine short term.

@manzt manzt force-pushed the manzt/no-override branch from d154c07 to 570b2e9 Compare July 28, 2025 15:24
@manzt manzt force-pushed the manzt/no-override branch from 570b2e9 to e26aa43 Compare July 28, 2025 15:57
@manzt manzt changed the title Prevent accidental overwrites and add Python script conversion for unknown scripts Prevent accidental overwrites and converter for non-marimo Python scripts Jul 28, 2025
@manzt manzt force-pushed the manzt/no-override branch from e26aa43 to ab06020 Compare July 28, 2025 18:47
@manzt manzt force-pushed the manzt/no-override branch from ab06020 to 04733eb Compare July 28, 2025 18:49
@manzt manzt requested a review from akshayka as a code owner July 28, 2025 18:49
@manzt manzt force-pushed the manzt/no-override branch from f41a25d to 1ecb8b0 Compare July 28, 2025 18:52
@manzt
Copy link
Contributor Author

manzt commented Jul 28, 2025

I might followup to make confirm non-blocking in non-tty contexts.

I don't think these changes introduce any new interactive code paths, unless I'm misunderstanding. Are you referring to improving the existing overwrite confirmation to be non-blocking in non-TTY contexts?

@dmadisetti
Copy link
Collaborator

I don't think these changes introduce any new interactive code paths, unless I'm misunderstanding. Are you referring to improving the existing overwrite confirmation to be non-blocking in non-TTY contexts?

Yes, related to this PR, but yes nothing this code explicitly touches

manzt added a commit that referenced this pull request Jul 28, 2025
Updates docs to reflect #5686's new `marimo convert` support for `.py`
files, including automatic py:percent format detection when jupytext is
available.
manzt added a commit that referenced this pull request Jul 28, 2025
Updates docs to reflect #5686's new `marimo convert` support for `.py`
files, including automatic py:percent format detection when jupytext is
available.
manzt added a commit that referenced this pull request Jul 28, 2025
Updates docs to reflect #5686's new `marimo convert` support for `.py`
files, including automatic py:percent format detection when jupytext is
available.
@manzt manzt force-pushed the manzt/no-override branch from 35d43f4 to ad17abc Compare July 29, 2025 01:54
manzt added a commit that referenced this pull request Jul 29, 2025
Updates docs to reflect #5686's new `marimo convert` support for `.py`
files, including automatic py:percent format detection when jupytext is
available.
mscolnick
mscolnick previously approved these changes Jul 29, 2025
manzt and others added 10 commits July 29, 2025 10:42
Currently, opening an unrecognized Python script with `marimo edit
script.py` overwrites the file contents with an empty marimo notebook,
causing data loss. This change detects non-marimo Python scripts when
the parser encounters Python code that doesn't follow the marimo
notebook format, prompting the users to `marimo convert` the file first.
Enable `marimo convert script.py` to convert regular Python scripts.
Co-authored-by: Akshay Agrawal <akshay@marimo.io>
Copy link
Collaborator

@dmadisetti dmadisetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! See comment. I can still follow up in a PR, but let's decide and get it in before next release.
I'm happy with this PR, but LMK if you have thoughts and want to adjust the flow here

) from None

# Only show the tip if we're in an interactive terminal
if status == "invalid" and sys.stdin.isatty():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the rebase.
This might be dead code now? Either way the ideal behavior for molab is:

if not tty and invalid: continue # as normal (regardless of .py ending)

If this is too dangerous, we can hide behind a another flag. An auto convert flag could be reasonable

if invalid and autoconvert: convert and continue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above condition is:

if status == "invalid" and filename.endswith(".py"):

I assume non-Python files could also result in an invalid notebook status, making this code not dead in that context? I don't know the molab context well enough and trust whatever you think.

@manzt manzt merged commit 997d7d5 into main Jul 29, 2025
26 of 40 checks passed
@manzt manzt deleted the manzt/no-override branch July 29, 2025 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bash-focus Area to focus on during release bug bash enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants