module JupyterNB::Helpers

Public Instance Methods

add_field(name, value, last=false) click to toggle source

Returns a string that contains a single field for a notebook

This is e.g. used to output the cell type:

"cell_type": "code",

@param [String] name Name of the field (e.g. cell_type) @param value can be a String or Integer @param [Boolean] last if set to true, the trailing ',' is omitted

# File lib/helpers.rb, line 27
def add_field(name, value, last=false)
        line = ""
        @indent.times do line << " " end

        line << "\"#{name}\": "
        
        line << "\"#{value}\"" if value.is_a?(String)
        line << "#{value}" if value.is_a?(Integer)
        
        last ? line << "\n" : line << ",\n"

        return line
end
add_string(str) click to toggle source

Returns a string that contains a single automatically indented string

This is used to output source code within cells. @param [String] str contains the string to be indented and contained in “”

# File lib/helpers.rb, line 12
def add_string(str)
        line = ""
        @indent.times do line << " " end
        line << "\"#{str}\\n\""
        return line
end
close_array(last=false) click to toggle source

Returns a string that closes a group within a notebook The indentation is automatically decreased by one.

This is e.g. used to close the metadata:

],

@param [Boolean] last if set to true, the trailing ',' is omitted

# File lib/helpers.rb, line 109
def close_array(last=false)
        # decrease indent first to bring closing bracked to the
        # same indent as opening bracket
        @indent -= 1

        line = ""
        @indent.times do line << " " end

        line << "]"
        last ? line << "\n" : line << ",\n"

        return line
end
close_group(last=false) click to toggle source

Returns a string that closes a group within a notebook The indentation is automatically decreased by one.

This is e.g. used to close the metadata:

},

@param [Boolean] last if set to true, the trailing ',' is omitted

# File lib/helpers.rb, line 70
def close_group(last=false)
        # decrease indent first to bring closing bracked to the
        # same indent as opening bracket
        @indent -= 1

        line = ""
        @indent.times do line << " " end

        line << "}"
        last ? line << "\n" : line << ",\n"

        return line
end
open_array(name) click to toggle source

Returns a string that opens a group within a notebook The indentation is automatically increased by one.

This is e.g. used to output the cells:

"cells": [

@param [String] name Name of the array (e.g. cells)

# File lib/helpers.rb, line 91
def open_array(name)
        line = ""
        @indent.times do line << " " end

        line << "\"#{name}\": [\n"

        @indent += 1

        return line
end
open_group(name="") click to toggle source

Returns a string that opens a group within a notebook The indentation is automatically increased by one.

This is e.g. used to output the metadata:

"metadata": {

@param [String] name Name of the group (e.g. metadata)

# File lib/helpers.rb, line 48
def open_group(name="")
        line = ""
        @indent.times do line << " " end

        if name != ""
                line << "\"#{name}\": {\n"
        else
                line << "{\n"
        end

        @indent += 1

        return line
end