Created
July 10, 2023 11:47
-
-
Save cdalar/42e2549e075f4c9a3eeb7858377e7e9f to your computer and use it in GitHub Desktop.
python jinja2 template example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.venv/bin/python | |
import jinja2, sys | |
import configparser | |
from collections import defaultdict | |
def ini_to_dict(ini_path): | |
config = configparser.ConfigParser() | |
config.read(ini_path) | |
ini_dict = defaultdict(dict) | |
for section in config.sections(): | |
for key, val in config.items(section): | |
ini_dict[section][key] = val | |
return ini_dict | |
def main(): | |
# print(f"Arguments count: {len(sys.argv)}") | |
# for i, arg in enumerate(sys.argv): | |
# print(f"Argument {i:>6}: {arg}") | |
templateLoader = jinja2.FileSystemLoader(searchpath="./") | |
templateEnv = jinja2.Environment(loader=templateLoader) | |
TEMPLATE_FILE = "template.md" | |
template = templateEnv.get_template(TEMPLATE_FILE) | |
dict = ini_to_dict('template.data') | |
# print(dict) | |
outputText = template.render(dict[sys.argv[1]]) | |
print(outputText) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment