Re: [web2py] embed image from variable
Thank you Massimo. Everything is working now. I misunderstood something. Michele, thanks for the tip! On Thursday, October 31, 2013 1:50:02 PM UTC-4, Michele Comitini wrote: Maybe you want embed in html which is much cooler it requires only one single request/response: def index(): form = SQLFORM.factory(Field('x')).process() img64 = None if form.accepted: img = myplotfunction(form.vars.x) img64 = embed64(data=img,extension='image/png') # - ! if img not png then change extension ! return locals() {{=form}} {{if form.accepted:}} img src={{=img64}} / {{pass}} 2013/10/31 Massimo Di Pierro massimo@gmail.com javascript: Not sure I understand. You have every piece you need. def index(): form = SQLFORM.factory(Field('x')).process() return locals() and in index.html() {{=form}} {{if form.accepted:}} img src={{=URL(f='myplotfunction'**, vars=dict(myvar=** form.vars.x))}}/ {{pass}} On Wednesday, 30 October 2013 14:55:50 UTC-5, Josh Myers wrote: I have all this working the same way as Massimo suggested with matplotlib, no problems. But, I can't for the life of me figure out how to pass a variable into the controller plot function from the view. This would be so I can, for example, ask the user to choose a variable through a form and then produce a histogram of that variable with matplotlib. Right now I have it working but only if the variable for the histogram is preset in the controller plot function. I tried to set the variable with request.vars like so: img src={{=URL(f='myplotfunction'**, vars=dict(myvar=** userselectedvar))}}/ And then simply use request.vars.myvar in the function: def myplotfunction(): ... return ... Where am I going wrong? I am misunderstanding syntax or how request.vars works? I would appreciate any help. - Josh -- 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 javascript:. For more options, visit https://groups.google.com/groups/opt_out. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
Maybe you want embed in html which is much cooler it requires only one single request/response: def index(): form = SQLFORM.factory(Field('x')).process() img64 = None if form.accepted: img = myplotfunction(form.vars.x) img64 = embed64(data=img,extension='image/png') # - ! if img not png then change extension ! return locals() {{=form}} {{if form.accepted:}} img src={{=img64}} / {{pass}} 2013/10/31 Massimo Di Pierro massimo.dipie...@gmail.com Not sure I understand. You have every piece you need. def index(): form = SQLFORM.factory(Field('x')).process() return locals() and in index.html() {{=form}} {{if form.accepted:}} img src={{=URL(f='myplotfunction'**, vars=dict(myvar=** form.vars.x))}}/ {{pass}} On Wednesday, 30 October 2013 14:55:50 UTC-5, Josh Myers wrote: I have all this working the same way as Massimo suggested with matplotlib, no problems. But, I can't for the life of me figure out how to pass a variable into the controller plot function from the view. This would be so I can, for example, ask the user to choose a variable through a form and then produce a histogram of that variable with matplotlib. Right now I have it working but only if the variable for the histogram is preset in the controller plot function. I tried to set the variable with request.vars like so: img src={{=URL(f='myplotfunction'**, vars=dict(myvar=** userselectedvar))}}/ And then simply use request.vars.myvar in the function: def myplotfunction(): ... return ... Where am I going wrong? I am misunderstanding syntax or how request.vars works? I would appreciate any help. - Josh -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
I have all this working the same way as Massimo suggested with matplotlib, no problems. But, I can't for the life of me figure out how to pass a variable into the controller plot function from the view. This would be so I can, for example, ask the user to choose a variable through a form and then produce a histogram of that variable with matplotlib. Right now I have it working but only if the variable for the histogram is preset in the controller plot function. I tried to set the variable with request.vars like so: img src={{=URL(f='myplotfunction', vars=dict(myvar=userselectedvar))}}/ And then simply use request.vars.myvar in the function: def myplotfunction(): ... return ... Where am I going wrong? I am misunderstanding syntax or how request.vars works? I would appreciate any help. - Josh -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
Not sure I understand. You have every piece you need. def index(): form = SQLFORM.factory(Field('x')).process() return locals() and in index.html() {{=form}} {{if form.accepted:}} img src={{=URL(f='myplotfunction', vars=dict(myvar=form.vars.x))}}/ {{pass}} On Wednesday, 30 October 2013 14:55:50 UTC-5, Josh Myers wrote: I have all this working the same way as Massimo suggested with matplotlib, no problems. But, I can't for the life of me figure out how to pass a variable into the controller plot function from the view. This would be so I can, for example, ask the user to choose a variable through a form and then produce a histogram of that variable with matplotlib. Right now I have it working but only if the variable for the histogram is preset in the controller plot function. I tried to set the variable with request.vars like so: img src={{=URL(f='myplotfunction', vars=dict(myvar=userselectedvar))}}/ And then simply use request.vars.myvar in the function: def myplotfunction(): ... return ... Where am I going wrong? I am misunderstanding syntax or how request.vars works? I would appreciate any help. - Josh -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] embed image from variable
Hi, I like to add Graphs with matplotlib directly into my sites. I used a web2py slice to get an image into a variable: from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure import cStringIO def plot(title='title',xlab='x',ylab='y',mode='plot', data={'xxx':[(0,0),(1,1),(1,2),(3,3)], 'yyy':[(0,0,.2,.2),(2,1,0.2,0.2),(2,2,0.2,0.2),(3,3,0.2,0.3 )]}): fig=Figure() fig.set_facecolor('white') ax=fig.add_subplot(111) if title: ax.set_title(title) if xlab: ax.set_xlabel(xlab) if ylab: ax.set_ylabel(ylab) legend=[] keys=sorted(data) for key in keys: stream = data[key] (x,y)=([],[]) for point in stream: x.append(point[0]) y.append(point[1]) if mode=='plot': ell=ax.plot(x, y) legend.append((ell,key)) if mode=='hist': ell=ax.hist(y,20) #if legend: #ax.legend([x for (x,y) in legend], [y for (x,y) in legend], # 'upper right', shadow=True) canvas=FigureCanvas(fig) stream=cStringIO.StringIO() canvas.print_png(stream) return stream.getvalue() def pcolor2d(title='title',xlab='x',ylab='y', z=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]]): fig=Figure() fig.set_facecolor('white') ax=fig.add_subplot(111) if title: ax.set_title(title) if xlab: ax.set_xlabel(xlab) if ylab: ax.set_ylabel(ylab) image=ax.imshow(z) image.set_interpolation('bilinear') canvas=FigureCanvas(fig) stream=cStringIO.StringIO() canvas.print_png(stream) return stream.getvalue() this is my controler: def Testimg(): plotimg=plot() return dict(plotimg=plotimg) and this is my view: {{extend 'layout.html'}} Some Content table {{=TR(TD(Test),TD('Value'))}} /table {{=plotimg}} I managed to view the plot but I cant get it embedded into my page with some content above and below. And I get (probably) the binary png displayed as text -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
I guess you can try {{=IMG(_src=plotimg)}} or img src={{=plotimg}} / -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
I tried that but it doesn't show an image it puts the img tag in the html code and fills the src= with the pinary stuff but no image is displayed On Tuesday, January 29, 2013 4:53:33 PM UTC+1, rochacbruno wrote: I guess you can try {{=IMG(_src=plotimg)}} or img src={{=plotimg}} / -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
I did some experimenting the problem is putting the image into the return dict() or into any variable. I got no clue how to fix it or why that is the problem. On Tuesday, January 29, 2013 5:07:16 PM UTC+1, BlueShadow wrote: I tried that but it doesn't show an image it puts the img tag in the html code and fills the src=binary with the pinary stuff but no image is displayed On Tuesday, January 29, 2013 4:53:33 PM UTC+1, rochacbruno wrote: I guess you can try {{=IMG(_src=plotimg)}} or img src={{=plotimg}} / -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] embed image from variable
You do not return the image that way. You need two actions, one only returns plot() (the binary data) and another would be a regular action which display a link to the other action. def myimage(): return plot() def Testing(): return dict() # in view img src={{=URL('myimage')}}/ ... On Tuesday, 29 January 2013 10:53:03 UTC-6, BlueShadow wrote: I did some experimenting the problem is putting the image into the return dict() or into any variable. I got no clue how to fix it or why that is the problem. On Tuesday, January 29, 2013 5:07:16 PM UTC+1, BlueShadow wrote: I tried that but it doesn't show an image it puts the img tag in the html code and fills the src=binary with the pinary stuff but no image is displayed On Tuesday, January 29, 2013 4:53:33 PM UTC+1, rochacbruno wrote: I guess you can try {{=IMG(_src=plotimg)}} or img src={{=plotimg}} / -- --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.