Module: JupyterNB::Helpers
Instance Method Summary collapse
-
#add_field(name, value, last = false) ⇒ Object
Returns a string that contains a single field for a notebook.
-
#add_string(str) ⇒ Object
Returns a string that contains a single automatically indented string.
-
#close_array(last = false) ⇒ Object
Returns a string that closes a group within a notebook The indentation is automatically decreased by one.
-
#close_group(last = false) ⇒ Object
Returns a string that closes a group within a notebook The indentation is automatically decreased by one.
-
#open_array(name) ⇒ Object
Returns a string that opens a group within a notebook The indentation is automatically increased by one.
-
#open_group(name = "") ⇒ Object
Returns a string that opens a group within a notebook The indentation is automatically increased by one.
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",
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.
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:
],
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:
},
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": [
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": {
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 |