Engee documentation
Notebook

Conversion .ngscript in .jl

This example implements a simple conversion utility. .ngscript to files with the extension .jl designed for direct execution in the Julia terminal.

The main task of the script is to extract the contents of laptop cells (including both markdown text and executable code) and save it in a readable format.

The script itself reads the contents of the notebook, presented in JSON format, processing two main categories of cells: markdown type cells and code type cells. Markdown cells are saved as comments, while code cells are written literally, enabling a subsequent run. It is also possible to skip markdown cells if necessary.

In [ ]:
using JSON

Consider the function itself. It takes the path to the input file and an optional argument. skip_markdown, which determines whether to skip markdown type cells during conversion.
Now let's move on to the algorithm and consider it line by line.

  1. We declare the function.
  2. Transform the relative path to the absolute one.
  3. We form the name of the output file by replacing the extension of the source file with .jl.
  4. We read the contents of the JSON file and parse it into the Julia structure.
  5. Open the output file for recording.
  6. Loop through all cells.
  7. If it is found that the cell belongs to the markdown type.
  8. Skip processing if the markdown cells skip flag is set.
  9. Skip markdown cells containing inserted images (![]).
  10. We record the contents of the markdown cell in the form of a Julia comment (#=...=#
  11. If the cell is a code and contains a non-zero number of rows.
  12. Write the lines of code directly to the file, preserving their order.
  13. The end.
In [ ]:
function script2jl(input_path::String; skip_markdown=false)
    input_file = abspath(input_path)
    output_file = replace(input_file, r"\.\w+$" => ".jl")
    notebook = JSON.parsefile(input_file)
    open(output_file, "w") do f
        for cell in notebook["cells"]
            if cell["cell_type"] == "markdown"
                skip_markdown && continue
                any(startswith(l, "![") for l in cell["source"]) && continue
                println(f, "#= ", join(cell["source"]), " =#\n")
            elseif cell["cell_type"] == "code" && !isempty(cell["source"])
                println(f, join(cell["source"]), "\n")
            end
        end
    end
    println("Конвертация ngscript в jl завершена.")
end
Out[0]:
script2jl (generic function with 1 method)

Usage examples

Converting a script2jl.ngscript file with an absolute path without skipping the markdown cell.

In [ ]:
@time script2jl("$(@__DIR__)/script2jl.ngscript")
Конвертация ngscript в jl завершена.
  0.069372 seconds (14.57 k allocations: 1.258 MiB, 95.24% compilation time)

Converting a script2jl.ngscript file with a markdown cell omitted.

In [ ]:
@time script2jl("script2jl.ngscript", skip_markdown=true)
Конвертация ngscript в jl завершена.
  0.002050 seconds (740 allocations: 345.977 KiB)

Conclusion

Based on the results of this example, we received the code in the format .jl which can be connected to any project via the command include("$(@__DIR__)/script2jl.jl").
image.png

This tool is useful for developers who use .ngscript for experimentation and prototyping. It allows you to easily transfer working code into full-fledged programs in the Julia language.

Due to its simplicity and flexibility, the script increases the efficiency of software development and simplifies migration between programming environments.