Sha256: fc61155330e77891403d3d4f23082484a082891673b1d65594a796ea5a8d0b0a

Contents?: true

Size: 1.01 KB

Versions: 15

Compression:

Stored size: 1.01 KB

Contents

require 'nb_util/version'
require 'cli'
require 'json'
require 'pp'

module NbUtil
  module_function
  def getcode(argv0)
    input_filename = ARGV[1]
    ipynb = JSON.parse(File.read(input_filename))
    ipynb_filename = ARGV[2] || input_filename.gsub(/(.ipynb)$/, '')
    hash = {}
    i = 0
    ipynb["cells"].each do |k, v|
      hash[i.to_s] = k
      i += 1
    end
    for j in 0..i-1 do
      var="@hash#{j}"
      eval("#{var}={}")
      hash[j.to_s].each do |k, v|
        eval("#{var}[k] = v")
      end
    end

    flag = 0
    source_count = 0
    @getcode = ""
    for i in 0..j - 1 do
      eval("if @hash#{i}[\"cell_type\"] != \"code\" then flag = 1 end")
      if flag == 0 then
        eval("puts @getcode = @hash#{i}[\"source\"]")
        source_count = source_count + 1
        output_filename = ipynb_filename + source_count.to_s + ipynb["metadata"]["language_info"]["file_extension"]
        File.open(output_filename, 'w+') do |f|
          f.puts(@getcode)
        end
      end
      flag = 0
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nb_util-0.6.0 lib/nb_util/getcode.rb
nb_util-0.5.6 lib/nb_util/getcode.rb
nb_util-0.5.4 lib/nb_util/getcode.rb
nb_util-0.5.1 lib/nb_util/getcode.rb
nb_util-0.4.8 lib/nb_util/getcode.rb
nb_util-0.4.7 lib/nb_util/getcode.rb
nb_util-0.4.5 lib/nb_util/getcode.rb
nb_util-0.4.4 lib/nb_util/getcode.rb
nb_util-0.4.3 lib/nb_util/getcode.rb
nb_util-0.4.2 lib/nb_util/getcode.rb
nb_util-0.4.1 lib/nb_util/getcode.rb
nb_util-0.4.0 lib/nb_util/getcode.rb
nb_util-0.3.8 lib/nb_util/getcode.rb
nb_util-0.3.5 lib/nb_util/getcode.rb
nb_util-0.3.4 lib/nb_util/getcode.rb