Skip to content

Instantly share code, notes, and snippets.

@eonu
Last active January 29, 2020 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eonu/c3392a0eb07e53851bd6afe69bf3c4a7 to your computer and use it in GitHub Desktop.
Save eonu/c3392a0eb07e53851bd6afe69bf3c4a7 to your computer and use it in GitHub Desktop.
NBX
#!/usr/bin/env ruby
class NBX
# Execute long-running Jupyter notebooks from the command-line
def initialize
@allow_errors = true
@inplace = false
end
def start(args)
args = parse_options args
if args.empty?
help
elsif args.size == 1
run args.first.inspect
elsif args.size == 2
input, output = args.map &:inspect
help if @inplace
run "#{input} --output #{output}"
else
help
end
end
def run(args)
cmd = "jupyter nbconvert --ExecutePreprocessor.timeout=-1 --execute --to notebook #{args}"
cmd << ' --allow-errors' if @allow_errors
cmd << ' --inplace' if @inplace
exec cmd
end
def parse_options(args)
if args.include? '-i'
@inplace = true
args.delete '-i'
end
if args.include? '-!'
@allow_errors = false
args.delete '-!'
end
if args.include?('-i!') or args.include?('-!i')
@inplace, @allow_errors = true, false
['-i!', '-!i'].each {|opt| args.delete opt}
end
args
end
def help
puts
puts "\e[4mnbx\e[0m: Execute long-running Jupyter notebooks from the command-line"
puts
puts "\e[1mUsage\e[0m:"
puts " nbx nb.ipynb \e[90m# Run nb.ipynb and store output in nb.nbconvert.ipynb\e[0m"
puts " nbx -i nb.ipynb \e[90m# Run nb.ipynb and write output cells inplace\e[0m"
puts " nbx nb.ipynb out.ipynb \e[90m# Run nb.ipynb and store output in out.ipynb\e[0m"
puts
puts "\e[1mNote\e[0m: \e[37mThe -! flag can be used to disallow errors during execution.\e[0m"
puts
exit 0
end
end
nbx = NBX.new
nbx.start ARGV
@eonu
Copy link
Author

eonu commented Jan 28, 2020

nbx: Execute long-running Jupyter notebooks from the command-line

Usage:
  nbx nb.ipynb            # Run nb.ipynb and store output in nb.nbconvert.ipynb
  nbx -i nb.ipynb         # Run nb.ipynb and write output cells inplace
  nbx nb.ipynb out.ipynb  # Run nb.ipynb and store output in out.ipynb

Note: The -! flag can be used to disallow errors during execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment