1

this isn't a very specific question, so sorry in advance.

I have a folder called Crews, and it contains json files.

I'm using WTForms to create a drop down list, and I want the list to be populated with the names of the json files within that folder, but I don't know how to go about this.

(I'm creating a web app that visualizes the chosen json data, so the form will contain a function that returns the chosen json data, which is called by the d3.js code)

gbhrea
  • 494
  • 1
  • 9
  • 22

1 Answers1

9

Use this to get all the files in your directory:

How do I list all files of a directory?

Once you get that list of json files, you can use it to populate the choices in a WTForms SelectField

from flask.ext.wtf import Form
from wtforms import SelectField

filenames = ['1.json', '2.json'] # This will be generated by you
class MyForm
    json_file = SelectField(u"Filename", [Optional()], choices=[(f, f) for f in filenames])
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
Adam Steele
  • 256
  • 2
  • 10