Tag: ToscaWidgets

  • jqplot in Turbogears

     

    I’ve been working on the Rosenberg jqPlot GraphsNMS graphs slowly migrating them from using rrdtool graph and using jqplot.  While there have been many false-starts and re-works, I now have a working set of graphs, two of which are shown on the page.

    The graphs look a lot slicker and I have also simplified the admin screens.  I found I kept having to type the same thing in over and over for the rrdgraphs and have narrowed down the type of graphs to approximately 5.  They’re certainly not bulletproof and need more testing but they’re a good start.

    The graphs are based upon the ToscaWidgets2 series of widgets that then provide a nice “handle” for the jqPlot code.  My graphs even have some hand-coded Javascript to give nice units on the Y axis.

     

    Enhanced by Zemanta
  • Pie Charts in TurboGears

    You might of looked at Ralph Bean’s tutorial on graphs and thought, that’s nice but I’d like something different.  The great thing about ToscaWidgets using the jqPlot library is that pretty much anything you can do in jqPlot, you can do in ToscaWidgets and by extension in TurboGears.  You want different graph types? jqPlot has heaps!

    My little application needed a Pie Chart to display the overall status of attributes. There are 6 states an attribute can be in: Up, Alert, Down, Testing, Admin Down and Unknown.  The Pie Chart would show them all with some sort of colour representing the status. For this example I’ve used hard-coded data but you would normally extract it from the database using a TurboGears model and possibly some bucket sorting code.

    I’ve divided my code into widget specific, which is found in myapp/widgets/attribute.py and the controller code found unsurprisingly at myapp/controllers/attribute.py  Also note that some versions of ToscaWidgets have a bug which means any jqPlot widget won’t work, version 2.0.4 has the fix for issue 80 that explains briefly the bug.

    The widget code looks like this:

    from tw2.jqplugins.jqplot import JQPlotWidget
    from tw2.jqplugins.jqplot.base import pieRenderer_js
    import tw2.core as twc
    
    class AttributeStatusPie(JQPlotWidget):
        """
        Pie Chart of the Attributes' Status """
        id = 'attribute-status-pie'
        resources = JQPlotWidget.resources + [
                pieRenderer_js,
                ]
    
        options = {
                'seriesColors': [ "#468847", "#F89406", "#B94A48", "#999999", "#3887AD", "#222222"],
                'seriesDefaults' : {
                    'renderer': twc.js_symbol('$.jqplot.PieRenderer'),
                    },
                'legend': {
                    'show': True,
                    'location': 'e',
                    },
                }

    Some important things to note are:

    • resources are the way of pulling in the javascript includes that actually do the work, generally if you have something like a renderer using js_symbol further on, it needs to be listed in the resources too.
    • seriesColors is how you make a specific data item a specific colour, or perhaps change the range of colours.  It’s not required if you use the default set, which is defined in the jqPlot options.
    • The renderer tells jqPlot what sort of graph we want, the line above says we want pie

     

    Next the controller needs to be defined:

    from myapp.widgets.attribute import AttributeStatusPie
    
    @expose('myapp.templates.widget')
        def statuspie(self):
            data = [[
                    ['Up', 20], ['Alert', 7], ['Down', 12], ['Admin Down', 3], ['Testing', 1], ['Unknown', 4],
                    ]]
            pie = AttributeStatusPie(data=data)
            return dict(w=pie)

    And that is about it, we now have a controller path attributes/statuspie which shows us the pie chart.
    My template is basically a bare template with a ${w.display | n} in it to just show the widget for testing.

    Pie Chart in Turbogears
    Pie Chart in Turbogears

     

    Enhanced by Zemanta
  • Pre-selecting ToscaWidgets jqgrid values in TurboGears

    My Turbogears project has recently reached an important milestone; one of the back-end processes now runs pretty much continuously and the plugins it use (or at least the ones I can see) are also working.  That means I can turn to the front-end which displays the data the back-end collected.

    For some of the data I am using a ToscaWidgets (or TW2) widget called a jqGridWidget which is a very nice jquery device that separates the presentation and data using a json query.  I’ve mentioned previously about how to get a jqGridWidget working but left the pre-filtering out until now.  This meant that my grid showed all the items in the database, not just the ones I wanted to see, but it was a start.

    Now this widget displays things called Attributes which are basically children of another model called Hosts. Basically, Attributes are things you want to check or track about a Host.  My widget used to show all Attributes but often on a Host screen, I want to see its hosts only. So, this is how I got my widget to behave; I’m not sure this is THE CORRECT way of doing it, but it does work.

    First, in the Hosts controller you need to create the widget and pass along the host_id that the controller has obtained.  I was not able to use the sub-classing trick you see in some TW2 documentation but actually make a widget instance.

    class HostsController(BaseController):
        # other stuff here
        class por2(portlets.Portlet):
            title = 'Host Attributes'
            widget = AttributeGrid()
            widget.host_id = host_id

    Next, the prepare() method in the Widget needs to get hold of the host_id and put it into the postData list.  I needed to do it before calling super() because the options become one long string in the sub-class.

    class AttributeGrid(jqgrid.jqGridWidget):
        def prepare(self, **kw):
            if self.host_id is not None:
                self.options['postData'] = {'hostid': self.host_id}
            super(AttributeGrid, self).prepare()

    This means the jqgrid when it asks for its JSON data will include the hostid parameter as well.  We need that method to “see” the host ID so we can filter the database access.

    Finally in the JSON method for the Attribute we pick up and filter on the hostid.

        @expose('json')
        @validate(validators={'hostid':validators.Int()})
        def jqsumdata(self, hostid=0, page=1, rows=1, *args, **kw):
            conditions = []
            if hostid > 0:
                conditions.append(model.Attribute.host_id == hostid)
            attributes =model.DBSession.query(model.Attribute).filter(and_(*conditions))

    From there you run through the attributes variable and build your JSON reply.

     

    Enhanced by Zemanta