Sha256: 2a63f7e711b65e0f277bb711c0ca34121558873434a1b8572398793dfe87af6d

Contents?: true

Size: 1.28 KB

Versions: 19

Compression:

Stored size: 1.28 KB

Contents

require 'pycall'
require 'iruby'

module PyCall
  module IRubyHelper
    private

    def check_pyobject_respond_to_format_method(obj, method_name)
      return false unless obj.kind_of? PyObject
      return false unless PyCall.hasattr?(obj, method_name)
      PyCall.getattr(obj, method_name).kind_of? PyCall::LibPython.PyMethod_Type
    end

    def register_pyobject_formatter(format_name, mime, priority_value=0)
      method_name = :"_repr_#{format_name}_"
      match do |obj|
        check_pyobject_respond_to_format_method(obj, method_name)
      end
      priority priority_value
      format mime do |obj|
        PyCall.getattr(obj, method_name).()
      end
    end
  end
end

::IRuby::Display::Registry.module_eval do
  extend PyCall::IRubyHelper

  register_pyobject_formatter :html, 'text/html'
  register_pyobject_formatter :markdown, 'text/markdown'
  register_pyobject_formatter :svg, 'image/svg+xml'
  register_pyobject_formatter :png, 'image/png'
  register_pyobject_formatter :jpeg, 'image/jpeg'
  register_pyobject_formatter :latex, 'text/latex'
  register_pyobject_formatter :json, 'application/json'
  register_pyobject_formatter :javascript, 'application/javascript'
  register_pyobject_formatter :pdf, 'application/pdf'
  register_pyobject_formatter :pretty, 'text/plain', -1000
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pycall-1.0.1-x86-mingw32 lib/pycall/iruby_helper.rb
pycall-1.0.1-x64-mingw32 lib/pycall/iruby_helper.rb
pycall-1.0.1 lib/pycall/iruby_helper.rb
pycall-1.0.0 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170711 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170502 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170426 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170419b lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170419a lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170419 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170403 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170329 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170317 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170311 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170309 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170308 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170307 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170302 lib/pycall/iruby_helper.rb
pycall-0.1.0.alpha.20170226 lib/pycall/iruby_helper.rb