ok... I'm slowing getting there...

now, I've got a file "test/views/testfigure/index.html" where I have
{{extend 'layout.html'}}
<h1>This is the testfigure/index.html template</h1>
<img src="{{=URL(r=request,f='../matplotlib/simple')}}" />
<img src="{{=URL(r=request,f='simple')}}" />

and a file "test/controllers/matplotlib.py" that provided the PNG
stream

<img src="{{=URL(r=request,f='../matplotlib/simple')}}" /> works fine,
but
<img src="{{=URL(r=request,f='simple')}}" /> does not
Is it a good practice to use a relative path or am I missing anything?

thanks!
aus



On Dec 17, 3:45 pm, mdipierro <[email protected]> wrote:
> consider the previous example with mathplotlib:
>
>     def simple(): ..... return stream.getvalue()
>
> now from a template you can do
>
>     <img src="{{=URL(r=request,f='simple')}}" />
>
> and this will include the image returned by the simple() action.
>
> Massimo
>
> On Dec 17, 8:38 am, aus <[email protected]> wrote:
>
> > is there any example for doing that? I just cannot figure out how to
> > call a python function (action) from a template...
>
> > thanks!
> > aus
>
> > On Dec 17, 7:31 am, mdipierro <[email protected]> wrote:
>
> > > <img src="{{=embed64(data=imagebinarydata)}}" />
>
> > > will work (not on IE).
>
> > > Be aware that nobody ever does this. Nobody ever embeds images in
> > > HTML. Although it is cool in come cases.
> > > People normally just have source point to the action that generates
> > > the PNG.
>
> > > Massimo
>
> > > On Dec 16, 6:43 pm, aus <[email protected]> wrote:
>
> > > > here I am again...
> > > > I fell like a moron, but I just cannot have it work...
>
> > > > so, the last line of the controller is:
> > > > return dict(imagebinarydata=stream.getvalue())
>
> > > > and this is the corresponding view:
> > > > {{extend 'layout.html'}}
> > > > <h1>This is the matplotlib/simple.html template</h1>
> > > > <img src="{{=embed64(data = {{=imagebinarydata}} ) }}" />
>
> > > > unfortunately the syntax is wrong:
>
> > > > response.write(embed64(data = {{=imagebinarydata)
> > > >                                 ^
> > > > SyntaxError: invalid syntax
>
> > > > and I don't quite understand why.
> > > > Is it also necessary to include extension='image/png' in the call to
> > > > embed64?
>
> > > > Thanks!
>
> > > > Cheers,
> > > > aus
>
> > > > On Dec 17, 1:02 am, aus <[email protected]> wrote:
>
> > > > > Thank you, Massimo!
> > > > > Sorry for the multiple posts... I didn't mean it :)
>
> > > > > cheers,
> > > > > ale
>
> > > > > On Dec 16, 7:26 pm, mdipierro <[email protected]> wrote:
>
> > > > > > You have to do it the old fashion way
>
> > > > > > <img src="url_to_the_png_action" />
>
> > > > > > web2py allows you to do
>
> > > > > > <img src="{{=embed64(data='imagebinarydata')}}" /> but this is not
> > > > > > supported by IE.
>
> > > > > > Massimo
>
> > > > > > On Dec 16, 11:59 am, aus <[email protected]> wrote:
>
> > > > > > > now the question is how to place the figure in a variable to be 
> > > > > > > used
> > > > > > > in a template. What's the best way of doing that? Should I pack 
> > > > > > > the
> > > > > > > stream into a dict? How should the view look like?
>
> > > > > > > thanks!
> > > > > > > aus
>
> > > > > > > On Dec 16, 5:27 pm, aus <[email protected]> wrote:
>
> > > > > > > > ok, I found it, I mistyped 'Content-Type', my fault
>
> > > > > > > > thanks!
> > > > > > > > aus
>
> > > > > > > > On Dec 16, 5:04 pm, aus <[email protected]> wrote:
>
> > > > > > > > > ok, it works, but firefox displays the raw data, not a 
> > > > > > > > > figure. I guess
> > > > > > > > > I need an appropriate view for that, am I right?
>
> > > > > > > > > Cheers
> > > > > > > > > aus
>
> > > > > > > > > On Dec 16, 4:56 pm, mdipierro <[email protected]> wrote:
>
> > > > > > > > > >     response.header['content-type']='image/png'
>
> > > > > > > > > > should have been
>
> > > > > > > > > >     response.headers['Content-Type']='image/png'
>
> > > > > > > > > > Massimo
>
> > > > > > > > > > On Dec 16, 9:48 am, aus <[email protected]> 
> > > > > > > > > > wrote:
>
> > > > > > > > > > > Hi Massimo,
>
> > > > > > > > > > > thank you for the *really* prompt reply!
> > > > > > > > > > > I managed to have Dojox charts work, but I'm getting a 
> > > > > > > > > > > error message
> > > > > > > > > > > form the "matplotlib" code (btw, "import datetime" was 
> > > > > > > > > > > missing). I
> > > > > > > > > > > pasted the Dojo code at the bottom of this email, just in 
> > > > > > > > > > > case I'm not
> > > > > > > > > > > the only beginner around :)
>
> > > > > > > > > > > Traceback (most recent call last):
> > > > > > > > > > >   File 
> > > > > > > > > > > "/home/aus/Software/Web2py/web2py/gluon/restricted.py", 
> > > > > > > > > > > line
> > > > > > > > > > > 62, in restricted
> > > > > > > > > > >     exec ccode in environment
> > > > > > > > > > >   File 
> > > > > > > > > > > "/home/aus/Software/Web2py/web2py/applications/test/controllers/
> > > > > > > > > > > matplotlib.py", line 29, in <module>
> > > > > > > > > > >   File 
> > > > > > > > > > > "/home/aus/Software/Web2py/web2py/gluon/globals.py", line 
> > > > > > > > > > > 55,
> > > > > > > > > > > in <lambda>
> > > > > > > > > > >     self._caller=lambda f: f()
> > > > > > > > > > >   File 
> > > > > > > > > > > "/home/aus/Software/Web2py/web2py/applications/test/controllers/
> > > > > > > > > > > matplotlib.py", line 23, in simple
> > > > > > > > > > >     response.header['content-type']='image/png'
> > > > > > > > > > > TypeError: 'NoneType' object does not support item 
> > > > > > > > > > > assignment
>
> > > > > > > > > > > what's wrong with the response?
>
> > > > > > > > > > > thank you once again!
>
> > > > > > > > > > > Cheers,
> > > > > > > > > > > aus
>
> > > > > > > > > > > Dojo / Dojox charting / JSON example:
>
> > > > > > > > > > > <script type="text/javascript">
>
> > > > > > > > > > > dojo.require("dojox.charting.Chart2D");
> > > > > > > > > > > {{import gluon.contrib.simplejson as sj}}
>
> > > > > > > > > > > function plott(){
> > > > > > > > > > >     //var dd = dojox.json.ref.fromJson(data);
> > > > > > > > > > >     //var data = [1,3,2,4,3,6];
> > > > > > > > > > >     console.debug({{=sj.dumps(data)}});
> > > > > > > > > > >     var chart1 = new 
> > > > > > > > > > > dojox.charting.Chart2D("simplechart");
> > > > > > > > > > >     chart1.addPlot("default", {type: "Lines"});
> > > > > > > > > > >     chart1.addAxis("x");
> > > > > > > > > > >     chart1.addAxis("y", {vertical: true});
> > > > > > > > > > >     chart1.addSeries("Series 1", {{=sj.dumps(data)}});
> > > > > > > > > > >     chart1.render();};
>
> > > > > > > > > > > dojo.addOnLoad(plott);
>
> > > > > > > > > > > </script>
>
> > > > > > > > > > > On Dec 16, 3:49 pm, mdipierro <[email protected]> 
> > > > > > > > > > > wrote:
>
> > > > > > > > > > > > In web2py+matplotlib:
>
> > > > > > > > > > > > def simple():
> > > > > > > > > > > >     import random, cStringIO
>
> > > > > > > > > > > >     from matplotlib.backends.backend_agg import 
> > > > > > > > > > > > FigureCanvasAgg as
> > > > > > > > > > > > FigureCanvas
> > > > > > > > > > > >     from matplotlib.figure import Figure
> > > > > > > > > > > >     from matplotlib.dates import DateFormatter
>
> > > > > > > > > > > >     fig=Figure()
> > > > > > > > > > > >     ax=fig.add_subplot(111)
> > > > > > > > > > > >     x=[]
> > > > > > > > > > > >     y=[]
> > > > > > > > > > > >     now=datetime.datetime.now()
> > > > > > > > > > > >     delta=datetime.timedelta(days=1)
> > > > > > > > > > > >     for i in range(10):
> > > > > > > > > > > >         x.append(now)
> > > > > > > > > > > >         now+=delta
> > > > > > > > > > > >         y.append(random.randint(0, 1000))
> > > > > > > > > > > >     ax.plot_date(x, y, '-')
> > > > > > > > > > > >     
> > > > > > > > > > > > ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
> > > > > > > > > > > >     fig.autofmt_xdate()
> > > > > > > > > > > >     canvas=FigureCanvas(fig)
> > > > > > > > > > > >     response.header['content-type']='image/png'
> > > > > > > > > > > >     stream=cStringIO.StringIO()
> > > > > > > > > > > >     canvas.print_png(stream)
> > > > > > > > > > > >     return stream.getvalue()
>
> > > > > > > > > > > > Massimo
>
> > > > > > > > > > > > On Dec 16, 6:12 am, aus <[email protected]> 
> > > > > > > > > > > > wrote:
>
> > > > > > > > > > > > > Hi Massimo,
>
> > > > > > > > > > > > > thank you for your reply. Your suggestion does not 
> > > > > > > > > > > > > seem to work, but
> > > > > > > > > > > > > maybe I'm not using Dojo properly. How would you code 
> > > > > > > > > > > > > the plot with
> > > > > > > > > > > > > flot?
>
> > > > > > > > > > > > > Also, it would be great to be able to embed 
> > > > > > > > > > > > > images/charts generated by
> > > > > > > > > > > > > matplotlib, similarly to what can be done with 
> > > > > > > > > > > > > django:http://www.scipy.org/Cookbook/Matplotlib/Django.
> > > > > > > > > > > > >  Is there a similar
> > > > > > > > > > > > > feature in web2py?
>
> > > > > > > > > > > > > Thank you!
>
> > > > > > > > > > > > > Cheers,
> > > > > > > > > > > > > aus
>
> > > > > > > > > > > > > On Dec 16, 6:57 am, mdipierro 
> > > > > > > > > > > > > <[email protected]> wrote:
>
> > > > > > > > > > > > > > I do not know about Dojo. I have been using flot 
> > > > > > > > > > > > > > instead. perhaps this
> > > > > > > > > > > > > > helps
>
> > > > > > > > > > > > > > {{import gluon.contrib.simplejson as sj}}
> > > > > > > > > > > > > > {{=sj.dumps(data)}}
>
> > > > > > > > > > > > > > data will be printed in the page in json format. If 
> > > > > > > > > > > > > > your view/template
> > > > > > > > > > > > > > includes javascript code you can put the above 
> > > > > > > > > > > > > > statements in the js
> > > > > > > > > > > > > > itself.
>
> > > > > > > > > > > > > > Perhaps other users can help more.
>
> > > > > > > > > > > > > > Massimo
>
> > > > > > > > > > > > > > On Dec 15, 11:31 pm, aus 
> > > > > > > > > > > > > > <[email protected]> wrote:
>
> > > > > > > > > > > > > > > Hi everybody,
>
> > > > > > > > > > > > > > > although I have some experience with programming, 
> > > > > > > > > > > > > > > javascript/AJAX is
> > > > > > > > > > > > > > > completely new to me. I'm very happy with web2py 
> > > > > > > > > > > > > > > and I'm trying now to
> > > > > > > > > > > > > > > pass arrays and possibly more structured data to 
> > > > > > > > > > > > > > > Dojox to plot series
> > > > > > > > > > > > > > > computed in python using Dojox's charts. How can 
> > > > > > > > > > > > > > > I do that? I somehow
> > > > > > > > > > > > > > > cannot cross the wall between the templace code 
> > > > > > > > > > > > > > > and the javascript
> > > > > > > > > > > > > > > code... Is JSON a possible solution? Could 
> > > > > > > > > > > > > > > someone provide an example?
>
> > > > > > > > > > > > > > > thank you!
>
> > > > > > > > > > > > > > > Cheers,
> > > > > > > > > > > > > > > aus
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to