How are cell IDs generated?

I would like to populate Pluto cells with content stored in an Excel file.
Copy&Paste is of course an option, but not very sensible…
Does anyone know how Pluto assigns the cell ID?
It would make my life much easier.
I could generate a plain text file, with each cell separated by an ID like # ╔═╡ 2d509cd9-2838-48c9-951e-01853b0d2dab, and then copy everything inside a template of a Pluto Notebook.
Of course, it would also be great if anyone knows how to do this with a Julia function reading the Excel file.
Any idea?

Pluto accepts any valid UUID as cell ID.
It uses uuid1 on the Julia side:

While it uses uuidv4 on the javascript side:

In any case, if you want generate a valid cell_id you can also simply use Base.UUID(rand(UInt128)).

For what concerns reading from excel, have a look at GitHub - felipenoris/XLSX.jl: Excel file reader and writer for the Julia language..
I never personally used this but it should be quite easy to simply extract values from cells

2 Likes

But IMHO in principle you also can just use any number fitting into the uuid format which does not conflict with other cell ids. After all they all are notebook-local.
Pluto itself uses 00000000-0000-0000-0000-000000000001 and 00000000-0000-0000-0000-000000000002 for the Project.toml and Manifest.toml cells.

1 Like

Yeah when I said valid I just meant that it can be created as a Base.UUID, since that is how it’s stored on the julia side.
I just believe Base.UUID(rand(UInt128)) is the easiset way to generate it without loading any other package or stdlib (as far as I know).

Edit: and it will already print the correct format when writing to a file

1 Like

@j-fu and @disberd thank you very much for your suggestions. I ended up using my own UUIDs, after checking they worked.
Regarding XLSX.jl, I am used to it for importing data into DataFrames. But my need right now is of other sort. I am making a test for my students. The questions bank is stored in an Excel workbook. I have a column where for each question I have the markdown code ready to import into Pluto. What I want is to populate cells with the elements in that column. But I don’t need to store the information in any object to use later. I.e., I don’t want to “read” the Excel file and store the content into an object. I want the content to become the full content of several Pluto Notebook cells.
My workaround solution is as follows: I now have an Excel formula that generates my UUID before the markdown code already implemented for each question. Then I create an empty Pluto Notebook and with a single Copy&Paste I am able to get what I want.
In any case, if anyone has a clever solution, please be my guest.

I though you could simply write a julia script that reads the cell contents and write them directly to a file representing the pluto notebook.
That way you can generate the UUIDs directly in julia during the serialization of the contents and don’t have to manually copy paste.

Edit: You can have a look at how Pluto itselfs generates the notebook file from Julia for easily replicating that by just changing what you put in the cell contents with what you read from excel.

Pluto.jl/saving and loading.jl at main · fonsp/Pluto.jl · GitHub

1 Like

Excelent @disberd ! Thank you. I will try to take a look at these functions during the summer and write my own script. Namely the function save_notebook(). Do you recommend that I use something like:

import Pluto: save_notebook

Or just copy and paste the contents of the function into my own code for generating an object with the cell’s content?

I’d say neither.
I guess you wouldn’t need advanced features of Pluto like the PackageManager or specific cell metadata (since you say you are only allowing markdown code in the excel cells).

If that is the case, the specific function within Pluto is a bit of an overkill, importing it directly from Pluto would increase your load time by having a dependency on Pluto and would also require you to first create a Notebook object (still from Pluto) in order to use the function.

Similarly if you just copy past the function in your own package it won’t work.

For just serializing cells you can look at the specific function signature save_notebook(io::IO, notebook::Notebook) starting at line 32, and simply remove the parts related to metadata and pkgmanager, which is more than half of the function.

Then you’d have to make your own version of it (without requiring to provide an object of type Notebook as second argument), but you just maybe provide the excel file to read or something of the sort. And then in the loop that goes through the code of each cell and write it to the IO you simply do the reading of excel cells and write that to the file

Perfect! Thank you @disberd. With these tips, you made my life easier.