ipynbdiff 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54c011ed4790dc548b33d6e4773bb9bbdaedd7b8c1ffbdbcfa5e494e5380b1a9
4
- data.tar.gz: 70f78c722ddc432576ec52d55617e4ce5bdf29cb7ff32ad46219ac52d0d6f5d5
3
+ metadata.gz: 10f77cf53513157724c6e1d8c8abc4659e3254dd77c3c14ebb2aaabd3155639c
4
+ data.tar.gz: 10815dc6a9cb76c73b4d6c9f712bdb28be9217a2b40efbcf076342e9f5d34811
5
5
  SHA512:
6
- metadata.gz: 757f9ea284f358771835db2e27ebe90cb5c8c99336313a65d04cbd0080c0891e1c25af79740cb4d191eeafec10e66c74baceef1ba24729b6ba6f743d502564fa
7
- data.tar.gz: 35866e056ae8f4ab043bd9cc247f6c3f8dc3b02d475e94221ca11352badf1e96cf840e8020553ffa8c32b1359f967c74ccc67e0f4abda376e8a035d027871daf
6
+ metadata.gz: 06e895b990e7099d094b6e78c8a54fc6e5c618b28a9ae266cdb47a127189d67df7a7468f5a96fb2da21529f30911dfca761d7e4b0fc5452fc351e1878c697811
7
+ data.tar.gz: 54ee8fb62e05b130b304989787ee1032ffe41a2dc68dcdfde5bf7480588bdcd3a4f9a20c3b63eeb7cf21dd0ed89527b34d39a7ec15170f7cddda6114ea8fac0a
data/ipynbdiff.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'ipynbdiff'
5
- s.version = '0.3.5'
5
+ s.version = '0.3.6'
6
6
  s.summary = 'Human Readable diffs for Jupyter Notebooks'
7
7
  s.description = 'Better diff for Jupyter Notebooks by first preprocessing them and removing clutter'
8
8
  s.authors = ['Eduardo Bonet']
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IpynbDiff
4
+
5
+ # Transforms Jupyter output data into markdown
6
+ class OutputTransformer
7
+
8
+ ORDERED_KEYS = {
9
+ 'execute_result' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex text/plain],
10
+ 'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex]
11
+ }.freeze
12
+
13
+ def transform(output)
14
+ case (output_type = output['output_type'])
15
+ when 'error'
16
+ transform_error(output['traceback'])
17
+ when 'execute_result', 'display_data'
18
+ transform_non_error(ORDERED_KEYS[output_type], output['data'])
19
+ end
20
+ end
21
+
22
+ def transform_error(traceback)
23
+ traceback.map do |t|
24
+ t.split("\n").map do |line|
25
+ line.gsub(/\[[0-9][0-9;]*m/, '').sub("\u001B", ' ').gsub(/\u001B/, '').rstrip << "\n"
26
+ end
27
+ end
28
+ end
29
+
30
+ def transform_non_error(accepted_keys, elements)
31
+ accepted_keys.map do |key|
32
+ transform_element(key, elements[key]) if elements.key?(key)
33
+ end.flatten
34
+ end
35
+
36
+ def transform_element(output_type, output_element)
37
+ case output_type
38
+ when 'image/png', 'image/jpeg'
39
+ transform_image(output_type, output_element)
40
+ when 'image/svg+xml'
41
+ transform_svg(output_element)
42
+ when 'text/markdown', 'text/latex', 'text/plain'
43
+ transform_text(output_element)
44
+ end
45
+ end
46
+
47
+ def transform_image(image_type, image_content)
48
+ [" ![](data:#{image_type};base64,#{image_content.gsub("\n", '')})", "\n"]
49
+ end
50
+
51
+ def transform_svg(image_content)
52
+ single_line = image_content.map(&:strip).join('').gsub(/\s+/, ' ')
53
+
54
+ [" ![](data:image/svg+xml;utf8,#{single_line})", "\n"]
55
+ end
56
+
57
+ def transform_text(text_content)
58
+ text_content.map { |line| " #{line}" }.append("\n")
59
+ end
60
+ end
61
+ end
data/lib/transformer.rb CHANGED
@@ -8,13 +8,16 @@ module IpynbDiff
8
8
  class Transformer
9
9
  require 'json'
10
10
  require 'yaml'
11
+ require 'output_transformer'
11
12
 
12
13
  @cell_decorator = :html
13
14
  @include_metadata = TRUE
14
15
 
16
+
15
17
  def initialize(include_metadata: TRUE, cell_decorator: :html)
16
18
  @include_metadata = include_metadata
17
19
  @cell_decorator = cell_decorator
20
+ @output_transformer = OutputTransformer.new
18
21
  end
19
22
 
20
23
  def validate_notebook(notebook)
@@ -73,38 +76,8 @@ module IpynbDiff
73
76
  ]
74
77
  end
75
78
 
76
- def format_traceback(traceback)
77
- traceback.map do |t|
78
- t.split("\n").map do |line|
79
- line.gsub(/\[[0-9][0-9;]*m/, '').sub("\u001B", ' ').gsub(/\u001B/, '').rstrip << "\n"
80
- end
81
- end
82
- end
83
-
84
- def transform_execute_result(output)
85
- output['data']['text/plain'].map { |line| " #{line}" }.append("\n")
86
- end
87
-
88
- def transform_image_result(output)
89
- if output['data'].key?('image/png')
90
- ["![](data:image/png;base64,#{output['data']['image/png'].gsub("\n", '')})", "\n"]
91
- end
92
- end
93
-
94
- def transform_error_result(output)
95
- format_traceback(output['traceback'])
96
- end
97
-
98
79
  def transform_output(output)
99
- transformed =
100
- case output['output_type']
101
- when 'execute_result'
102
- transform_execute_result(output)
103
- when 'display_data'
104
- transform_image_result(output)
105
- when 'error'
106
- transform_error_result(output)
107
- end
80
+ transformed = @output_transformer.transform(output)
108
81
 
109
82
  decorate_output(transformed, output).join('') if transformed
110
83
  end
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.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Bonet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-19 00:00:00.000000000 Z
11
+ date: 2021-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diffy
@@ -121,6 +121,7 @@ files:
121
121
  - README.md
122
122
  - ipynbdiff.gemspec
123
123
  - lib/ipynbdiff.rb
124
+ - lib/output_transformer.rb
124
125
  - lib/transformer.rb
125
126
  homepage: https://gitlab.com/gitlab-org/incubation-engineering/mlops/rb-ipynbdiff
126
127
  licenses: