After reinstall python and TG2, finally I can get things working

Only one issue, this is the code looks like now:

# -*- coding: utf-8 -*-
"""Main Controller"""

from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _, lazy_ugettext as l_

from mplibtest.lib.base import BaseController
from mplibtest.model import DBSession, metadata
from mplibtest.controllers.error import ErrorController

import StringIO
import matplotlib
import numpy
from matplotlib.backends.backend_agg import FigureCanvasAgg
from numpy import arange, sin, pi

__all__ = ['RootController']


class RootController(BaseController):

    error = ErrorController()


    @expose('mplibtest.templates.index')
    def index(self):
        """Handle the front-page."""
        return dict(page='index')

    @expose('mplibtest.templates.about')
    def about(self):
        """Handle the 'about' page."""
        return dict(page='about')

    def expose_matplot_figure(f=None, **render_opts):

        def get_fig(result):
            if isinstance(result, dict):
                            fig = result['fig']
                            del result['fig']
                            return fig
            else:
                return result

                def get_render_opts(result):
                    if isinstance(result, dict):
                            render_opts.update(result)
                    return render_opts

        def wrap_f_with_render_figure(f):
                    def render_figure(*pargs, **vargs):
                            # Make the PNG
                            result = f(*pargs, **vargs)
                            canvas = FigureCanvasAgg(get_fig(result))
                            rendered_fig = StringIO.StringIO()
                            canvas.print_figure(rendered_fig)
                            return rendered_fig.getvalue()
                    return expose(content_type="image/png")(render_figure)

        if f is None:
                    # This corresponds to the usage @expose_matplotlib_figure
(dpi=20)
                    return wrap_f_with_render_figure
        else:
                    # This corresponds to the usage @expose_matplotlib_figure
                    return wrap_f_with_render_figure(f)

    def sample_fig(self):
        fig = matplotlib.figure.Figure(figsize=(9, 6))
        fig.Name = "Sinewave"
        ax = fig.add_subplot(111)
        ax.set_xlabel("angle")
        ax.set_ylabel("amplitude")
        t = arange(0.0, 1.0+0.01, 0.01)
        s1 = sin(2*pi*t)
        ax.plot(t, s1, color="k")
        return fig

    @expose_matplot_figure
    def sinegraph_default_dpi(self):
        return self.sample_fig()

   and the url is http://localhost:8080/sinegraph_default_dpi

If you pay attention, the code is almost the same as the
http://docs.turbogears.org/1.0/GenerateFigures#id2 example except for
one line of code in the wrap_f_with_render_figure method:

canvas.print_figure(rendered_fig, **get_render_opts(result))

if I put the second parameter in that line of code I get this error:

NameError: global name 'get_render_opts' is not defined

Seems to be a newbie error to me but I can't figured out how to solved

thanks for the help
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to