Engee documentation
Notebook

Convert .ngscript to .jl

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

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

The script itself reads the notebook content represented in JSON format, processing two main categories of cells: markdown cells and code cells. The markdown cells are saved as comments, while code cells are written literally, providing the ability to run later. It is also possible to skip markdown cells if necessary.

In [ ]:
using JSON

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

  1. Declare a function.
  2. Convert the relative path to an absolute path.
  3. Form the output file name by replacing the source file extension with .jl.
  4. Read the contents of the JSON format file and parse it into a Julia structure.
  5. Open the output file for writing.
  6. Cycle through all the cells.
  7. If a cell is found to be of type markdown.
  8. Skip processing if the markdown cell skip flag is set.
  9. Skip markdown cells containing inserted images (![]).
  10. Write the contents of the markdown cell as 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. 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)

Examples of use

Convert script2jl.ngscript file with 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)

Convert script2jl.ngscript file with missing markdown cell.

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

Conclusion

As a result of executing this example, we got the code in the format .jl which can be attached 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 Julia programs.

Thanks to its simplicity and flexibility, the script improves software development efficiency and simplifies migration between programming environments.