Skip to content
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

Convert to Jupyter notebook or Python script #717

Closed
King-of-Infinite-Space opened this issue Feb 1, 2024 · 7 comments
Closed

Convert to Jupyter notebook or Python script #717

King-of-Infinite-Space opened this issue Feb 1, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@King-of-Infinite-Space
Copy link

Description

Currently marimo convert converts Jupyter notebook to marimo notebook but can't convert it back.

Also, although marimo notebooks can be executed as scripts, it is still useful to export it to a python script (rather than having the code wrapped in functions). Jupyter supports exporting as python script through nbconvert.

These features would make it easier for people to integrate marimo into their existing workflows. Thanks!

Suggested solution

Support marimo convert your_notebook.py > your_notebook.ipynb command.
Add "Export to Python script" option.

Alternative

No response

Additional context

No response

@akshayka
Copy link
Contributor

akshayka commented Feb 2, 2024

Hey, thanks for the suggestion. Conversion from a marimo notebook to a Jupyter notebook may not work well -- Jupyter doesn't support any of the marimo UI elements or library functions, and it also doesn't support out of order execution.

Conversion to a pure Python script is something we could conceivably do in the future.

@scls19fr
Copy link
Contributor

scls19fr commented Mar 2, 2024

Even if we could lose some features it will be "fair" that Marimo try to do it's best to convert into Jupyter notebook.

round-trip conversion between Jupyter notebook and Marimo notebook wil lead to some features loss... that's life!

You said that Jupyter doesn't support out of order execution

We can consider that the export should consider one cell (maybe first, maybe latest a "input" for data flow).

Moreover there is not only on kernel for Jupyter.

Most people use ipykernel (see https://docs.jupyter.org/en/latest/projects/kernels.html ) but there is also ipyflow https://github.com/ipyflow/ipyflow which is a reactive kernel for Jupyter.

@mscolnick
Copy link
Contributor

hey @King-of-Infinite-Space - we haven't added "export to Jupyter", but we do have "export to a flat Python script"

# prints to stdout
marimo export script notebook.py > notebook.script.py
# writes to the outfile
marimo export script notebook.py --output notebook.script.py
# writes to the outfile and watch notebook.py for changes
marimo export script notebook.py --watch --output notebook.script.py

@dmadisetti
Copy link
Collaborator

I want to add that with export to markdown you can also do

# prints to stdout
marimo export md notebook.py > notebook.qmd
# writes to jupyter notebook, maybe with a bit of post processing (replace .python.marimo with just python)
quarto convert notebook.qmd

@King-of-Infinite-Space
Copy link
Author

Thanks for following up. I noticed that the generated python script uses # --- to separate the cells. I suggest using # %%, because VSCode will detect them as code cells (doc). This is also the format used by VSCode via Jupyter: Export to Python Script, for example

# %%
import numpy as np

# %%
print(np.exp(1))

Then I can use Jupyter: Export Current Python File as Jupyter Notebook to convert it back to Jupyter notebook.

@akshayka
Copy link
Contributor

akshayka commented May 9, 2024

@King-of-Infinite-Space , I have a PR out implementing your suggestion (thank you!).

@mscolnick
Copy link
Contributor

I am going to close this issue given #1350, since you can now export as a Python script that is Jupyter compatible which can be run in vscode (or other jupyter magic-comment supporting IDEs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants