Skip to content
Snippets Groups Projects
Commit fa47e707 authored by Eduardo Bonet's avatar Eduardo Bonet :speech_balloon:
Browse files

Merge branch '361843-adds-option-to-hide-images' into 'main'

Adds options to hide images from diffs

See merge request !17
parents 6ca923b9 c51634d8
No related branches found
No related tags found
1 merge request!17Adds options to hide images from diffs
Pipeline #539906448 passed
......@@ -5,18 +5,18 @@ module IpynbDiff
require 'transformer'
require 'diff'
def self.diff(from, to, raise_if_invalid_nb: false, include_frontmatter: false, diffy_opts: {})
transformer = Transformer.new(include_frontmatter: include_frontmatter)
def self.diff(from, to, raise_if_invalid_nb: false, include_frontmatter: false, hide_images: false, diffy_opts: {})
transformer = Transformer.new(include_frontmatter: include_frontmatter, hide_images: hide_images)
Diff.new(transformer.transform(from), transformer.transform(to), diffy_opts)
rescue InvalidNotebookError
raise if raise_if_invalid_nb
end
def self.transform(notebook, raise_errors: false, include_frontmatter: true)
def self.transform(notebook, raise_errors: false, include_frontmatter: true, hide_images: false)
return unless notebook
Transformer.new(include_frontmatter: include_frontmatter).transform(notebook).as_text
Transformer.new(include_frontmatter: include_frontmatter, hide_images: hide_images).transform(notebook).as_text
rescue InvalidNotebookError
raise if raise_errors
end
......
......@@ -6,12 +6,18 @@ module IpynbDiff
require 'symbolized_markdown_helper'
include SymbolizedMarkdownHelper
HIDDEN_IMAGE_OUTPUT = ' [Hidden Image Output]'
ORDERED_KEYS = {
'execute_result' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex text/plain],
'display_data' => %w[image/png image/svg+xml image/jpeg text/markdown text/latex],
'stream' => %w[text]
}.freeze
def initialize(hide_images: false)
@hide_images = hide_images
end
def transform(output, symbol)
transformed = case (output_type = output['output_type'])
when 'error'
......@@ -61,6 +67,8 @@ module IpynbDiff
end
def transform_image(image_type, image_content, symbol)
return _(nil, HIDDEN_IMAGE_OUTPUT) if @hide_images
lines = image_content.is_a?(Array) ? image_content : [image_content]
single_line = lines.map(&:strip).join.gsub(/\s+/, ' ')
......
......@@ -17,9 +17,10 @@ module IpynbDiff
@include_frontmatter = true
@objects_to_ignore = ['application/javascript', 'application/vnd.holoviews_load.v0+json']
def initialize(include_frontmatter: true)
def initialize(include_frontmatter: true, hide_images: false)
@include_frontmatter = include_frontmatter
@output_transformer = OutputTransformer.new
@hide_images = hide_images
@output_transformer = OutputTransformer.new(hide_images: hide_images)
end
def validate_notebook(notebook)
......
......@@ -16,8 +16,9 @@ describe IpynbDiff do
let(:from) { File.read(from_path) }
let(:to) { File.read(to_path) }
let(:include_frontmatter) { false }
let(:hide_images) { false }
subject { IpynbDiff.diff(from, to, include_frontmatter: include_frontmatter) }
subject { IpynbDiff.diff(from, to, include_frontmatter: include_frontmatter, hide_images: hide_images) }
context 'if preprocessing is active' do
it 'html tables are stripped' do
......@@ -51,6 +52,14 @@ describe IpynbDiff do
end
end
context 'When hide_images is true' do
let(:hide_images) { true }
it 'hides images' do
expect(subject.to_s(:text)).to include(' [Hidden Image Output]')
end
end
context 'When include_frontmatter is false' do
it 'should drop metadata from the diff' do
expect(subject.to_s(:text)).to_not include('+ display_name: New Python 3 (ipykernel)')
......@@ -82,19 +91,34 @@ describe IpynbDiff do
end
end
context 'when include_frontmatter' do
context 'options' do
let(:include_frontmatter) { false }
let(:hide_images) { false }
subject do
IpynbDiff.transform(File.read('spec/testdata/from.ipynb'), include_frontmatter: include_frontmatter )
IpynbDiff.transform(File.read('spec/testdata/from.ipynb'),
include_frontmatter: include_frontmatter,
hide_images: hide_images)
end
context 'is true' do
context 'include_frontmatter is false' do
it { is_expected.to_not include('display_name: Python 3 (ipykernel)') }
end
context 'include_frontmatter is true' do
let(:include_frontmatter) { true }
it { is_expected.to include('display_name: Python 3 (ipykernel)') }
end
context 'is false' do
let(:include_frontmatter) { false }
it { is_expected.to_not include('display_name: Python 3 (ipykernel)') }
context 'hide_images is false' do
it { is_expected.not_to include('[Hidden Image Output]') }
end
context 'hide_images is true' do
let(:hide_images) { true }
it { is_expected.to include(' [Hidden Image Output]') }
end
end
end
......
%% Cell type:code id:5 tags:senoid
``` python
```
%%%% Output: display_data
[Hidden Image Output]
%%%% Output: display_data
[Hidden Image Output]
.cells.0
.cells.0.source
.cells.0.outputs.0
.cells.0.outputs.1
%% Cell type:code id:5 tags:senoid
``` python
```
%% Output
......@@ -9,7 +9,8 @@ BASE_PATH = 'spec/testdata/'
def default_config
@default_config ||= {
include_frontmatter: false
include_frontmatter: false,
hide_images: false
}
end
......@@ -49,6 +50,7 @@ describe IpynbDiff::Transformer do
'ignores unknown output type' | 'unknown_output_type' | {}
'handles backslash correctly' | 'backslash_as_last_char' | {}
'multiline png output' | 'multiline_png_output' | {}
'hides images when option passed' | 'hide_images' | { hide_images: true }
end
with_them do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment