Skip to content

Commit

Permalink
Can now pass template variables into nbkickoff
Browse files Browse the repository at this point in the history
Change the command-line syntax for `nbkickoff` slightly to use
positional arguments, which is easier to use.

Also allow the caller to specify any number of template variables on the
command line, using a simple `NAME=VALUE` syntax.
  • Loading branch information
Onno Broekmans committed Jun 15, 2018
1 parent 7a53f42 commit 009eea0
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions nbkickoff/nbkickoff.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env python3

from .__about__ import __summary__
from .__about__ import (__summary__, __title__)
import logging
import os
from pathlib import Path
import shutil
import sys
import webbrowser

Expand Down Expand Up @@ -59,19 +58,38 @@ def open_notebook(notebook_file):
launch_detached_process(sys.executable, '-m', 'notebook', '--NotebookApp.open_browser=True', notebook_file)


def kickoff(template_file, target_file):
create_notebook_from_template(template_file, target_file, {})
def kickoff(template_file, target_file, variables):
create_notebook_from_template(template_file, target_file, variables)
open_notebook(target_file)


def main():
import argparse
parser = argparse.ArgumentParser(description=__summary__)
parser.add_argument('-t', '--template', required=True, help='Template notebook file')
parser.add_argument('-f', '--file', required=True, help='Target notebook file to create')

def parse_template_var(s):
if '=' not in s:
raise argparse.ArgumentTypeError('Invalid template variable specification: ' + s)
parts = s.split('=', 1)
return {parts[0]: parts[1]}

class DictMergeAction(argparse.Action):
def __call__(self, p, namespace, values, option_string=None):
current_dict = dict()
if hasattr(namespace, self.dest) and getattr(namespace, self.dest) is not None:
current_dict = getattr(namespace, self.dest)
for v in values:
current_dict = {**current_dict, **v}
setattr(namespace, self.dest, current_dict)

parser = argparse.ArgumentParser(prog=__title__, description=__summary__)
parser.add_argument('template', help='Template notebook file')
parser.add_argument('target', help='Target notebook file to create')
parser.add_argument('variable', action=DictMergeAction, type=parse_template_var, nargs='*',
help='Template variable in the format NAME=VALUE')
args = parser.parse_args()
kickoff(args.template, args.file)
kickoff(args.template, args.target, args.variable)


if __name__ == '__main__':
main()

0 comments on commit 009eea0

Please sign in to comment.