Sha256: 5f2ef22fbe55099bd853b88cf301f202612a738f023d28c3b5ca19d24cdb828c

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module JupyterOnRails
  class Kernel
    def initialize(sandbox: false)
      @root = ENV.fetch('RAILS_ROOT')
      @sandbox = sandbox
    end

    def boot
      boot_rails
      load_extensions
    end

    private

    def boot_rails
      boot_file = File.expand_path('config/boot.rb', @root)
      require boot_file
    end

    def load_extensions
      require_relative 'iruby_kernel_extention'
      JupyterOnRails::IRubyKernelExtention.root = @root
      JupyterOnRails::IRubyKernelExtention.sandbox = @sandbox

      require 'iruby'
      IRuby::Kernel.instance_eval { prepend JupyterOnRails::IRubyKernelExtention }
      IRuby::Display::Registry.instance_eval do
        match do |obj|
          obj.is_a?(ActiveRecord::Relation) ||
            obj.is_a?(::Class) && obj < ActiveRecord::Base && !obj.abstract_class
        end
        priority 100
        format 'text/html' do |obj|
          n = 10
          puts "finding top #{n}"
          obj.limit(n).to_df.to_html
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jupyter_on_rails-0.8.1 lib/jupyter_on_rails/kernel.rb
jupyter_on_rails-0.8.0 lib/jupyter_on_rails/kernel.rb