librelist archives

« back to archive

Calculating sums in jinja?

Calculating sums in jinja?

From:
Alexandru
Date:
2015-06-16 @ 13:48
Hello everyone,

I'm pretty new to all this, so please excuse any silly code.

I have the following code in my app file:

    @richter.route("/rezultate")
    def rezultate():
         search_for=request.args.get("search_for")
         cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE
    '{}'".format(search_for))
         items = [dict(id=row[0], farmacie=row[1], produs=row[2],
    stoc=row[3]) for row in cur1.fetchall()]
         cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM
    stocks INNER JOIN reserved ON stocks.id=reserved.id")
         rezervate = [dict(id=row[0], res_full=row[1]) for row in
    cur2.fetchall()]
         return render_template("rezultate.html", items=items,
    rezervate=rezervate)


And the following in my theme:

    {% for item in items %}
    <tr>
         <form action="{{ url_for('rezervare', id=item.id) }}"
    id="form_rezervare_{{ item.id }}" method="POST">
             <td>{{ item.farmacie }}</td>
             <td>{{ item.produs }}</td>
             <td>{{ item.stoc }}</td>
             <td>
             {% for rezervat in rezervate %}
                 {% if item.id == rezervat.id %}
    *                {{ rezervat.res_full }}*
                 {% endif %}
             {% endfor %}
             </td>
             <td><input type="number" class="form-control"
    name="rezerva_{{ item.id }}"></td>
             <td><button type="submit" class="btn btn-sm btn-primary"
    form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
         </form>
    </tr>
    {% endfor %}


On the line marked in bold above, I need/want to make a sum of the 
entries pulled from the database for each (unique) item, but I cannot 
figure out how to do it.

Any tips on how I can manage this?

Thank you all!

Re: [flask] Calculating sums in jinja?

From:
Brad Derstine
Date:
2015-06-16 @ 14:46
You have to set a variable in the jinja, then increment as you loop 
through to calculate the sum…


http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template

 

-Brad

 

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Alexandru
Sent: Tuesday, June 16, 2015 9:49 AM
To: flask@librelist.com
Subject: [flask] Calculating sums in jinja?

 

Hello everyone,

I'm pretty new to all this, so please excuse any silly code.

I have the following code in my app file:

@richter.route("/rezultate")
def rezultate():
    search_for=request.args.get("search_for")
    cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE 
'{}'".format(search_for))
    items = [dict(id=row[0], farmacie=row[1], produs=row[2], stoc=row[3]) 
for row in cur1.fetchall()]
    cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM stocks 
INNER JOIN reserved ON stocks.id=reserved.id")
    rezervate = [dict(id=row[0], res_full=row[1]) for row in cur2.fetchall()]
    return render_template("rezultate.html", items=items, rezervate=rezervate)


And the following in my theme:

{% for item in items %}
<tr>
    <form action="{{ url_for('rezervare', id=item.id) }}" 
id="form_rezervare_{{ item.id }}" method="POST">
        <td>{{ item.farmacie }}</td>
        <td>{{ item.produs }}</td>
        <td>{{ item.stoc }}</td>
        <td>
        {% for rezervat in rezervate %}
            {% if item.id == rezervat.id %}
                {{ rezervat.res_full }}
            {% endif %}
        {% endfor %}
        </td>
        <td><input type="number" class="form-control" name="rezerva_{{ 
item.id }}"></td>
        <td><button type="submit" class="btn btn-sm btn-primary" 
form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
    </form>
</tr>
{% endfor %}


On the line marked in bold above, I need/want to make a sum of the entries
pulled from the database for each (unique) item, but I cannot figure out 
how to do it.

Any tips on how I can manage this?

Thank you all!

Re: [flask] Calculating sums in jinja?

From:
Alexandru Chicheanu
Date:
2015-06-16 @ 15:24
Hi,

I'm not sure that helps... I need to calculate the sum of the items, not 
the number of items.

Currently, the code I have just outputs

    <td>3 3 2 2</td>


I need it to output the sum of those numbers

    <td>10</td>


Alex.

On 06/16/2015 05:46 PM, Brad Derstine wrote:
>
> You have to set a variable in the jinja, then increment as you loop 
> through to calculate the sum…
>
> 
http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template
>
> -Brad
>
> *From:*flask@librelist.com [mailto:flask@librelist.com] *On Behalf Of 
> *Alexandru
> *Sent:* Tuesday, June 16, 2015 9:49 AM
> *To:* flask@librelist.com
> *Subject:* [flask] Calculating sums in jinja?
>
> Hello everyone,
>
> I'm pretty new to all this, so please excuse any silly code.
>
> I have the following code in my app file:
>
>     @richter.route("/r ezultate")
>     def rezultate():
>     search_for=request.args.get("search_for")
>         cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE
>     '{}'".format(search_for))
>         items = [dict(id=row[0], farmacie=row[1], produs=row[2],
>     stoc=row[3]) for row in cur1.fetchall()]
>         cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM
>     stocks INNER JOIN reserved ON stocks.id=reserved.id")
>         rezervate = [dict(id=row[0], res_full=row[1]) for row in
>     cur2.fetchall()]
>         return render_template("rezultate.html", ite ms=items,
>     rezervate=rezervate)
>
>
> And the following in my theme:
>
>     {% for item in items %}
>     <tr>
>         <form action="{{ url_for('rezervare', id=item.id) }}"
>     id="form_rezervare_{{ item.id }}" method="POST">
>             <td>{{ item.farmacie }}</td>
>             <td>{{ item.produs }}</td>
>             <td>{{ item.stoc }}</td>
>             <td>
>             {% for rezervat in rezervate %}< br>            {% if
>     item.id == rezervat.id %}
>     *                {{ rezervat.res_full }}*
>                 {% endif %}
>             {% endfor %}
>             </td>
>             <td><input type="number" class="form-control"
>     name="rezerva_{{ item.id }}"></td>
>             <td><button type="submit" class="btn btn-sm btn-primary"
>     form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
>         </form>
>     </tr>
>     {% endfor %}
>
>
> On the line marked in bold above, I need/want to make a sum of the 
> entries pulled from the database for each (unique) item, but I cannot 
> figure out how to do it.
>
> Any tips on how I can manage this?
>
> Thank you all!
>

Re: [flask] Calculating sums in jinja?

From:
archerydwd
Date:
2015-06-16 @ 15:37
Hi,

Why not do this calculation in the python code before you send it to jinja?
Just assign it to a new variable and return that variable along with the 
rest of the information.

Darren.

> On 16 Jun 2015, at 16:24, Alexandru Chicheanu <alexandru@bluemasters.ro> wrote:
> 
> Hi,
> 
> I'm not sure that helps... I need to calculate the sum of the items, not
the number of items.
> 
> Currently, the code I have just outputs
> <td>3 3 2 2</td>
> 
> I need it to output the sum of those numbers
> <td>10</td>
> 
> Alex.
> 
> On 06/16/2015 05:46 PM, Brad Derstine wrote:
>> You have to set a variable in the jinja, then increment as you loop 
through to calculate the sum…
>> 
http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template

<http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template>
>>  
>> -Brad
>>  
>> From: flask@librelist.com <mailto:flask@librelist.com> 
[mailto:flask@librelist.com <mailto:flask@librelist.com>] On Behalf Of 
Alexandru
>> Sent: Tuesday, June 16, 2015 9:49 AM
>> To: flask@librelist.com <mailto:flask@librelist.com>
>> Subject: [flask] Calculating sums in jinja?
>>  
>> Hello everyone,
>> 
>> I'm pretty new to all this, so please excuse any silly code.
>> 
>> I have the following code in my app file:
>> @richter.route("/r ezultate")
>> def rezultate():
>>     search_for=request.args.get("search_for")
>>     cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE 
'{}'".format(search_for))
>>     items = [dict(id=row[0], farmacie=row[1], produs=row[2], 
stoc=row[3]) for row in cur1.fetchall()]
>>     cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM 
stocks INNER JOIN reserved ON stocks.id=reserved.id")
>>     rezervate = [dict(id=row[0], res_full=row[1]) for row in cur2.fetchall()]
>>     return render_template("rezultate.html", ite ms=items, rezervate=rezervate)
>> 
>> And the following in my theme:
>> {% for item in items %}
>> <tr>
>>     <form action="{{ url_for('rezervare', id=item.id) }}" 
id="form_rezervare_{{ item.id }}" method="POST">
>>         <td>{{ item.farmacie }}</td>
>>         <td>{{ item.produs }}</td>
>>         <td>{{ item.stoc }}</td>
>>         <td>
>>         {% for rezervat in rezervate %}< br>            {% if item.id 
== rezervat.id %}
>>                 {{ rezervat.res_full }}
>>             {% endif %}
>>         {% endfor %}
>>         </td>
>>         <td><input type="number" class="form-control" name="rezerva_{{ 
item.id }}"></td>
>>         <td><button type="submit" class="btn btn-sm btn-primary" 
form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
>>     </form>
>> </tr>
>> {% endfor %}
>> 
>> On the line marked in bold above, I need/want to make a sum of the 
entries pulled from the database for each (unique) item, but I cannot 
figure out how to do it.
>> 
>> Any tips on how I can manage this?
>> 
>> Thank you all!
> 
> 

Re: [flask] Calculating sums in jinja?

From:
Alexandru Chicheanu
Date:
2015-06-16 @ 16:40
I figured out my own mistake.

I should've calculated the SUM in the SQL query, not in the jinja.
Like so:

    cur2 = g.db.execute("SELECT stocks.id, SUM(reserved.res_full) FROM
    stocks INNER JOIN reserved ON stocks.id=reserved.id GROUP BY
    reserved.id")


Cheers!

On 06/16/2015 06:24 PM, Alexandru Chicheanu wrote:
> Hi,
>
> I'm not sure that helps... I need to calculate the sum of the items, 
> not the number of items.
>
> Currently, the code I have just outputs
>
>     <td>3 3 2 2</td>
>
>
> I need it to output the sum of those numbers
>
>     <td>10</td>
>
>
> Alex.
>
> On 06/16/2015 05:46 PM, Brad Derstine wrote:
>>
>> You have to set a variable in the jinja, then increment as you loop 
>> through to calculate the sum…
>>
>> 
http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template
>>
>> -Brad
>>
>> *From:*flask@librelist.com [mailto:flask@librelist.com] *On Behalf Of 
>> *Alexandru
>> *Sent:* Tuesday, June 16, 2015 9:49 AM
>> *To:* flask@librelist.com
>> *Subject:* [flask] Calculating sums in jinja?
>>
>> Hello everyone,
>>
>> I'm pretty new to all this, so please excuse any silly code.
>>
>> I have the following code in my app file:
>>
>>     @richter.route("/r ezultate")
>>     def rezultate():
>>     search_for=request.args.get("search_for")
>>         cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE
>>     '{}'".format(search_for))
>>         items = [dict(id=row[0], farmacie=row[1], produs=row[2],
>>     stoc=row[3]) for row in cur1.fetchall()]
>>         cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM
>>     stocks INNER JOIN reserved ON stocks.id=reserved.id")
>>         rezervate = [dict(id=row[0], res_full=row[1]) for row in
>>     cur2.fetchall()]
>>         return render_template("rezultate.html", ite ms=items,
>>     rezervate=rezervate)
>>
>>
>> And the following in my theme:
>>
>>     {% for item in items %}
>>     <tr>
>>         <form action="{{ url_for('rezervare', id=item.id) }}"
>>     id="form_rezervare_{{ item.id }}" method="POST">
>>             <td>{{ item.farmacie }}</td>
>>             <td>{{ item.produs }}</td>
>>             <td>{{ item.stoc }}</td>
>>             <td>
>>             {% for rezervat in rezervate %}< br>            {% if
>>     item.id == rezervat.id %}
>>     *                {{ rezervat.res_full }}*
>>                 {% endif %}
>>             {% endfor %}
>>             </td>
>>             <td><input type="number" class="form-control"
>>     name="rezerva_{{ item.id }}"></td>
>>             <td><button type="submit" class="btn btn-sm btn-primary"
>>     form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
>>         </form>
>>     </tr>
>>     {% endfor %}
>>
>>
>> On the line marked in bold above, I need/want to make a sum of the 
>> entries pulled from the database for each (unique) item, but I cannot 
>> figure out how to do it.
>>
>> Any tips on how I can manage this?
>>
>> Thank you all!
>>
>

Re: [flask] Calculating sums in jinja?

From:
Brad Derstine
Date:
2015-06-16 @ 15:36
{% for rezervat in rezervate %}

    {% set sum = sum + rezervat.res_full %}

{% endfor %}

{{sum}}

 

Then use {{sum}} where you want the sum to show up. Or use a function. Or 
pass it through the mysql and use that.

 

-Brad

 

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of 
Alexandru Chicheanu
Sent: Tuesday, June 16, 2015 11:24 AM
To: flask@librelist.com
Subject: Re: [flask] Calculating sums in jinja?

 

Hi,

I'm not sure that helps... I need to calculate the sum of the items, not 
the number of items.

Currently, the code I have just outputs

<td>3 3 2 2</td>


I need it to output the sum of those numbers

<td>10</td>


Alex.

On 06/16/2015 05:46 PM, Brad Derstine wrote:

You have to set a variable in the jinja, then increment as you loop 
through to calculate the sum…


http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template

 

-Brad

 

From: flask@librelist.com <mailto:flask@librelist.com>  
[mailto:flask@librelist.com] On Behalf Of Alexandru
Sent: Tuesday, June 16, 2015 9:49 AM
To: flask@librelist.com <mailto:flask@librelist.com> 
Subject: [flask] Calculating sums in jinja?

 

Hello everyone,

I'm pretty new to all this, so please excuse any silly code.

I have the following code in my app file:

@richter.route("/r ezultate")
def rezultate():
    search_for=request.args.get("search_for")
    cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE 
'{}'".format(search_for))
    items = [dict(id=row[0], farmacie=row[1], produs=row[2], stoc=row[3]) 
for row in cur1.fetchall()]
    cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM stocks 
INNER JOIN reserved ON stocks.id=reserved.id")
    rezervate = [dict(id=row[0], res_full=row[1]) for row in cur2.fetchall()]
    return render_template("rezultate.html", ite ms=items, rezervate=rezervate)


And the following in my theme:

{% for item in items %}
<tr>
    <form action="{{ url_for('rezervare', id=item.id) }}" 
id="form_rezervare_{{ item.id }}" method="POST">
        <td>{{ item.farmacie }}</td>
        <td>{{ item.produs }}</td>
        <td>{{ item.stoc }}</td>
        <td>
        {% for rezervat in rezervate %}< br>            {% if item.id == 
rezervat.id %}
                {{ rezervat.res_full }}
            {% endif %}
        {% endfor %}
        </td>
        <td><input type="number" class="form-control" name="rezerva_{{ 
item.id }}"></td>
        <td><button type="submit" class="btn btn-sm btn-primary" 
form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
    </form>
</tr>
{% endfor %}


On the line marked in bold above, I need/want to make a sum of the entries
pulled from the database for each (unique) item, but I cannot figure out 
how to do it.

Any tips on how I can manage this?

Thank you all!

 

Re: [flask] Calculating sums in jinja?

From:
Alex C.
Date:
2015-06-16 @ 15:30
Nevermind,

I figured out my own mistake.

I should've calculated the SUM in the SQL query, not in the jinja.
Like so:

    cur2 = g.db.execute("SELECT stocks.id, SUM(reserved.res_full) FROM
    stocks INNER JOIN reserved ON stocks.id=reserved.id GROUP BY
    reserved.id")


Cheers!

On 06/16/2015 06:24 PM, Alexandru Chicheanu wrote:
> Hi,
>
> I'm not sure that helps... I need to calculate the sum of the items, 
> not the number of items.
>
> Currently, the code I have just outputs
>
>     <td>3 3 2 2</td>
>
>
> I need it to output the sum of those numbers
>
>     <td>10</td>
>
>
> Alex.
>
> On 06/16/2015 05:46 PM, Brad Derstine wrote:
>>
>> You have to set a variable in the jinja, then increment as you loop 
>> through to calculate the sum…
>>
>> 
http://stackoverflow.com/questions/7537439/how-to-increment-a-variable-on-a-for-loop-in-jinja-template
>>
>> -Brad
>>
>> *From:*flask@librelist.com [mailto:flask@librelist.com] *On Behalf Of 
>> *Alexandru
>> *Sent:* Tuesday, June 16, 2015 9:49 AM
>> *To:* flask@librelist.com
>> *Subject:* [flask] Calculating sums in jinja?
>>
>> Hello everyone,
>>
>> I'm pretty new to all this, so please excuse any silly code.
>>
>> I have the following code in my app file:
>>
>>     @richter.route("/r ezultate")
>>     def rezultate():
>>     search_for=request.args.get("search_for")
>>         cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE
>>     '{}'".format(search_for))
>>         items = [dict(id=row[0], farmacie=row[1], produs=row[2],
>>     stoc=row[3]) for row in cur1.fetchall()]
>>         cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM
>>     stocks INNER JOIN reserved ON stocks.id=reserved.id")
>>         rezervate = [dict(id=row[0], res_full=row[1]) for row in
>>     cur2.fetchall()]
>>         return render_template("rezultate.html", ite ms=items,
>>     rezervate=rezervate)
>>
>>
>> And the following in my theme:
>>
>>     {% for item in items %}
>>     <tr>
>>         <form action="{{ url_for('rezervare', id=item.id) }}"
>>     id="form_rezervare_{{ item.id }}" method="POST">
>>             <td>{{ item.farmacie }}</td>
>>             <td>{{ item.produs }}</td>
>>             <td>{{ item.stoc }}</td>
>>             <td>
>>             {% for rezervat in rezervate %}< br>            {% if
>>     item.id == rezervat.id %}
>>     *                {{ rezervat.res_full }}*
>>                 {% endif %}
>>             {% endfor %}
>>             </td>
>>             <td><input type="number" class="form-control"
>>     name="rezerva_{{ item.id }}"></td>
>>             <td><button type="submit" class="btn btn-sm btn-primary"
>>     form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
>>         </form>
>>     </tr>
>>     {% endfor %}
>>
>>
>> On the line marked in bold above, I need/want to make a sum of the 
>> entries pulled from the database for each (unique) item, but I cannot 
>> figure out how to do it.
>>
>> Any tips on how I can manage this?
>>
>> Thank you all!
>>
>

Re: [flask] Calculating sums in jinja?

From:
David Nieder
Date:
2015-06-16 @ 16:31
On 16.06.2015 15:48, Alexandru wrote:
> Hello everyone,

Hi
>
> I'm pretty new to all this, so please excuse any silly code.
>
> I have the following code in my app file:
>
>     @richter.route("/rezultate")
>     def rezultate():
>          search_for=request.args.get("search_for")
>          cur1 = g.db.execute("SELECT * FROM stocks WHERE produs LIKE
>     '{}'".format(search_for))
>          items = [dict(id=row[0], farmacie=row[1], produs=row[2],
>     stoc=row[3]) for row in cur1.fetchall()]
>          cur2 = g.db.execute("SELECT stocks.id, reserved.res_full FROM
>     stocks INNER JOIN reserved ON stocks.id=reserved.id")
>          rezervate = [dict(id=row[0], res_full=row[1]) for row in
>     cur2.fetchall()]
>          return render_template("rezultate.html", items=items,
>     rezervate=rezervate)
>

You really shouldn't build your SQL Statements this way. Image your 
'search_for' variable contains sth like 'foo; DROP TABLE stocks;'. This 
is called SQL injection. Instead of:

g.db.execute("SELECT * FROM stocks WHERE produs LIKE 
'{}'".format(search_for))

do:

g.db.execute("SELECT * FROM stocks WHERE produs LIKE ?", search_for)


To your question:
1. Do the calculation in python and give it to the template or
2. Write yourself a jinja filter:
http://flask.pocoo.org/docs/0.10/templating/#registering-filters

>
> And the following in my theme:
>
>     {% for item in items %}
>     <tr>
>          <form action="{{ url_for('rezervare', id=item.id) }}"
>     id="form_rezervare_{{ item.id }}" method="POST">
>              <td>{{ item.farmacie }}</td>
>              <td>{{ item.produs }}</td>
>              <td>{{ item.stoc }}</td>
>              <td>
>              {% for rezervat in rezervate %}
>                  {% if item.id == rezervat.id %}
>     *                {{ rezervat.res_full }}*
>                  {% endif %}
>              {% endfor %}
>              </td>
>              <td><input type="number" class="form-control"
>     name="rezerva_{{ item.id }}"></td>
>              <td><button type="submit" class="btn btn-sm btn-primary"
>     form="form_rezervare_{{ item.id }}">REZERVA STOC</button></td>
>          </form>
>     </tr>
>     {% endfor %}
>
>
> On the line marked in bold above, I need/want to make a sum of the
> entries pulled from the database for each (unique) item, but I cannot
> figure out how to do it.
>
> Any tips on how I can manage this?
>
> Thank you all!
>