laitimes

The big problem of JP notebook has been solved, and it is worth paying attention to

author:Aliens play Python

Preface

If you use python to do data-related work, I believe you must have used jupyter notebook, and some time ago someone complained to me that I almost lost my job because of the problem of jupyter notebook.

Something like this:

The big problem of JP notebook has been solved, and it is worth paying attention to

The second cell generates the final data, but uses the variables defined by the first cell. Coincidentally, the contents of the first cell were modified and executed, but the second cell was forgotten.

That's what I often need to remind myself to do from start to finish every time.

That's why in these environments, there is a small number in front of each cell to remind you of the order in which these cells are executed.

But that's no longer a problem now. Can you imagine that when I execute the first cell, the second cell will automatically update the contents?

The big problem of JP notebook has been solved, and it is worth paying attention to

This is Marimo, and it's not a library, it's a Python platform. It's not just about updating cell data with responsiveness. You can also write some page components, allowing you to write your own data report pages more directly.

Let's experience it.

Install and launch via pip:

pip install marimo -U && marimo tutorial intro           

If there are no accidents, you can see the message that the service starts:

The big problem of JP notebook has been solved, and it is worth paying attention to

The browser will also open the page automatically.

Since the above command is to launch the built-in template, there is already some content in it.

Let's briefly introduce some of the key points.

We need to write python code on Mariom's service page. Import it, which is later used to create page elements.

The big problem of JP notebook has been solved, and it is worth paying attention to

mo.md Write markdown content. The content is displayed above.

It's also too ordinary. Let's get something that can be interacted with

The big problem of JP notebook has been solved, and it is worth paying attention to

At first glance, it doesn't work. However, it is now possible to use the defined variable slider in other cells:

The big problem of JP notebook has been solved, and it is worth paying attention to
  • Note that slider.value accesses the value of the component

Now, dragging the slider below will show the corresponding number of 'leaves' in the upper cell.

Interesting, right? Now we no longer need to worry about some cells forgetting to execute due to personal negligence. The page is always up-to-date with the latest available results.

The big problem of JP notebook has been solved, and it is worth paying attention to

Some people will say that these are not enough to attract me, and it would be perfect if I could send them to others.

Let's take a look at a few ways to distribute it.

Turn on the button in the top right corner

The big problem of JP notebook has been solved, and it is worth paying attention to

Choosing the second option is obviously to export a static html file.

The big problem of JP notebook has been solved, and it is worth paying attention to

At this time, if the browser is opened, it can still be linked, then it is a god.

The big problem of JP notebook has been solved, and it is worth paying attention to

As you can imagine, the role of static file export is not very large. Suitable for components that are not interacted with on the page.

So, can you send the code file directly to others, and if someone else has installed Mariom, you can run it. This is certainly possible, and unlike JP Notebook, Mariom generates normal Python code files.

At the top of the page, you can see where the current code file is located:

The big problem of JP notebook has been solved, and it is worth paying attention to

Find the file and we can copy it to another place:

The big problem of JP notebook has been solved, and it is worth paying attention to
  • You can see that it's all plain python code

So how do you run an existing mariom code file?

marimo run intro.py           

Just execute the command.

The command marimo edit intro.py opens the file in an editable state

However, this is only suitable for sharing between teams. Is there any other way?

It's natural to think of publishing to the web.

Is it going to be complicated? You probably already did. In the previous example, when we run marimo, we can see a piece of information from the console:

The big problem of JP notebook has been solved, and it is worth paying attention to

It's already a web in itself.

Don't forget to triple with one click. Your likes, favorites, and followers are the driving force for my creation.