How to insert checkbox dynamically from Python / Flask code?

1

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

I have in my code a for which scans the subdirectories 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 subdirectories respectively found

Example: mine has found 4 subdirectories (dir1, dir2, dir3, dir4). So in my layout I will have 4 checkboxes (dir1, dir2, dir3, dir4).

Can anyone help?

    
asked by anonymous 16.05.2016 / 15:06

2 answers

0
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

@app.route("/")
def main():
    return checkboxes(".") # retorna all_names para a página.

app.run()
    
14.11.2016 / 18:21
0

The easiest way is to do this directly on Jinja2

{% for d in dirs %}
    <input id="{{d}}" name="{{d}}" type="checkbox">
{% endfor %}
    
19.01.2018 / 11:38