How to insert checkbox dynamically from Python/Flask code?


I'm trying to create a script in Flask and I came across the following situation:

I have a for in my code that scans the sub-directories of a root directory:

for root, dirs, files in os.walk(destination):
    for name in dirs:

I need to put a checkbox list in my html layout with the names of the respectively found sub-directories

Example: my for found 4 sub-directories (dir1, dir2, dir3, dir4). So my layout will have 4 checkboxes (dir1, dir2, dir3, dir4).

Can anyone help?


from flask import Flask
import os # os.walk()

app = Flask("checkboxes") # from flask

def checkboxes(directory):
    all_names = ""
    template = "{}: <input type=\"checkbox\" value=\"{}\"/><br>\n"

    for root, dirs, files in os.walk(directory):
        for name in dirs:
            all_names += template.format(name) # name vai para {0}.
    return all_names

def main():
    return checkboxes(".") # retorna all_names para a página.
Scroll to Top