Skip to content

Instantly share code, notes, and snippets.

@sharmaeklavya2
Created March 2, 2021 09:09
Show Gist options
  • Select an option

  • Save sharmaeklavya2/1149c76b29a74553752f0cf255f27a40 to your computer and use it in GitHub Desktop.

Select an option

Save sharmaeklavya2/1149c76b29a74553752f0cf255f27a40 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"Takes a Jinja2 template file and a JSON context file as input. Returns rendered output."
import json
import argparse
import jinja2
def render(template_path, context_path, output_path):
with open(template_path) as tfp:
template = jinja2.Template(tfp.read())
with open(context_path) as cfp:
context = json.load(cfp)
with open(output_path, 'w') as ofp:
ofp.write(template.render(context))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-t', '--template-path', required=True,
help='path to template file')
parser.add_argument('-c', '--context-path', required=True,
help='path to JSON file containing context for the template')
parser.add_argument('-o', '--output-path', required=True,
help='path to output file')
args = parser.parse_args()
render(args.template_path, args.context_path, args.output_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment