Class: JupyterNB::Generator

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/generator.rb

Instance Method Summary collapse

Methods included from Helpers

#add_field, #add_string, #close_array, #close_group, #open_array, #open_group

Constructor Details

#initialize(lang) ⇒ Generator

Default constructor

Parameters:

  • lang

    can be either :ruby or :python3



22
23
24
25
26
27
28
# File 'lib/generator.rb', line 22

def initialize(lang)
  @cells = Array.new
  
  if ((lang == :ruby) or (lang == :python3) or (lang == :julia))
     = .new(lang)
  end
end

Instance Method Details

#add_cell(type, metadata, outputs, source) ⇒ Object

Adds a content cell to the notebook to be generated

Parameters:

  • type (String)

    defines the type of the cell (markdown, code, …)

  • metadata

    can be given as a string with linebreaks or as an array of strings

  • outputs

    can be given as a string with linebreaks or as an array of strings def add_cell(type,metadata,outputs,source)

  • source

    can be given as a string with linebreaks or as an array of strings



63
64
65
# File 'lib/generator.rb', line 63

def add_cell(type,,outputs,source)
  @cells << Cell.new(type,,outputs,source)
end

#generateObject

Returns a string that contains an IPython Notebook



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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 << 
  result << generate_versioninfo
  result << "}"

  return result
end