0

I have a csv file and I need to read it as list and add every item to input form, but when I use specific option value I can add them by pressing comma, but when I read them from .csv file this method does not work. What can be the problem?

with open('templates/Testing.csv') as f:
        reader = csv.reader(f)
        myvalues = next(reader)


@app.route('/', methods=['GET'])
def page_show():
    return render_template('includes/default.html', myvalues=myvalues)

from .csv method(it does not work by pressing comma)

<form action="/action" method="POST">
  <input list="myvalues" multiple>
  <datalist id="myvalues">
    {% for val in myvalues %}
        <option value="{{val}}">{{val}}</option>
    {% endfor %}
  </datalist>
  <input type="submit">
</form>

adding specific value method(it works by pressing comma)

<form action="/action" method="POST">
  <input list="myvalues" multiple>
  <datalist id="myvalues">
        <option value="one">one</option>
        <option value="two">one</option>
        <option value="three">one</option>
        <option value="four">one</option>
  </datalist>
  <input type="submit">
</form>

1 Answers1

0

I think I can solve at least a part of your problem.

First of all, the next() function only returns the next item of the list (csv data). Whereas I reckon you should have used the list() function. This function creates a list where you can loop through in default.html.

To solve the problem of the variables not being accessible in default.html, it probably gives you the following error: UnboundLocalError: local variable 'myvalues' referenced before assignment. Which means that you are trying to asign myvalues to myvalues but python doesn't know where to look for that variable. See more on that here.

So how would you solve this? Just rename the myvalues to something like myData. See the full code below:

main.py

myvalues = []
with open('templates/Testing.csv') as f:
        reader = csv.reader(f)
        myvalues = next(reader)


@app.route('/', methods=['GET'])
def page_show():
    return render_template('includes/default.html', myData=myvalues)

default.html:


<form action="/action" method="POST">
  <input list="myvalues" multiple>
  <datalist id="myvalues">
    {% for val in myData %}
        <option value="{{val}}">{{val}}</option>
    {% endfor %}
  </datalist>
  <input type="submit">
</form>

Btw, that

I can add them by pressing comma, but when I read them from .csv file this method does not work

I have no idea what you mean, pleas elaborate.

Hope this helps! If not, please comment

Community
  • 1
  • 1
Chiel
  • 1,324
  • 1
  • 11
  • 30
  • thanks for replying, but in your sample I can see all rows in input from csv. I need to use only first row of my .csv file. All values in the first row. –  Jun 11 '20 at 10:52
  • when I say add them by pressing comma I mean I need to select first value from dropdown, and add other values, by pressing comma or automatically, I mean make a loop for dropdown to select values again and again. You can check this [link](https://stackoverflow.com/questions/14148538/multiple-selections-with-datalist). Please assist, I asked such question many times, no one could help. –  Jun 11 '20 at 10:54
  • Hi, Ill change the answer according to the first comment. However for the second comment you should take a look at the answers [here](https://stackoverflow.com/questions/14148538/multiple-selections-with-datalist). [This one](https://stackoverflow.com/a/58404139/8902440) seems the most simple. Hope this helps – Chiel Jun 11 '20 at 12:13