Module: RubyPyMill::API
- Defined in:
- lib/ruby_pymill/api.rb
Class Method Summary collapse
-
.run(notebook:, output:, kernel: "rpymill", cell_tags: nil, params: nil, log: nil) ⇒ Object
Ruby から notebook を実行する公式API.
Class Method Details
.run(notebook:, output:, kernel: "rpymill", cell_tags: nil, params: nil, log: nil) ⇒ Object
Ruby から notebook を実行する公式API
例:
RubyPyMill::API.run(
notebook: "demo/notebooks/xxx.ipynb",
output: "demo/outputs/out.ipynb",
kernel: "rpymill",
cell_tags:"setup,preprocess,report",
params: "demo/params/kodama.json",
log: "demo/logs/run_xxx.log"
)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ruby_pymill/api.rb', line 18 def self.run( notebook:, output:, kernel: "rpymill", cell_tags: nil, params: nil, log: nil ) cmd = [ "ruby_pymill", "exec", notebook, "--output", output, "--kernel", kernel, ] cmd += ["--cell-tag", ] if && !.empty? cmd += ["--params", params] if params && !params.empty? stdout_all = +"" status = nil Open3.popen2e(*cmd) do |_stdin, stdout_err, wait_thr| stdout_err.each do |line| print line # コンソールにも流す stdout_all << line # ログにも残す end status = wait_thr.value end if log log_dir = File.dirname(log) Dir.mkdir(log_dir) unless Dir.exist?(log_dir) File.write(log, stdout_all) end unless status&.success? raise "ruby_pymill failed (status=#{status.exitstatus})" end stdout_all end |