irails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fbc76a46d914cf5aa4b54c6165d94c77b2c40b98
4
+ data.tar.gz: dec155c6c6712bdb415f239e84e1b22dbab8260f
5
+ SHA512:
6
+ metadata.gz: 636f4db27153185d8b6b74fc6a6254c40cb171e248009588f943bf67f02d5689b54bba649bbb503a7dfc252ddc7060f1acd61bfd2b94ec76a40b5e19c6ab147e
7
+ data.tar.gz: f7fe77f37c16cc1f2bfa569a31ddd293bf0e423c25c959497a64ced3e31ff52ed28238696984c5971bcfb7381dc68eec616e4125197adbaddbe6cbd518a249fc
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ .ipynb_checkpoints/
2
+ *.gem
3
+ *.rbc
4
+ *.log
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ coverage
10
+ doc/
11
+ pkg
12
+ rdoc
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+ venv
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - ruby-head
5
+ - 2.2.2
6
+ - 2.1.6
7
+ - 2.0.0
8
+
9
+ before_install:
10
+ - "sudo apt-get install libzmq3-dev"
11
+ - "sudo pip install 'ipython[notebook]'"
12
+
13
+ script: bundle exec rake
14
+
15
+ notifications:
16
+ irc: "chat.freenode.net#sciruby"
data/CONTRIBUTORS ADDED
@@ -0,0 +1,19 @@
1
+ Carlos Agarie <carlos.agarie@gmail.com>
2
+ Damián Silvani <munshkr@gmail.com>
3
+ Dangyi Liu <LeedyPKU@gmail.com>
4
+ Daniel Mendler <mail@daniel-mendler.de>
5
+ Dimitris Zorbas <zorbash@skroutz.gr>
6
+ Domitry <domitry@gmail.com>
7
+ Jan Vlnas <git@jan.vlnas.cz>
8
+ John Woods <jwoods@vt.edu>
9
+ Josh Adams <josh@isotope11.com>
10
+ Julia Evans <julia@jvns.ca>
11
+ martin sarsale <martin@properati.com>
12
+ Matthias Bussonnier <bussonniermatthias@gmail.com>
13
+ Michael Hauser-Raspe <michael.hauser-raspe@cantab.net>
14
+ MinRK <benjaminrk@gmail.com>
15
+ Naoki Nishida <domitry@gmail.com>
16
+ Robby Clements <rclements@isotope11.com>
17
+ Spacewander <spacewanderlzx@gmail.com>
18
+ Takahiro Satoh <zalt50cc@gmail.com>
19
+ Robert Haines <robert.haines@manchester.ac.uk>
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :pry do
5
+ gem 'pry'
6
+ gem 'pry-doc'
7
+ gem 'awesome_print'
8
+ end
9
+
10
+ group :plot do
11
+ gem 'gnuplot'
12
+ gem 'rubyvis'
13
+ gem 'nyaplot', github: 'domitry/nyaplot'
14
+ end
15
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013-2015 IRuby contributors and the Ruby Science Foundation
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # IRails
2
+
3
+ This is a Ruby kernel for IPython/Jupyter and is part of [SciRuby](http://scirails.com/). You can try it at [try.jupyter.org](http://try.jupyter.org/).
4
+
5
+ ![Screenshot](https://cloud.githubusercontent.com/assets/50754/7956845/3fa46df8-09e3-11e5-8641-f5b8669061b5.png)
6
+
7
+ ### Quick start
8
+
9
+ At first install IPython/Jupyter. I recommend an installation using virtualenv.
10
+
11
+ apt-get install python3-dev virtualenv libzmq3-dev
12
+ virtualenv -p python3 venv
13
+ source venv/bin/activate
14
+ pip install 'ipython[notebook]'
15
+
16
+ After that, install the Ruby gem.
17
+
18
+ gem install irails
19
+
20
+ Now you can run irails with:
21
+
22
+ irails
23
+ irails notebook
24
+
25
+ Take a look at the [example notebook](http://nbviewer.ipython.org/urls/raw.github.com/SciRuby/scirails-notebooks/master/getting_started.ipynb)
26
+ and the [collection of notebooks](https://github.com/SciRuby/scirails-notebooks/) which includes a Dockerfile to create a containerized installation of irails
27
+ and other scientific gems. You can find the prebuild image at [dockerhub](https://registry.hub.docker.com/u/minad/scirails-notebooks/).
28
+
29
+
30
+ ### Required dependencies
31
+
32
+ * IPython/Jupyter >= 3.0.0
33
+ * libzmq >= 3.2
34
+ * Ruby >= 2.1.0
35
+
36
+ ### Authors
37
+
38
+ See the [CONTRIBUTORS](CONTRIBUTORS) file.
39
+
40
+ ### License
41
+
42
+ Copyright © 2013-15, Hiroshi Kajisha, IRuby contributors and the Ruby Science Foundation.
43
+
44
+ All rights reserved.
45
+
46
+ IRuby, along with [SciRuby](http://scirails.com/), is licensed under the MIT license. See the [LICENSE](LICENSE) file for details.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rake/testtask'
2
+
3
+ begin
4
+ require 'bundler/gem_tasks'
5
+ rescue Exception
6
+ end
7
+
8
+ Rake::TestTask.new('test') do |t|
9
+ t.libs << 'lib'
10
+ t.libs << 'test'
11
+ t.test_files = FileList['test/**/*_test.rb']
12
+ t.verbose = true
13
+ end
14
+
15
+ task default: 'test'
data/bin/irails ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path(__dir__ + '/../lib')
4
+ require 'irails/command'
5
+ IRails::Command.new(ARGV).run
data/irails.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/irails/version'
2
+ require 'date'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'irails'
6
+ s.date = Date.today.to_s
7
+ s.version = IRails::VERSION
8
+ s.authors = ['Hiroshi Kajisha', 'Daniel Mendler', 'The SciRuby developers']
9
+ s.email = ['kajisha@gmail.com', 'mail@daniel-mendler.de']
10
+ s.summary = 'Rails Kernel for Jupyter'
11
+ s.description = 'A Rails kernel for Jupyter frontends (e.g. notebook). Try it at try.jupyter.org.'
12
+ s.homepage = 'https://github.com/kajisha/irails'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^test/})
18
+ s.require_paths = %w(lib)
19
+
20
+ m = "Consider installing the optional dependencies to get additional functionality:\n"
21
+ File.read('Gemfile').scan(/gem\s+'(.*?)'/) { m << " * #{$1}\n" }
22
+ s.post_install_message = m << "\n"
23
+
24
+ s.required_ruby_version = '>= 2.1.0'
25
+
26
+ s.add_development_dependency 'rake'
27
+ s.add_development_dependency 'minitest'
28
+
29
+ s.add_runtime_dependency 'bond'
30
+ s.add_runtime_dependency 'rbczmq'
31
+ s.add_runtime_dependency 'multi_json'
32
+ s.add_runtime_dependency 'mimemagic'
33
+ s.add_runtime_dependency 'rails'
34
+ end
@@ -0,0 +1 @@
1
+ /* Placeholder for Ruby kernel.css */
@@ -0,0 +1,15 @@
1
+ // Ruby kernel.js
2
+
3
+ define(['base/js/namespace'], function(IPython) {
4
+ "use strict";
5
+ var onload = function() {
6
+ IPython.CodeCell.options_default['cm_config']['indentUnit'] = 2;
7
+ var cells = IPython.notebook.get_cells();
8
+ for (var i in cells){
9
+ var c = cells[i];
10
+ if (c.cell_type === 'code')
11
+ c.code_mirror.setOption('indentUnit', 2);
12
+ }
13
+ }
14
+ return {onload:onload};
15
+ });
Binary file
Binary file
@@ -0,0 +1,82 @@
1
+ module IRails
2
+ In, Out = [nil], [nil]
3
+ ::In, ::Out = In, Out
4
+
5
+ module History
6
+ def eval(code, store_history)
7
+ b = TOPLEVEL_BINDING
8
+
9
+ b.local_variable_set(:_ih, In) unless b.local_variable_defined?(:_ih)
10
+ b.local_variable_set(:_oh, Out) unless b.local_variable_defined?(:_oh)
11
+
12
+ out = super
13
+
14
+ # TODO Add IRails.cache_size which controls the size of the Out array
15
+ # and sets the oldest entries and _<n> variables to nil.
16
+ if store_history
17
+ b.local_variable_set("_#{Out.size}", out)
18
+ b.local_variable_set("_i#{In.size}", code)
19
+
20
+ Out << out
21
+ In << code
22
+
23
+ b.local_variable_set(:___, Out[-3])
24
+ b.local_variable_set(:__, Out[-2])
25
+ b.local_variable_set(:_, Out[-1])
26
+ b.local_variable_set(:_iii, In[-3])
27
+ b.local_variable_set(:_ii, In[-2])
28
+ b.local_variable_set(:_i, In[-1])
29
+ end
30
+
31
+ out
32
+ end
33
+ end
34
+
35
+ class PlainBackend
36
+ prepend History
37
+
38
+ def initialize
39
+ require 'bond'
40
+ Bond.start(debug: true)
41
+ end
42
+
43
+ def eval(code, store_history)
44
+ TOPLEVEL_BINDING.eval(code)
45
+ end
46
+
47
+ def complete(code)
48
+ Bond.agent.call(code, code)
49
+ end
50
+ end
51
+
52
+ class PryBackend
53
+ prepend History
54
+
55
+ def initialize
56
+ require 'pry'
57
+ Pry.pager = false # Don't use the pager
58
+ Pry.print = proc {|output, value|} # No result printing
59
+ Pry.exception_handler = proc {|output, exception, _| }
60
+ reset
61
+ end
62
+
63
+ def eval(code, store_history)
64
+ @pry.last_result = nil
65
+ unless @pry.eval(code)
66
+ reset
67
+ raise SystemExit
68
+ end
69
+ raise @pry.last_exception if @pry.last_result_is_exception?
70
+ @pry.push_initial_binding unless @pry.current_binding # ensure that we have a binding
71
+ @pry.last_result
72
+ end
73
+
74
+ def complete(code)
75
+ @pry.complete(code)
76
+ end
77
+
78
+ def reset
79
+ @pry = Pry.new(output: $stdout, target: TOPLEVEL_BINDING)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,46 @@
1
+ module IRails
2
+ # Comm is a new messaging system for bidirectional communication.
3
+ # Both kernel and front-end listens for messages.
4
+ class Comm
5
+ attr_writer :on_msg, :on_close
6
+
7
+ class << self
8
+ def target; @target ||= {} end
9
+ def comm; @comm ||= {} end
10
+ end
11
+
12
+ def initialize(target_name, comm_id = SecureRandom.uuid)
13
+ @target_name, @comm_id = target_name, comm_id
14
+ end
15
+
16
+ def open(**data)
17
+ Kernel.instance.session.send(:publish, :comm_open, comm_id: @comm_id, data: data, target_name: @target_name)
18
+ Comm.comm[@comm_id] = self
19
+ end
20
+
21
+ def send(**data)
22
+ Kernel.instance.session.send(:publish, :comm_msg, comm_id: @comm_id, data: data)
23
+ end
24
+
25
+ def close(**data)
26
+ Kernel.instance.session.send(:publish, :comm_close, comm_id: @comm_id, data: data)
27
+ Comm.comm.delete(@comm_id)
28
+ end
29
+
30
+ def on_msg(&b)
31
+ @on_msg = b
32
+ end
33
+
34
+ def on_close(&b)
35
+ @on_close = b
36
+ end
37
+
38
+ def handle_msg(data)
39
+ @on_msg.call(data) if @on_msg
40
+ end
41
+
42
+ def handle_close(data)
43
+ @on_close.call(data) if @on_close
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,136 @@
1
+ require 'fileutils'
2
+ require 'multi_json'
3
+
4
+ module IRails
5
+ class Command
6
+ def initialize(args)
7
+ @args = args
8
+
9
+ ipython_dir = ENV['IPYTHONDIR'] || '~/.ipython'
10
+ @args.each do |arg|
11
+ ipython_dir = $1 if arg =~ /\A--ipython-dir=(.*)\Z/
12
+ end
13
+ @kernel_dir = File.join(File.expand_path(ipython_dir), 'kernels', 'rails')
14
+ @kernel_file = File.join(@kernel_dir, 'kernel.json')
15
+ @irails_path = File.expand_path $0
16
+
17
+ require './config/boot'
18
+ require './config/application'
19
+
20
+ Rails.application.require_environment!
21
+ end
22
+
23
+ def run
24
+ case @args.first
25
+ when 'version', '-v', '--version'
26
+ require 'irails/version'
27
+ puts "IRails #{IRails::VERSION}, Rails #{Rails.version}"
28
+ when 'help', '-h', '--help'
29
+ print_help
30
+ when 'register'
31
+ if registered_irails_path && !@args.include?('--force')
32
+ STDERR.puts "#{@kernel_file} already exists!\nUse --force to force a register."
33
+ exit 1
34
+ end
35
+ register_kernel
36
+ when 'unregister'
37
+ unregister_kernel
38
+ when 'kernel'
39
+ run_kernel
40
+ else
41
+ run_ipython
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def print_help
48
+ puts %{
49
+ Usage:
50
+ irails register Register IRails kernel in #{@kernel_file}.
51
+ irails unregister Unregister IRails kernel.
52
+ irails console Launch the IRails terminal-based console.
53
+ irails notebook Launch the IRails HTML notebook server.
54
+ ... Same as IPython.
55
+
56
+ Please note that IRails accepts the same parameters as IPython.
57
+ Try `ipython help` for more information.
58
+ }
59
+ end
60
+
61
+ def run_kernel
62
+ require 'irails/logger'
63
+ IRails.logger = MultiLogger.new(*Logger.new(STDOUT))
64
+ @args.reject! {|arg| arg =~ /\A--log=(.*)\Z/ && IRails.logger.loggers << Logger.new($1) }
65
+ IRails.logger.level = @args.delete('--debug') ? Logger::DEBUG : Logger::INFO
66
+
67
+ raise(ArgumentError, 'Not enough arguments to the kernel') if @args.size < 2 || @args.size > 4
68
+ config_file, boot_file, working_dir = @args[1..-1]
69
+ Dir.chdir(working_dir) if working_dir
70
+
71
+ require boot_file if boot_file
72
+ check_bundler {|e| IRails.logger.warn "Could not load bundler: #{e.message}\n#{e.backtrace.join("\n")}" }
73
+
74
+ require 'irails'
75
+ Kernel.new(config_file).run
76
+ rescue Exception => e
77
+ IRails.logger.fatal "Kernel died: #{e.message}\n#{e.backtrace.join("\n")}"
78
+ raise
79
+ end
80
+
81
+ def check_version
82
+ required = '3.0.0'
83
+ version = `ipython --version`.chomp
84
+ if version < required
85
+ STDERR.puts "Your IPython version #{version} is too old, at least #{required} is required"
86
+ exit 1
87
+ end
88
+ end
89
+
90
+ def run_ipython
91
+ # If no command is given, we use the console to launch the whole 0MQ-client-server stack
92
+ @args = %w(console) + @args if @args.first.to_s !~ /\A\w/
93
+ @args += %w(--kernel ruby) if %w(console qtconsole).include? @args.first
94
+
95
+ check_version
96
+ check_registered_kernel
97
+ check_bundler {|e| STDERR.puts "Could not load bundler: #{e.message}" }
98
+
99
+ Kernel.exec('ipython', *@args)
100
+ end
101
+
102
+ def check_registered_kernel
103
+ if kernel = registered_irails_path
104
+ STDERR.puts "#{@irails_path} differs from registered path #{registered_irails_path}.
105
+ This might not work. Run 'irails register --force' to fix it." if @irails_path != kernel
106
+ else
107
+ register_kernel
108
+ end
109
+ end
110
+
111
+ def check_bundler
112
+ require 'bundler'
113
+ raise %q{irails is missing from Gemfile. This might not work.
114
+ Add `gem 'irails'` to your Gemfile to fix it.} unless Bundler.definition.dependencies.any? {|s| s.name == 'irails' }
115
+ Bundler.setup
116
+ rescue LoadError
117
+ rescue Exception => e
118
+ yield(e)
119
+ end
120
+
121
+ def register_kernel
122
+ FileUtils.mkpath(@kernel_dir)
123
+ File.write(@kernel_file, MultiJson.dump(argv: [ @irails_path, 'kernel', '{connection_file}' ],
124
+ display_name: "Rails #{::Rails.version}", language: 'ruby'))
125
+ FileUtils.copy(Dir[File.join(__dir__, 'assets', '*')], @kernel_dir) rescue nil
126
+ end
127
+
128
+ def registered_irails_path
129
+ File.exist?(@kernel_file) && MultiJson.load(File.read(@kernel_file))['argv'].first
130
+ end
131
+
132
+ def unregister_kernel
133
+ FileUtils.rm_rf(@kernel_dir)
134
+ end
135
+ end
136
+ end