Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Thousand Separator in Jinja2 on Google App Engine

I'm working on YouTube Data API. I'm trying to display the viewCount from the video statistics in my HTML using jinja2 on Google App Engine.

When I specify constant values like in my template like:

{{ '{0:,}'.format(1234567890) }} 

the output works okay as:

 1,234,567,890

However, if I specify the code as:

 {{ '{0:,}'.format(video_item.statistics.viewCount) }} 

It does not work and displays internal server error saying:

{{ '{0:,}'.format(vivi.statistics.viewCount) }}, ValueError: Cannot specify ',' with 's'.

I'm not sure what that means.

However,

{{video_item.statistics.viewCount}}

works correctly. Can someone help me out please? Thanks

like image 353
Afloz Avatar asked Jan 18 '14 08:01

Afloz


2 Answers

@matthias-eisen thankx for your answer. It worked fine. In Jinja2, int(some_string) does not work. I used:

some_string | int

So for my question, it should be:

{{ '{0:,}'.format(video_item.statistics.viewCount | int) }}
like image 200
Afloz Avatar answered Sep 28 '22 01:09

Afloz


The API passes viewCount as a string (see https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=statistics&id=I90H3dN2HbI&_h=2&).

Inside the Handler:

view_count = '{0:,}'.format(int(video_item.statistics.viewCount))

Inside the Template:

{{ view_count }}

Also: http://docs.python.org/2/library/string.html#format-specification-mini-language

like image 21
Matthias Eisen Avatar answered Sep 28 '22 01:09

Matthias Eisen