import sqlite3 import jinja2 import os import shutil for dirname in [ "public/song", "public/motif", "public/album" ]: os.makedirs(dirname, exist_ok=True) environment = jinja2.Environment(loader=jinja2.FileSystemLoader("templates/")) songplate = environment.get_template("songpage.jinja") db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True) # generate song pages for id, name, name_jp in db.execute(''' select id, name, name_jp from song '''): # query album info album_info = db.execute(''' select album_id, track, album.name, album.code from song_album join album on song_album.album_id=album.id where song_id = ? ''', (id,) ).fetchall() print(album_info) filename = f"public/song/{id:04}" content = songplate.render( name=name, id=id, album_info=album_info ) with open(filename, mode="w") as file: file.write(content) print(f"... wrote {filename}")