skip to Main Content

I am very new to Python and Jinja and facing an issue in accessing the values of the list in HTML. It is showing blank on the page. Given below is the code snippet, any help would be highly appreciated. The emplList is retrieving the values in the manner
[0,"John"] [1,"Sunny"] [2,"Pal"]

app = Flask(__name__)
emplList = pd.read_sql_query("SELECT DISTINCT employee_name FROM employeeTBL", conn)

@app.route('/')
def home():
    return render_template('app.html', employees=emplList)

if __name__=='__main__':
    app.run(debug=True)
    

<select id="multipleSelect" multiple name="native-select" placeholder="Native Select" data-search="true" data-silent-initial-value-set="true">
          {% for row in employees%}
             <option value={{row.employee_name}}>{{row.employee_name}}</option>
          {% endfor %}
</select>

2

Answers


  1. Chosen as BEST ANSWER

    finally able to get past the error, given below is the code that worked

    {% for row in employees.employee_name %}
                 <option value='{{row}}'>{{row}}</option>
    {% endfor %}
    

  2. Edit: This should be exactly what you need based on the pandas docs and how jinja expects template data:

    render_template('app.html', employees=emplList.to_dict('records'))
    

    If this isn’t working for you, feel free to comment on my answer here or edit your question to provide more detailed info on what your data is or problems you’re having.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search