jupyternb 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebfbba6b7604c84fa2c6ae94f613cfff2858668a
4
+ data.tar.gz: 9e7b56500cce2959b712c5773e95e89f145d9687
5
+ SHA512:
6
+ metadata.gz: fa0e4011c768c38be623ebd3f2d9415761fcd724b056f3c213c124d623f841e2ea6a8bf3102e21139c55ac975dd7a4f6438b5732ac5aa16b834fb37585f0a1f0
7
+ data.tar.gz: 6886f076a095aa598e29fe9e73de2f15cf415e494db5a5af8f5f56ab8c16f7a13d8a25e91052bba527f71f8a9b93c58a4a9d3ee8e4b9406254b5ac6c473f2e90
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ doc
2
+ Gemfile.lock
3
+ *.gem
4
+ test
5
+ *.swp
6
+ .yardoc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Hermann Detz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ <Copyright (C) 2018 Hermann Detz>
2
+
3
+ <This software may be modified and distributed under the terms>
4
+ <of the MIT license. See the LICENSE file for details.>
5
+
6
+ # jupyternb
7
+
8
+ This gem provides useful functions to generate Jupyter Notebooks,
9
+ (formerly IPython Notebooks), from Ruby scripts.
10
+
11
+ ## Setup
12
+
13
+ Add the gem to your Gemfile:
14
+
15
+ gem 'jupyternb', :path => "path-to/jupyternb"
16
+
17
+ Update your gem list:
18
+
19
+ $ bundle install
20
+
21
+ ## Usage
22
+
23
+ Currently, the only functionality is to generate .ipynb files.
24
+ A simple example is given in the following:
25
+
26
+ ```ruby
27
+ module JupyterNB
28
+
29
+ # Create a Jupyter Notebook Generator
30
+ # The parameter defines the kernel used. Present options are :ruby or :python3.
31
+ gen = Generator.new(:ruby)
32
+
33
+ # Add some content cells (either multi-line strings or arrays of strings)
34
+ gen.add_cell("markdown", "", "", "# Test header\nsome text\n## Second Header\nmore text")
35
+ gen.add_cell("code", "", "", ["puts \"Hello World!\"","# Do something useful here"])
36
+
37
+ # Simply print the output to the terminal
38
+ puts gen.generate
39
+
40
+ end
41
+ ```
42
+
43
+ ## Feedback & Contributions
44
+
45
+ 1. Fork it ( http://github.com/hermanndetz/middleman-gnuplot/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
50
+
data/jupyternb.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require_relative 'lib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jupyternb"
8
+ spec.version = JupyterNB::VERSION
9
+ spec.authors = ["Hermann Detz"]
10
+ spec.email = ["hermann.detz@gmail.com"]
11
+ spec.description = <<-EOF
12
+ This gem provides useful functions to work with
13
+ Jupyter Notebook files.
14
+ EOF
15
+ spec.summary = %q{Jupyter Notebook Tools}
16
+ spec.homepage = "https://github.com/hermanndetz/jupyternb"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+ end
24
+
data/lib/cell.rb ADDED
@@ -0,0 +1,96 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ require "helpers"
7
+
8
+ module JupyterNB
9
+ class Cell
10
+ # Cell type e.g. "code" or "markdown"
11
+ @type = ""
12
+
13
+ # Array of strings (lines) for metadata
14
+ @metadata = []
15
+
16
+ # Array of strings (lines) for source
17
+ @source = []
18
+
19
+ # Used in generate function, defined here because
20
+ # needed by Helpers mixin
21
+ @indent = 1
22
+
23
+ # Execution count of code cell (hardcoded to 0 because it's just generated)
24
+ @count = 0
25
+
26
+ # Constructor
27
+ # @param [String] type defines the type of the cell (markdown, code, ...)
28
+ # @param metadata can be given as a string with linebreaks or as an array of strings
29
+ # @param outputs can be given as a string with linebreaks or as an array of strings def add_cell(type,metadata,outputs,source)
30
+ # @param source can be given as a string with linebreaks or as an array of strings
31
+ def initialize(type, metadata, outputs, source)
32
+ @type = type
33
+ @metadata = read_parameter(metadata)
34
+ @source = read_parameter(source)
35
+ @count = 0
36
+ end
37
+
38
+ # Returns a string that contains an IPython Notebook cell
39
+ #
40
+ # The outputs field is not implemented yet. Should actually not be
41
+ # necessary for a pure generator.
42
+ # Generation of metadata within the cell is also not yet implemented.
43
+ #
44
+ # @param [Integer] indent defines the indentation of the cell content
45
+ # @param [Boolean] last if set to true, the trailing ',' is omitted
46
+ def generate(indent=1,last=false)
47
+ @indent = indent
48
+
49
+ result = ""
50
+ result << open_group
51
+ result << add_field("cell_type", @type)
52
+ result << add_field("execution_count", @count) if (@type == "code")
53
+ result << open_group("metadata")
54
+ # not implemented yet
55
+ result << close_group
56
+
57
+ result << open_array("source")
58
+ @source.each do |l|
59
+ result << add_string(l)
60
+
61
+ (l == @source.last) ? result << "\n" : result << ",\n"
62
+ end
63
+ result << close_array(true)
64
+ result << close_group(last)
65
+
66
+ return result
67
+ end
68
+
69
+ private
70
+
71
+ # Returns an array of strings for multi-line parameters like metadata or source
72
+ # @param param can be given as a string with linebreaks or as an array of strings
73
+ def read_parameter (param)
74
+ result = []
75
+
76
+ unless (param.nil? == true)
77
+ if param.is_a?(String)
78
+ param.each_line do |l|
79
+ result << l.chomp
80
+ end
81
+ elsif param.is_a?(Array)
82
+ param.each do |l|
83
+ next unless l.is_a?(String)
84
+
85
+ result << l
86
+ end
87
+ end
88
+ end
89
+
90
+ return result
91
+ end
92
+
93
+ include JupyterNB::Helpers
94
+ end
95
+ end
96
+
data/lib/generator.rb ADDED
@@ -0,0 +1,87 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ require "cell"
7
+ require "metadata"
8
+
9
+ module JupyterNB
10
+ class Generator
11
+ # Array of cells (markdown, code, ...) that should be generated within the notebook
12
+ @cells = []
13
+
14
+ # Defines the indent for output lines (used by Helpers module)
15
+ @indent = 0
16
+
17
+ # Stores a metadata object
18
+ @metadata = nil
19
+
20
+ # Default constructor
21
+ # @param lang can be either :ruby or :python3
22
+ def initialize(lang)
23
+ @cells = Array.new
24
+
25
+ if ((lang == :ruby) or (lang == :python3))
26
+ @metadata = Metadata.new(lang)
27
+ end
28
+ end
29
+
30
+ # Returns a string that contains an IPython Notebook
31
+ def generate
32
+ # start with 1 because everything is encapsulated within {}
33
+ # at indent 0
34
+ @indent = 1
35
+ result = ""
36
+ result << "{\n"
37
+
38
+ if (@cells.nil? == false) and (@cells.size > 0)
39
+ result << open_array("cells")
40
+
41
+ @cells.each do |c|
42
+ last = false
43
+ (c == @cells.last) ? last = true : last = false
44
+
45
+ result << c.generate(@indent, last)
46
+ end
47
+
48
+ result << close_array
49
+ end
50
+
51
+ result << generate_metadata
52
+ result << generate_versioninfo
53
+ result << "}"
54
+
55
+ return result
56
+ end
57
+
58
+ # Adds a content cell to the notebook to be generated
59
+ # @param [String] type defines the type of the cell (markdown, code, ...)
60
+ # @param metadata can be given as a string with linebreaks or as an array of strings
61
+ # @param outputs can be given as a string with linebreaks or as an array of strings def add_cell(type,metadata,outputs,source)
62
+ # @param source can be given as a string with linebreaks or as an array of strings
63
+ def add_cell(type,metadata,outputs,source)
64
+ @cells << Cell.new(type,metadata,outputs,source)
65
+ end
66
+
67
+ private
68
+
69
+ include JupyterNB::Helpers
70
+
71
+ # Returns a string containing the metadata of the IPython Notebook
72
+ def generate_metadata
73
+ return @metadata.generate(@indent)
74
+ end
75
+
76
+ # Returns a string that contains the version information of the
77
+ # IPython Notebook
78
+ # The values are reverse engineered from a jupyterhub project.
79
+ def generate_versioninfo
80
+ result = ""
81
+ result << add_field("nbformat", 4)
82
+ result << add_field("nbformat_minor", 2, true)
83
+ return result
84
+ end
85
+ end
86
+ end
87
+
data/lib/helpers.rb ADDED
@@ -0,0 +1,123 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ module JupyterNB
7
+ module Helpers
8
+ # Returns a string that contains a single automatically indented string
9
+ #
10
+ # This is used to output source code within cells.
11
+ # @param [String] str contains the string to be indented and contained in ""
12
+ def add_string(str)
13
+ line = ""
14
+ @indent.times do line << " " end
15
+ line << "\"#{str}\\n\""
16
+ return line
17
+ end
18
+
19
+ # Returns a string that contains a single field for a notebook
20
+ #
21
+ # This is e.g. used to output the cell type:
22
+ # "cell_type": "code",
23
+ #
24
+ # @param [String] name Name of the field (e.g. cell_type)
25
+ # @param value can be a String or Integer
26
+ # @param [Boolean] last if set to true, the trailing ',' is omitted
27
+ def add_field(name, value, last=false)
28
+ line = ""
29
+ @indent.times do line << " " end
30
+
31
+ line << "\"#{name}\": "
32
+
33
+ line << "\"#{value}\"" if value.is_a?(String)
34
+ line << "#{value}" if value.is_a?(Integer)
35
+
36
+ last ? line << "\n" : line << ",\n"
37
+
38
+ return line
39
+ end
40
+
41
+ # Returns a string that opens a group within a notebook
42
+ # The indentation is automatically increased by one.
43
+ #
44
+ # This is e.g. used to output the metadata:
45
+ # "metadata": {
46
+ #
47
+ # @param [String] name Name of the group (e.g. metadata)
48
+ def open_group(name="")
49
+ line = ""
50
+ @indent.times do line << " " end
51
+
52
+ if name != ""
53
+ line << "\"#{name}\": {\n"
54
+ else
55
+ line << "{\n"
56
+ end
57
+
58
+ @indent += 1
59
+
60
+ return line
61
+ end
62
+
63
+ # Returns a string that closes a group within a notebook
64
+ # The indentation is automatically decreased by one.
65
+ #
66
+ # This is e.g. used to close the metadata:
67
+ # },
68
+ #
69
+ # @param [Boolean] last if set to true, the trailing ',' is omitted
70
+ def close_group(last=false)
71
+ # decrease indent first to bring closing bracked to the
72
+ # same indent as opening bracket
73
+ @indent -= 1
74
+
75
+ line = ""
76
+ @indent.times do line << " " end
77
+
78
+ line << "}"
79
+ last ? line << "\n" : line << ",\n"
80
+
81
+ return line
82
+ end
83
+
84
+ # Returns a string that opens a group within a notebook
85
+ # The indentation is automatically increased by one.
86
+ #
87
+ # This is e.g. used to output the cells:
88
+ # "cells": [
89
+ #
90
+ # @param [String] name Name of the array (e.g. cells)
91
+ def open_array(name)
92
+ line = ""
93
+ @indent.times do line << " " end
94
+
95
+ line << "\"#{name}\": [\n"
96
+
97
+ @indent += 1
98
+
99
+ return line
100
+ end
101
+
102
+ # Returns a string that closes a group within a notebook
103
+ # The indentation is automatically decreased by one.
104
+ #
105
+ # This is e.g. used to close the metadata:
106
+ # ],
107
+ #
108
+ # @param [Boolean] last if set to true, the trailing ',' is omitted
109
+ def close_array(last=false)
110
+ # decrease indent first to bring closing bracked to the
111
+ # same indent as opening bracket
112
+ @indent -= 1
113
+
114
+ line = ""
115
+ @indent.times do line << " " end
116
+
117
+ line << "]"
118
+ last ? line << "\n" : line << ",\n"
119
+
120
+ return line
121
+ end
122
+ end
123
+ end
data/lib/jupyternb.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ require "generator"
data/lib/metadata.rb ADDED
@@ -0,0 +1,86 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ module JupyterNB
7
+ class Metadata
8
+ # Defines the indent for output lines (used by Helpers module)
9
+ @indent = 0
10
+ @kernel = {}
11
+ @langinfo = {}
12
+
13
+ # Constructor
14
+ # @param lang can be either :ruby or :python3
15
+ def initialize(lang)
16
+ @kernel = {}
17
+ @langinfo = {}
18
+
19
+ case lang
20
+ when :ruby then initialize_ruby
21
+ when :python3 then initialize_python3
22
+ end
23
+ end
24
+
25
+ # Returns a string containing the metadata of the IPython Notebook
26
+ # @param [Integer] indent defines the indentation of the generated output.
27
+ def generate(indent=0)
28
+ @indent = indent
29
+
30
+ result = ""
31
+ result << open_group("metadata")
32
+ result << open_group("kernelspec")
33
+ result << add_field("display_name", @kernel[:displayname])
34
+ result << add_field("language", @kernel[:language])
35
+ result << add_field("name", @kernel[:name], true)
36
+ result << close_group
37
+ result << open_group("language_info")
38
+ result << add_field("file_extension", @langinfo[:fileext])
39
+ result << add_field("mimetype", @langinfo[:mime])
40
+ result << add_field("name", @langinfo[:name])
41
+ result << add_field("version", @langinfo[:version], true)
42
+ result << close_group(true)
43
+ result << close_group
44
+ return result
45
+ end
46
+
47
+
48
+ private
49
+
50
+ include JupyterNB::Helpers
51
+
52
+ # Initialize metadata for Ruby kernel
53
+ def initialize_ruby
54
+ @kernel[:language] = "ruby"
55
+ @kernel[:name] = "ruby"
56
+ @kernel[:displayname] = "Ruby #{RUBY_VERSION}"
57
+
58
+ @langinfo[:name] = "ruby"
59
+ @langinfo[:fileext] = ".rb"
60
+ @langinfo[:mime] = "application/x-ruby"
61
+ @langinfo[:version] = RUBY_VErSION
62
+ end
63
+
64
+ # Initialize metadata for Python 3 kernel
65
+ def initialize_python3
66
+ @kernel[:language] = "python"
67
+ @kernel[:name] = "python3"
68
+ @kernel[:displayname] = "Python 3"
69
+
70
+ @langinfo[:name] = "python"
71
+ @langinfo[:fileext] = ".py"
72
+ @langinfo[:mime] = "text/x-python"
73
+
74
+ # Check for python3
75
+ `which python3`
76
+ if $?.success?
77
+ @langinfo[:version] = `python3 -V`.split(' ').last
78
+ else
79
+ # Fall back to initial 3.0 release, if no python3 executable is found
80
+ @langinfo[:version] = '3.0'
81
+ end
82
+ end
83
+
84
+ end
85
+ end
86
+
data/lib/version.rb ADDED
@@ -0,0 +1,10 @@
1
+ # Copyright (C) 2018 Hermann Detz
2
+ #
3
+ # This software may be modified and distributed under the terms
4
+ # of the MIT license. See the LICENSE file for details.
5
+
6
+ module JupyterNB
7
+ VERSION = "0.1.1"
8
+ end
9
+
10
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jupyternb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Hermann Detz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " This gem provides useful functions to work with\n\t\t\t\t\t\t\t\t\t\t\t
14
+ Jupyter Notebook files.\n"
15
+ email:
16
+ - hermann.detz@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - LICENSE
23
+ - README.md
24
+ - jupyternb.gemspec
25
+ - lib/cell.rb
26
+ - lib/generator.rb
27
+ - lib/helpers.rb
28
+ - lib/jupyternb.rb
29
+ - lib/metadata.rb
30
+ - lib/version.rb
31
+ homepage: https://github.com/hermanndetz/jupyternb
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.5.2.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Jupyter Notebook Tools
55
+ test_files: []