2

In the example below, I'm creating href's for each row item in Column3 and the variable (var1) will be sent along with it (which works fine). My question is how can I also send the adjacent row value using a variable (var2) for Column1? Would this need to be handled with jquery or another means?

var2 below is what what I want to be able to do, but not sure how to, currently.

table:

       Column1    Column2    Column3
Row1   Yes        Foo        Bar 
Row2   No         Foo1       Bar3
Row3   Maybe      Foo2       Bar2

app.py:

@app.route('/mypage/<var1>')
def python_function(var1,var2):
    df = pd.DataFrame(lotsofdata)
    return render_template('index.html',data=df)

index.html:

<table id="data">
<thead>
<tr>
{% for c in data.columns.values %}
    <th>{{ c }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for index, row in data.iterrows() %}
    <tr>
        {% for v in row %}
            <td>
            {% if data.columns[loop.index0] in ['Column3'] %}
                <a href="{{ url_for('python_function',var1=v,var2=???) }}">{{ v }}</a>
            {% else %}
                {{ v }}
            {% endif %}
            </td>
        {% endfor %}
    </tr>
{% endfor %}
</tbody>
</table>
brennan
  • 3,392
  • 24
  • 42
Mike
  • 2,531
  • 6
  • 26
  • 34

1 Answers1

2

To call a function in jinja you'd create a global function in __init__.py

def cool_func(var1, var2):
    return var1 + var2  # or something

app.jinja_env.globals.update(cool_func=cool_func)

Then for you example you could replace:

<a href="{{ url_for('python_function',var1=v,var2=???) }}">

With:

<a href={{ cool_func(var1, var2) }}>
brennan
  • 3,392
  • 24
  • 42
  • Ahhh.. I hadn't thought about doing it this way. Identify the adjacent cell in python and then call call it from jinja... that makes multi-cell calls super easy now. Fantastic, thank you! – Mike Apr 10 '17 at 13:27