SciRuby / iruby

Official gem repository: Ruby kernel for Jupyter/IPython Notebook
https://github.com/SciRuby/iruby
MIT License
901 stars 29 forks source link

Suggestion: to_iruby should take precedence over other display methods #314

Closed ankane closed 2 years ago

ankane commented 3 years ago

Hi, currently, if an object has methods like to_html or to_png, they'll take precedence over to_iruby. If an object has to_iruby defined, it seems like that should be preferred.

class A
  def to_html
    "HTML"
  end

  def to_iruby
    ["text/plain", "IRUBY"]
  end
end

A.new

Shows HTML instead of IRUBY

ankane commented 3 years ago

Related, it looks like iRuby calls all the display methods, even if it only renders one.

class A
  def to_html
    puts "called to_html"
    "HTML"
  end

  def to_png
    puts "called to_png"
  end

  def to_iruby
    puts "called to_png"
    ["text/plain", "IRUBY"]
  end
end

A.new

Prints

called to_png
called to_html
called to_png

And renders HTML

ankane commented 3 years ago

It looks like one way around this is to define a to_iruby_mimebundle method.

def to_iruby_mimebundle(**)
  [{"text/plain" => "IRUBY"}, {}]
end
mrkn commented 2 years ago

@ankane I changed the display routine in recent versions so that it follows the behavior of IPython's object formatter. It is necessary to support both Jupyter Notebook and JupyterLab, simultaneously.

mrkn commented 2 years ago

@ankane I recommend using to_iruby_mimebundle instead of to_iruby now.

ankane commented 2 years ago

Hey @mrkn, thanks for the response and context! Will use that.