Try this:
1. Using service:
@service.json
def my_data():
your_data = your_query.as_dict()
return response.json(you_data)
2. At view call:
http://127.0.0.1/test/default/my_data
3. Most likely it's to return a dictionary with data,if it works then you
call via getJson.
Em sexta-feira, 7 de outubro de 2016 04:30:53 UTC-3, 黄祥 escreveu:
>
> let say i have a code with web2py in highchart, when i tried to convert it
> data using json, it's not work
> *previous code works well using python in views manually *
> *views/templates/chart_pie.html*
> series: [{
> type: 'pie',
> name: '{{=T('Subscription') }} ',
> data: [
> {{for row_pie_subscription in rows_2:}}
> ['{{=T(row_pie_subscription.product.name) }}',
> {{=row_pie_subscription.subscription_amount}} ],
> {{pass}}
> ]
> }],
>
> *# 1 - trying using service not work, no error occured, the result is not
> expected (repeated data on subscription)*
> *controllers/default.py*
> import json
>
> @service.json
> def test():
> query = (db.account.investor == 1)
> rows = db(query).select(orderby = ~db.account.id)
> return json.dumps([[r.product.name, r.subscription_amount] for r in
> rows] )
> #return dict(rows = rows)
>
> *views/templates/chart_pie.html*
> $.getJSON('http://127.0.0.1:8000/test/default/call/json/test',
> function (data) {
>
> // pie subscription
> $('#pie_subscription').highcharts({
> ...
> series: [{
> type: 'pie',
> name: '{{=T('Subscription') }} ',
> data: data
> }],
> ...
> });
>
> });
>
> *trying to test manually http://127.0.0.1:8000/test/default/call/json/test
> <http://127.0.0.1:8000/test/default/call/json/test>*
> "[[\"MNC Dana Ekuitas\", 1.0], [\"MNC Dana Likuid\", 2.0], [\"MNC Dana
> Lancar\", 3.0]]"
>
> *# 2 - trying json dumps in view not work, no error occured, the result is
> not expected (no chart pie at all)*
> *views/templates/chart_pie.htm*l
> series: [{
> type: 'pie',
> name: '{{=T('Subscription') }} ',
> data:
> {{
> import json
> json.dumps([[r.product.name, r.subscription_amount] for r
> in rows] )
> }}
> }],
>
> is it possible to achieve it using web2py way?
>
> thanks and best regards,
> stifan
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.