Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Digging Into Operation Order

00:00 In the previous lesson, I built a small notebook to solve a set of linear equations. In this lesson, I’ll dig a little more into cell dependencies and how marimo determines run order.

00:11 A notebook isn’t the same as a single file Python script. It’s a set of cells. Since it’s interactive, you can make changes to the cells, and of course that will affect other cells.

00:21 If you make such a change, what behavior would you expect to happen? Jupyter tracks the cell edit order, but of course this isn’t the order of appearance on the screen, so the results can get a little unpredictable.

00:34 That means that it may not be obvious from looking at the notebook as to exactly how it came up with a particular answer. The resulting lack of reproducibility complicates things if you wish to share your notebook with somebody else, or if you rerun it.

00:48 marimo takes a different approach. It creates a Directed Acyclic Graph, known as a DAG, to determine the order of the cells. It always uses this DAG. That means the operation order is always the same, but it might not be the top to bottom order of the cells on the screen, so you might not get the answer you expected, but it will at least be consistent between runs.

01:10 For example, sometimes if you have an out of order import, marimo figures that out and does the right thing. This can be a little counterintuitive if you’re coming from the script programming world, but it’s a lot less counterintuitive than what other notebooks do.

01:26 Before I launch a new notebook to show you how ordering works, a quick little aside. I have multiple browsers on my computer, each with different settings and plugins installed.

01:36 When you use the marimo edit command, it invokes the operating system’s open action on the default browser. My default browser is where I read the web, and it’s set up differently than where I play with notebooks. Doubly so when I’m recording a video like this one and need the fonts cranked up super large.

01:52 You can stop marimo from launching a browser by passing the headless flag to the edit command. You then just copy and paste the URL it shows to whatever browser you want.

02:03 Okay, that’s enough of the tangent. Now I’ll fire up a new notebook to show you how ordering works.

02:10 This is my new notebook named hypotenuse.py. As you might guess from the name, I’m going to get my Pythagoras on. I’ll start with a quick little function.

02:37 Note that I’m intentionally not running the cell. In fact, I’m not going to run any of them until I’m done. You’ll understand why in a second.

02:46 Adding some variable definitions,

02:56 and now a cell to call the function.

03:05 And finally, somewhat counterintuitively, I’m going to remember that you have to import a module before you use it.

03:12 Remember, I still haven’t run any of these cells. Down in the corner here, the global play button indicates that the notebook is stale by being yellow. Before I press it, if this was a Python script written in this order, you’d have a problem.

03:26 The hypotenuse function uses the math module and function is called before the import happens. Best practice is to put your imports at the top, but Python doesn’t really care as long as it comes before the call to hypotenuse, or more specifically, before the use of the sqrt inside of that function.

03:43 As it currently stands though, if I copied exactly what was in these cells into a script file, it would fail. Let me press the play button, and it worked.

03:53 Just what is this devilry and black magic? As I mentioned, marimo determines run order based on a DAG. In creating that DAG, it has an understanding of cell interdependencies.

04:04 When you run a notebook, it runs the cells in the order determined by the DAG, not what’s on the screen. The good news is the DAG is deterministic. So although this may feel a bit weird, it is guaranteed to run in the same order every time.

04:19 Honestly, I’m not sure how I feel about this. I think I’d like notebooks to be a little more script-like. That said, this is still way better than the other notebooks, which don’t necessarily guarantee run order.

04:30 If you want to see the run order, you can press the explore dependencies button that looks like a little tree here on the left-hand side. When you open it, you see a listing of the cells as they are, but if you press the vertical tree button, you get a pretty picture.

04:46 It’s a pretty picture that’s a little hard to see. Let me just bring this up a little bit.

04:57 You can see from the picture that it has determined that cell four has to run first. Then cells zero, one, and two all have to happen before cell three. Let me just close this.

05:12 A consequence of the DAG is some things you do in a script file can’t be done in a marimo notebook. For example, when I run this, I get an error. marimo won’t let you redeclare a variable.

05:28 The error even conveniently tells you where the redefinition is so that you can find it. Note that marimo doesn’t prevent reassignment within a cell, so if you need to write some code with a counter or something, that’s fine.

05:41 You just can’t do it in a separate cell like I’ve done here. And because of the DAG, I can do something a bit strange.

05:51 Our hypotenuse value has been updated.

05:55 To my script-working brain, this is just bizarre, but because of the way the DAG works, the declaration can happen after the function call and it’ll still work.

06:05 Now to be clear, please just don’t do this. Writing your notebook cells in the order they would execute makes your notebook easier to read and maintain. All of this was just my way of showing you what’s going on under the covers.

06:19 Okay, let’s get out of the weird order world and head to something useful: embedding UI widgets in your notebook.

Become a Member to join the conversation.