Building a Starter Notebook
00:00
In the previous lesson, I gave an overview of the course. In this lesson, I’ll build a Hello World!
notebook to get you started with marimo.
00:09 A notebook is an interactive tool for running code alongside the corresponding results. It’s kind of like a REPL on steroids. A notebook has cells kind like a REPL prompt where you type in code, but it’s also able to show visual results like a graph or a DataFrame as a table.
00:26 marimo is one of the newer entries in the notebook space. In case you’re wondering about the name, the word is Japanese, which means I’m likely butchering the pronunciation, and it translates to “ball seaweed”.
00:37 The little green puffs on the right here are an example. Like most notebooks, the interface is in your browser. You run the marimo command and it launches a local server, and then attempts to launch your browser for you, pointing it at the right location.
00:52 You then use the interface in the browser to write your code and interact with the tool. While you do that, marimo keeps track of what you’re doing by saving it in a Python file, which you can then treat much like any other code, sharing it, uploading it to a repo, merging it, etc.
01:07 If you’re coming from the Jupyter world, that’s a really big deal. If you’re new to notebooks, just know that other tools typically use a data format instead, which means sharing and merging can sometimes be problematic.
01:19 Let me open up a terminal and create a new notebook.
01:23
I’ve already installed marimo in a virtual environment with pip install
. All I have to do now is run the marimo server that comes with the third-party package.
01:32 The marimo command has a bunch of subcommands, and those are used to create or edit a file. You use the edit command, passing in the name of a file to get started.
01:45 Edit works for both creating a new file and editing an existing one. Normally, marimo launches your browser for you, but through the magic of video editing, I’ve prevented that.
01:56 If something goes wrong when it attempts to launch your browser, you can simply grab this URL that it’s showing here and paste it to get started. Now let me switch to the browser.
02:08 This is the marimo interface. The heading here shows you the file that has been created. At the moment, there isn’t anything inside the notebook itself.
02:18 This is a cell. marimo provides you with an empty cell for your code. It even suggests you might want to import the marimo module. Instead, I’m going to click in the cell and type something different.
02:32 Notice here that as I type, buttons show up on the right-hand side. The play button is yellow to tell me that this cell has not been executed yet. I could run it, but let me type in some more code first.
02:50 As I type, the notebook prompts for autocomplete. Now let me run the cell. On a Mac, there’s a shortcut to run the cell: Command + Enter. If you’re not on a Mac, you can see the shortcut that works for you when you mouse over the play button.
03:05 Now that I’ve clicked play, the cell is split into two parts. The code I typed in, and the corresponding output. To get another cell, I click the plus sign on the left-hand side, which only shows up when my mouse is over the cell.
03:21 At this point, I might grumble a bit about discoverability and user interfaces, but this habit is all over the web and I should just give up that battle.
03:29 Now that I’ve got another cell, I can do what I came here for.
03:36 I hit Command + Enter that time, and you see the result immediately.
03:41 Notice at the bottom here, there are buttons for content type. If I click the Markdown button, it will automatically add a new cell using that type. Actually, it added two cells.
03:52 The first time you use a Markdown cell, the notebook automatically imports the marimo module if it isn’t already there. In the bottom right-hand corner here of the cell, you can see its type and that it’s a Markdown cell.
04:04 Let me type some Markdown.
04:11
I’m consistent at least, Hello World!
This is better than a regular old comment in your code because it’s like a little mini word processor.
04:19 It allows you to write prettier things to the screen. In a later lesson, I’ll create a dashboard using just these features. There’s another way to produce Markdown.
04:28
You can also use the md()
function in the marimo module. Let me scroll down a bit and add a cell.
04:49
The main reason to use the md()
function instead of just using a straight Markdown cell is you could embed values from your Python using things like an f-string as I’ve done here. Since I’m done, I can click the Save button over here on the right-hand side.
05:03 I don’t actually need to this time. It’s done it automatically, but it’s something to keep an eye on in case something didn’t run. Once you’ve got it saved, you use the big red button to close the service.
05:14 It’ll prompt you to make sure, and then shut it down.
05:21 Once you shut it down, you’ll notice back in the terminal that marimo shows you a little goodbye message. Let’s take a quick look at the resulting Python file. And here it is.
05:34
Like any good Python file, it imports its modules at the top. The generated_with
value tells marimo what version of the module created the file in case it gets loaded into the system again.
05:46 Then the key part to how this code works is the instantiation of the marimo app. This instance then gets used as a decorator to a nameless function for each cell.
05:56 Remember our first cell? This is it. Let me scroll down just a bit. You’ll recall that the first chunk of Markdown I wrote used the built-in Markdown cell.
06:06
Behind the scenes, the server put that inside of the md()
call, and this second chunk of Markdown is the md()
call that I wrote manually.
06:18 Okay, that’s the quick tour. Next up, I’ll use a notebook to solve a problem.
Become a Member to join the conversation.