Creating charts and trends from db

36 views
Skip to first unread message

Maurice Waka

unread,
Dec 20, 2018, 8:53:35 PM12/20/18
to web2py-users
I've come across this site where one can create charts and graphs.
However I have a challenge of using the database as I don't know how to link to it. For example, i want to track a variable like weight. The user inputs the values on a regular basis with the trend showing on the graph. How do I connect to the database and track it daily?
So far, my code is this:
model
db.define_table('my_tracker',
               
Field('weight','double',),
               
Field('created_by','reference auth_user',default = auth.user_id, readable = False, writable = False))

Controller:
    row = db(db.tracker.created_by == auth.user_id).select().first()
   
if not row:
        form
= SQLFORM(db.my_tracker, submit_button=T('Submit'))
        form
.element('input[name=weight]')['_style']='width:60px;height:30px;'

sandeep patel

unread,
Dec 21, 2018, 1:58:29 AM12/21/18
to web...@googlegroups.com
You can achieve this way.
Controller.py
import pygal
def chart():
    response.files.append(URL('default','static/js/pygal-tooltips.min.js'))  
    response.headers['Content-Type']='image/svg+xml'
    import pygal
    from pygal.style import Style
 
    custom_style = Style(
        background='transparent',
        plot_background='transparent',
        foreground='#53E89B',
        foreground_strong='#53A0E8',
        foreground_subtle='#630C0D',
        opacity='.6',
        opacity_hover='.9',
        transition='400ms ease-in',
        colors=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53')
        )
 
    chart = pygal.StackedLine(fill=True, interpolate='cubic', style=custom_style, )
    data = db(db. my_tracker).select()
    chart.add('A', [i.weight for i in data])

    return chart.render()

def index():
   chart= URL('default', 'chart')
  form = SQLFORM(db.my_tracker, submit_button=T('Submit')).process()

  form
.element('input[name=weight]')['_style']='width:60px;height:30px;'
  return dict(chart = chart,form=form)

#View/index.html
{{extend 'layout.html'}}
<figure>
  <embed type="image/svg+xml" src="{{=chart}}" />
</figure>

<h1>{{=form}}</h1>

You can change style according to your convenience


Thanks
SP



--
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Dec 21, 2018, 3:25:03 AM12/21/18
to web...@googlegroups.com
Hello Sandeep,
The form appears well, but the chart area doesn't show any chart, looks a small box at the top left side of the page. 
Trying to show it shows a message: 'insufficient privileges'.
Regards

You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/dGkepqrPY0g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

sandeep patel

unread,
Dec 21, 2018, 4:32:11 AM12/21/18
to web...@googlegroups.com
@Maurice
This code is working good. you can check here.
It would be easy to inspect the problem if you can show your controller code what you have written.

Thanks

Lovedie JC

unread,
Dec 21, 2018, 4:41:45 AM12/21/18
to web...@googlegroups.com
Let me countercheck. 

黄祥

unread,
Dec 21, 2018, 4:47:46 AM12/21/18
to web2py-users
another way around perhaps you can use web2py to provide the data from database as json, then show it with js (highchart, d3 or anything)

best regards,
stifan

Maurice Waka

unread,
Dec 21, 2018, 6:09:49 AM12/21/18
to web...@googlegroups.com
It was a small typo.
It worked!
Kind regards

--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/dGkepqrPY0g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages