Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a list as a variable in Python and use in Jinja2?

I am a very young programmer and I am trying to do something in Python but I'm stuck. I have a list of users in Couchdb (using python couchdb library & Flask framework) who have a username (which is the _id) and email. I want to use the list of email addresses in a select box in a jinja2 template.

My first problem is how to access the email addresses. If I do:

for user in db:
    doc = db[user]
    emails = doc['email']
    print options

I get:

[email protected]
[email protected]
[email protected]

So I can get my list of emails. But where my brutal inexperience is showing up is that I don't know how to then use them. The list only exists in the for loop. How do I return that list as a useable list of variables? And how do I then make that list appear in my jinja2 template in an option dropdown. I guess I need a function but I am a green programmer.

Would so appreciate help.

like image 292
Handloomweaver Avatar asked Oct 19 '10 16:10

Handloomweaver


People also ask

How do I pass a list in Jinja?

Answer #1: To pass some context data to javascript code, you have to serialize it in a way it will be “understood” by javascript (namely JSON). You also need to mark it as safe using the safe Jinja filter, to prevent your data from being htmlescaped.

Can you use Python functions in Jinja?

What might be worth knowing is the fact that you can pass a Python function into your Jinja templates. Doing this can greatly improve the readability of your template as well as allow you to handle more complicated scenario's.

Which data type we use for send values in Jinja?

Jinja has built-in support for all native Python data types, including List, Tuple, Dictionary, Set, String, Integer, Bool, and even Classes. This feature makes Jinja super easy to use with Python as one does not need to know explicit data types other than native Python data structures.

How does Jinja template work?

It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.


2 Answers

Assuming you have a model such as:

class User(Document):
    email = TextField()

You can use the static method load of the User class

users = [User.load(db, uid) for uid in db]

Now you can do this:

for user in users:
    print user.id, user.email  

But you're using it in flask so, in your view you can send this list of users to your template using something like this:

from flask import render_template
@app.route("/users")
def show_users():
    users = [User.load(db, uid) for uid in db]
    return render_template('users.html', users=users)

Now in the users.html jinja2 template the following will output a dropdown listbox of each user's e-mail

<select>
{% for user in users %}
    <option value="{{ user.id }}">{{ user.email }}</option>
{% endfor %}
</select>

Also, are you using the Flask-CouchDB extension? It might be helpful in abstracting out some of the low level couchdb coding: http://packages.python.org/Flask-CouchDB/

Disclaimer: The code above wasn't tested, but should work fine. I don't know much about CouchDB, but I am familiar with Flask. Also, I obviously didn't include a full Flask/CouchDB application here, so bits of code are missing.

like image 50
Josh Avatar answered Oct 04 '22 01:10

Josh


You pass parameters to a jinja template as a dictionary d when you call the template.renderfunction(d) function (for example). Thus, you could do:

emails = []
for user in db:
    doc = db[user]
    emails.append(doc['email'])
some_jinja_template.render({'list_of_emails' : emails})

Then in the template, you could do something like:

<ul>
{% for address in list_of_emails %}
    <li><a href="mailto:{{ address }}">Send email to {{ address }}</a></li>
{% endfor %}
</ul>

To make a list of emails, for example, or handle them however you'd like.

PS - I'm sure the code could be more elegant/more optimized with a list comprehension or whatever, but I figured I should emphasize readability for a so-called "green" programmer.

like image 20
nearlymonolith Avatar answered Oct 04 '22 00:10

nearlymonolith