ipynbdiff 0.4.4 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -7
- data/lib/ipynb_symbol_map.rb +24 -5
- data/lib/ipynbdiff.rb +4 -4
- data/lib/output_transformer.rb +10 -6
- data/lib/transformer.rb +3 -2
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09eee5bd0bb92f76cdabc67bd9b4ac1050756f952f9b5a3b91aaef984f860b87'
|
4
|
+
data.tar.gz: d89a1289023f1c44af159847a91f9139746a9c0846de169494f6438917754ca4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24b0b3fa46237a5b2fff8ef253b1dc2b90d5c8650c406097dd86e525e3e8bd5d89568b9280897d85c88444eb993ce3f490b15b0c741dd57dbea6e227dce2abea
|
7
|
+
data.tar.gz: a61bedd1f3b227f1b06dc97ed40ba19136fe4b157b9c0767887bbf22ee036bbbdfb21db3311c4b07a0e7ba39310f4cf8abd37dc8e7973c2035462478428bdd75
|
data/README.md
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
# IpynbDiff: Better diff for Jupyter Notebooks
|
2
2
|
|
3
|
-
This is a simple diff tool that cleans up
|
3
|
+
This is a simple diff tool that cleans up Jupyter notebooks, transforming each [notebook](example/1/from.ipynb)
|
4
4
|
into a [readable markdown file](example/1/from_html.md), keeping the output of cells, and running the
|
5
5
|
diff after. Markdowns are generated using an opinionated Jupyter to Markdown conversion. This means
|
6
6
|
that the entire file is readable on the diff.
|
7
7
|
|
8
8
|
The result are diffs that are much easier to read:
|
9
9
|
|
10
|
-
| Diff
|
11
|
-
|
|
12
|
-
| [
|
13
|
-
|  |
|
10
|
+
| Diff | IpynbDiff |
|
11
|
+
| ----------------------------------- | ----------------------------------------------------- |
|
12
|
+
| [Diff text](example/diff.txt) | [IpynbDiff text](example/ipynbdiff_percent.txt) |
|
13
|
+
|  |  |
|
14
14
|
|
15
|
-
|
16
|
-
This started as a port of This is a port of [ipynbdiff](https://gitlab.com/gitlab-org/incubation-engineering/mlops/ipynbdiff),
|
15
|
+
This started as a port of [ipynbdiff](https://gitlab.com/gitlab-org/incubation-engineering/mlops/poc/ipynbdiff),
|
17
16
|
but now has extended functionality although not working as git driver.
|
18
17
|
|
19
18
|
## Usage
|
data/lib/ipynb_symbol_map.rb
CHANGED
@@ -27,6 +27,8 @@ module IpynbDiff
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def parse(prefix = '.')
|
30
|
+
raise_if_file_ended
|
31
|
+
|
30
32
|
skip_whitespaces
|
31
33
|
|
32
34
|
if (c = current_char) == '"'
|
@@ -50,6 +52,8 @@ module IpynbDiff
|
|
50
52
|
current_should_be '['
|
51
53
|
|
52
54
|
loop do
|
55
|
+
raise_if_file_ended
|
56
|
+
|
53
57
|
break if skip_beginning(']')
|
54
58
|
|
55
59
|
new_prefix = "#{prefix}.#{i}"
|
@@ -68,6 +72,8 @@ module IpynbDiff
|
|
68
72
|
current_should_be '{'
|
69
73
|
|
70
74
|
loop do
|
75
|
+
raise_if_file_ended
|
76
|
+
|
71
77
|
break if skip_beginning('}')
|
72
78
|
|
73
79
|
prop_name = parse_string(return_value: true)
|
@@ -97,9 +103,9 @@ module IpynbDiff
|
|
97
103
|
loop do
|
98
104
|
increment_char_index
|
99
105
|
|
100
|
-
|
106
|
+
raise_if_file_ended
|
101
107
|
|
102
|
-
if current_char == '"' &&
|
108
|
+
if current_char == '"' && !prev_backslash?
|
103
109
|
init_idx += 1
|
104
110
|
break
|
105
111
|
end
|
@@ -113,11 +119,12 @@ module IpynbDiff
|
|
113
119
|
end
|
114
120
|
|
115
121
|
def parse_value
|
116
|
-
increment_char_index until VALUE_STOPPERS.include?(current_char)
|
122
|
+
increment_char_index until raise_if_file_ended || VALUE_STOPPERS.include?(current_char)
|
117
123
|
end
|
118
124
|
|
119
125
|
def skip_whitespaces
|
120
126
|
while WHITESPACE_CHARS.include?(current_char)
|
127
|
+
raise_if_file_ended
|
121
128
|
check_for_new_line
|
122
129
|
increment_char_index
|
123
130
|
end
|
@@ -133,11 +140,13 @@ module IpynbDiff
|
|
133
140
|
end
|
134
141
|
|
135
142
|
def current_char
|
143
|
+
raise_if_file_ended
|
144
|
+
|
136
145
|
@chars[@char_idx]
|
137
146
|
end
|
138
147
|
|
139
|
-
def
|
140
|
-
@chars[@char_idx - 1]
|
148
|
+
def prev_backslash?
|
149
|
+
@chars[@char_idx - 1] == '\\' && @chars[@char_idx - 2] != '\\'
|
141
150
|
end
|
142
151
|
|
143
152
|
def current_should_be(another_char)
|
@@ -148,7 +157,13 @@ module IpynbDiff
|
|
148
157
|
@current_line += 1 if current_char == "\n"
|
149
158
|
end
|
150
159
|
|
160
|
+
def raise_if_file_ended
|
161
|
+
@char_idx >= @chars.size && raise(InvalidTokenError)
|
162
|
+
end
|
163
|
+
|
151
164
|
def skip
|
165
|
+
raise_if_file_ended
|
166
|
+
|
152
167
|
skip_whitespaces
|
153
168
|
|
154
169
|
if (c = current_char) == '"'
|
@@ -164,6 +179,8 @@ module IpynbDiff
|
|
164
179
|
|
165
180
|
def skip_array
|
166
181
|
loop do
|
182
|
+
raise_if_file_ended
|
183
|
+
|
167
184
|
break if skip_beginning(']')
|
168
185
|
|
169
186
|
skip
|
@@ -173,6 +190,8 @@ module IpynbDiff
|
|
173
190
|
def skip_object
|
174
191
|
|
175
192
|
loop do
|
193
|
+
raise_if_file_ended
|
194
|
+
|
176
195
|
break if skip_beginning('}')
|
177
196
|
|
178
197
|
parse_string
|
data/lib/ipynbdiff.rb
CHANGED
@@ -5,18 +5,18 @@ module IpynbDiff
|
|
5
5
|
require 'transformer'
|
6
6
|
require 'diff'
|
7
7
|
|
8
|
-
def self.diff(from, to, raise_if_invalid_nb: false, include_frontmatter: false, diffy_opts: {})
|
9
|
-
transformer = Transformer.new(include_frontmatter: include_frontmatter)
|
8
|
+
def self.diff(from, to, raise_if_invalid_nb: false, include_frontmatter: false, hide_images: false, diffy_opts: {})
|
9
|
+
transformer = Transformer.new(include_frontmatter: include_frontmatter, hide_images: hide_images)
|
10
10
|
|
11
11
|
Diff.new(transformer.transform(from), transformer.transform(to), diffy_opts)
|
12
12
|
rescue InvalidNotebookError
|
13
13
|
raise if raise_if_invalid_nb
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.transform(notebook, raise_errors: false, include_frontmatter: true)
|
16
|
+
def self.transform(notebook, raise_errors: false, include_frontmatter: true, hide_images: false)
|
17
17
|
return unless notebook
|
18
18
|
|
19
|
-
Transformer.new(include_frontmatter: include_frontmatter).transform(notebook).as_text
|
19
|
+
Transformer.new(include_frontmatter: include_frontmatter, hide_images: hide_images).transform(notebook).as_text
|
20
20
|
rescue InvalidNotebookError
|
21
21
|
raise if raise_errors
|
22
22
|
end
|
data/lib/output_transformer.rb
CHANGED
@@ -6,12 +6,18 @@ module IpynbDiff
|
|
6
6
|
require 'symbolized_markdown_helper'
|
7
7
|
include SymbolizedMarkdownHelper
|
8
8
|
|
9
|
+
HIDDEN_IMAGE_OUTPUT = ' [Hidden Image Output]'
|
10
|
+
|
9
11
|
ORDERED_KEYS = {
|
10
12
|
'execute_result' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex text/plain],
|
11
13
|
'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex],
|
12
14
|
'stream' => %w[text]
|
13
15
|
}.freeze
|
14
16
|
|
17
|
+
def initialize(hide_images: false)
|
18
|
+
@hide_images = hide_images
|
19
|
+
end
|
20
|
+
|
15
21
|
def transform(output, symbol)
|
16
22
|
transformed = case (output_type = output['output_type'])
|
17
23
|
when 'error'
|
@@ -52,24 +58,22 @@ module IpynbDiff
|
|
52
58
|
new_symbol = symbol_prefix / output_type
|
53
59
|
case output_type
|
54
60
|
when 'image/png', 'image/jpeg'
|
55
|
-
transform_image(output_type, output_element, new_symbol)
|
61
|
+
transform_image(output_type + ';base64', output_element, new_symbol)
|
56
62
|
when 'image/svg+xml'
|
57
|
-
|
63
|
+
transform_image(output_type + ';utf8', output_element, new_symbol)
|
58
64
|
when 'text/markdown', 'text/latex', 'text/plain', 'text'
|
59
65
|
transform_text(output_element, new_symbol)
|
60
66
|
end
|
61
67
|
end
|
62
68
|
|
63
69
|
def transform_image(image_type, image_content, symbol)
|
64
|
-
_(
|
65
|
-
end
|
70
|
+
return _(nil, HIDDEN_IMAGE_OUTPUT) if @hide_images
|
66
71
|
|
67
|
-
def transform_svg(image_content, symbol)
|
68
72
|
lines = image_content.is_a?(Array) ? image_content : [image_content]
|
69
73
|
|
70
74
|
single_line = lines.map(&:strip).join.gsub(/\s+/, ' ')
|
71
75
|
|
72
|
-
_(symbol, " ")
|
73
77
|
end
|
74
78
|
|
75
79
|
def transform_text(text_content, symbol)
|
data/lib/transformer.rb
CHANGED
@@ -17,9 +17,10 @@ module IpynbDiff
|
|
17
17
|
@include_frontmatter = true
|
18
18
|
@objects_to_ignore = ['application/javascript', 'application/vnd.holoviews_load.v0+json']
|
19
19
|
|
20
|
-
def initialize(include_frontmatter: true)
|
20
|
+
def initialize(include_frontmatter: true, hide_images: false)
|
21
21
|
@include_frontmatter = include_frontmatter
|
22
|
-
@
|
22
|
+
@hide_images = hide_images
|
23
|
+
@output_transformer = OutputTransformer.new(hide_images: hide_images)
|
23
24
|
end
|
24
25
|
|
25
26
|
def validate_notebook(notebook)
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ipynbdiff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduardo Bonet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diffy
|