Exporting Notebooks
00:00 In the previous lesson, I showed you marimo’s sandboxing mode. In this lesson, I’ll demonstrate exporting.
00:07 marimo comes with a variety of export options, some that just export the visual aspect, and others that translate your notebook file into a plain old Python script.
00:17
I’m going to re-open our sim_eq.py
notebook from a few lessons back.
00:23 Hopefully, you remember this notebook from before. It uses NumPy to solve a set of equations, using the hamburger menu at the top here, I choose Download, and I’m presented with a series of choices.
00:37 I can output it as HTML, with or without the code. I can also produce Markdown, Python, and in certain situations produce PNG and PDF as well. Let me choose HTML, and the resulting export goes into my downloads folder.
00:55 Let me open that in a separate tab
01:00 and here you have something that looks familiar. Note this is just HTML. If you export a notebook with UI components, you’ll be able to see them and click them, but they won’t do anything.
01:10 You will get a little message telling you that it’s a static notebook, and that these are just for show though if you try to do that. So that’s the downloading.
01:19 You can also convert your notebook on the command
01:29
line. The export
subcommand translates your notebook file into a script. The -o
flag indicates the name of your new script file.
01:38
Let me run the file. You’ll remember this script had some Markdown and a print()
. Well, the Markdown doesn’t get printed out, which is why I put the print()
in the middle of it, even if it wasn’t useful in the notebook.
01:52
Inside this script, the NumPy calculation has been run, the equations got solved, and the print()
got called issuing this result. Let’s take a look at the contents of the script.
02:06 All in all, this looks a fair amount like our notebook. Two important differences. The DAG is gone, so the chunks of code here that correspond to cells may not be in the same order as they are presented in the notebook.
02:19
You still get a hint of where the cell boundaries are from these %%
comments.
02:25 Secondly, although marimo still gets imported, no app gets instantiated, so the Markdown cells do nothing. If it were up to me, I think I’d print out Markdown cells, but maybe that’s a future feature.
02:38
Let me scroll down here, and this is why the print()
in the notebook was important, so that something would be output when you exported the script.
02:46
If you’re going to export something, you may have to do a bit of editing, sprinkling some print()
s along the way to get the output that you want, but that’s kind of a small ask to go from notebook to fully independent script in a few short steps.
03:01 And that’s pretty much the course. Last up, I’ll summarize what you’ve learned, and point you at some other resources.
Become a Member to join the conversation.