class JupyterNB::Generator

Public Class Methods

new(lang) click to toggle source

Default constructor @param lang can be either :ruby or :python3

# File lib/generator.rb, line 22
def initialize(lang)
        @cells = Array.new
        
        if ((lang == :ruby) or (lang == :python3) or (lang == :julia))
                @metadata = Metadata.new(lang)
        end
end

Public Instance Methods

add_cell(type,metadata,outputs,source) click to toggle source

Adds a content cell to the notebook to be generated @param [String] type defines the type of the cell (markdown, code, …) @param metadata can be given as a string with linebreaks or as an array of strings @param outputs can be given as a string with linebreaks or as an array of strings def add_cell(type,metadata,outputs,source) @param source can be given as a string with linebreaks or as an array of strings

# File lib/generator.rb, line 63
def add_cell(type,metadata,outputs,source)
        @cells << Cell.new(type,metadata,outputs,source)
end
generate() click to toggle source

Returns a string that contains an IPython Notebook

# File lib/generator.rb, line 31
def generate
        # start with 1 because everything is encapsulated within {}
        # at indent 0
        @indent = 1
        result = ""
        result << "{\n"

        if (@cells.nil? == false) and (@cells.size > 0)
                result << open_array("cells")

                @cells.each do |c|
                                last = false
                                (c == @cells.last) ? last = true : last = false

                                result << c.generate(@indent, last)
                end

                result << close_array
        end

        result << generate_metadata
        result << generate_versioninfo
        result << "}"

        return result
end

Private Instance Methods

generate_metadata() click to toggle source

Returns a string containing the metadata of the IPython Notebook

# File lib/generator.rb, line 72
def generate_metadata
        return @metadata.generate(@indent)
end
generate_versioninfo() click to toggle source

Returns a string that contains the version information of the IPython Notebook The values are reverse engineered from a jupyterhub project.

# File lib/generator.rb, line 79
def generate_versioninfo
        result = ""
        result << add_field("nbformat", 4)
        result << add_field("nbformat_minor", 2, true)
        return result
end