Loading video player…

Examining a Sample Problem

00:00 In the previous lesson, I gave you a quick tour of a marimo notebook. In this lesson, I’ll show you a more realistic problem, something you might actually do with marimo and NumPy together.

00:11 Notebooks generally get used for experimentation and quick problem solving, especially when visualization is helpful, but I’ll come back to that in a later lesson.

00:21 They tend to have less code in them than a full-size program, but they don’t have to be one-off scripts. If you structure them well, you can always bring them back up, change a few variables, and get a new answer.

00:32 Consider the math problem of solving these two equations. Let’s create a notebook that does this. At the top of the notebook, I’m going to put some Markdown documenting what I’m doing, and then inside a Python cell or two, I’ll use the solve method in NumPy’s linalg module to get a solution.

00:49 With a little more Markdown, I can print out a result. If you’re coding along, start marimo with a new file and head to your browser to write the notebook.

00:59 I’m calling mine sim_eq.py.

01:04 Here I’m in the new notebook that I called sim_eq.py. Seeing as I know I’m going to use the marimo module, this time, I’m going to click in the cell and tab complete the suggested import.

01:15 I’m going to make sure to run the cell so that the import happens. This is particularly helpful because it means the things that have been imported are available to the autocomplete.

01:26 Now, I’ll add some Markdown.

01:40 marimo supports one of the richer variations on Markdown. I’m not sure if it’s a hundred percent GitHub Markdown compatible, but it does support using LaTeX math equations like GitHub Markdown.

01:51 As an aside, all the way through grad school, I and every prof I worked with, pronounced LaTeX as latex as that’s the way it’s spelled, and wasn’t until recently I learned I was wrong all these years.

02:02 If you’re not familiar, LaTeX is a typesetting tool created by Leslie Lamport, which is built on top of TeX, a similar typesetting tool created by Donald Knuth.

02:12 The TeX part is an acronym for Greek letters, meaning skill or technique because, well, this came out of academia and so of course it does. Anyhow, you can use LaTeX inside of Markdown to denote math equations and make your output look professional.

02:27 To write these kinds of statements, you surround them in dollar signs.

02:41 See? Pretty math. Not too important with simple linear equations like this, but if you have functions, square roots, integrals or the like, this is the way to go.

02:49 Now that I’ve got a bit of documentation, I’ll write some Python to actually solve the equations. Let me add a cell.

03:01 Normally here, I might just keep writing code, but as I mentioned, the autocomplete feature only sees what is in the environment. Since the import doesn’t happen until I run the cell, it’s good practice to run the cell so that I can do less typing later. Command + Enter. The equation solver in NumPy that I’m going to use is called solve, and it takes two NumPy arrays.

03:22 The first argument is an array of arrays containing the coefficients of the two equations, and the second argument is an array of values representing the right-hand side of the equations.

03:33 Scroll down a bit and add a cell.

04:05 See how the type ahead is helpful. Now, Command + Enter, and nothing. There’s no result being displayed here because I haven’t asked it to do so. I’m going to add another cell,

04:29 and this time I can see the results. Normally in a notebook, you don’t use print(), you just use some Markdown instead. But I’m looking into the future knowing that this line is going to be useful in a later lesson, so I’m going to keep it here.

04:43 Let’s also do this the more normal way,

05:04 and there you go. A prettier version of the same content. Now that I have a notebook, I can use it to solve different equations just by editing the values in the cells.

05:13 Let me scroll back up here,

05:27 and now I’ll rerun the cells. Scrolling back down, and you can see the new answers. Note how both the print() and the f-string Markdown got updated.

05:38 marimo tracks the relationships between the cells, and knows to refresh those that are dependent on the changes I made.

05:47 You’ve just seen how marimo handles changes. It isn’t quite the same as a regular Python script, and it’s definitely different from Jupyter. In the next lesson, I’ll do things out of order so that you can see just how marimo calculates cell dependencies.

Become a Member to join the conversation.