Module: JupyterNB::Helpers

Included in:
Cell, Generator, Metadata
Defined in:
lib/helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, value, last = false) ⇒ Object

Returns a string that contains a single field for a notebook

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

"cell_type": "code",

Parameters:

  • name (String)

    Name of the field (e.g. cell_type)

  • value

    can be a String or Integer

  • last (Boolean) (defaults to: false)

    if set to true, the trailing ‘,’ is omitted



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

Returns a string that contains a single automatically indented string

This is used to output source code within cells.

Parameters:

  • str (String)

    contains the string to be indented and contained in “”



12
13
14
15
16
17
# 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) ⇒ Object

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:

],

Parameters:

  • last (Boolean) (defaults to: false)

    if set to true, the trailing ‘,’ is omitted



109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object

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:

},

Parameters:

  • last (Boolean) (defaults to: false)

    if set to true, the trailing ‘,’ is omitted



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

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": [

Parameters:

  • name (String)

    Name of the array (e.g. cells)



91
92
93
94
95
96
97
98
99
100
# 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 = "") ⇒ Object

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": {

Parameters:

  • name (String) (defaults to: "")

    Name of the group (e.g. metadata)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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