Hi Christophe,
Thank you so much for your detailed instruction and examples. They are
really helpful and did work for my case. The obstacle here is that I don't
know that I could pass results this way in webpy. :-) Really appreciate
your great help!
Best Regards,
-Yongzhi
On Tuesday, July 14, 2015 at 2:02:23 AM UTC-7, Zbouboutchi wrote:
>
> Hi Yongzhi,
>
> I have the feeling that you're trying to send the whole function into the
> template, I'd rather send only the results since it's best to execute all
> the
> code you can in you web.py controller. Here's a minimalist example that
> works
> here and seems good for your need:
>
> ====================== web.py main file ===================
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
>
> import web
>
> urls = (
> '.*', 'hello'
> )
> app = web.application(urls, globals())
>
> render = web.template.render('templates')
>
> def get_values():
> return {'sensor1': 5,
> 'sensor2': 10,
> 'freespace': 453083058}
>
> class hello:
> def GET(self):
> values = get_values()
> return render.test(values = values)
>
> if __name__ == "__main__":
> app.run()
> ===========================================================
>
> ===============templates/test.html====================
> $def with (values)
> Hello reader, this is a test !<br/>
> <br/>
> sensor 1 : $values['sensor1']<br/>
> sensor 2 : $values['sensor2']<br/>
> free space: $values['freespace']<br/>
> ======================================================
>
> I get the following result in my browser:
> Hello reader, this is a test !
>
> sensor 1 : 5
> sensor 2 : 10
> free space: 453083058
>
> The function get_values() returns a dictionary containing all the values
> you
> want into an associative array ( {key: value} ). This way seems convenient
> with your question about sending seamlessly multiple values in your
> template
> considering you'll always be able to pack new values unless you give them
> another key in the dictionary.
> Notice that I sent the result of the function (assigned into the values
> object) but not the function itself. The values are calculated into the
> web.py
> class then sent to the template.
>
> In my example I give a complete dictionary at once but there are several
> ways
> to build dictionaries. If you get values one by one, the most affordable
> method might be to start with an empty one and to add the values when you
> get
> them:
>
> sample_dict = {}
> sample_dict['data_1'] = foo_function()
> sample_dict['data_2'] = bar_function()
>
> the resulting dictionary will be somewhat like this one:
> {'data_1': 'whatever foo_function() returned',
> 'data_2': 'whatever bar_function() returned'}
>
> You'll be able to get the values in the dictionary by referencing them in
> your
> template like this $sample_dict['data_1'].
>
> More informations about dict can be found in the python documentation.
> https://docs.python.org/2/library/stdtypes.html section 5.8
> https://docs.python.org/2/tutorial/datastructures.html section 5.5
>
> Hope this example helps :)
>
> Regards,
>
> Christophe.
>
>
> Le lundi 13 juillet 2015, 16:10:09 [email protected] <javascript:> a
> écrit :
> > Hi all,
> >
> > I want to display/update several metrics in a normal page (not in a
> form).
> > These metrics got updated every minute and were stored in a log file. I
> > prepared a function to open that log file then analyze the last several
> > lines to collect them. I want these metrics got updated in every page
> > load/refresh.
> >
> > The first method I considered is to utilize Templetor (
> > http://webpy.org/docs/0.3/templetor). I used $code block in the
> template
> > but figured out soon that this solution won't work for the security
> reason.
> > In my function I use open which is prohibited by webpy.
> >
> > Then I thought of `Import functions into templates` (
> >
> https://github.com/webpy/webpy.github.com/blob/master/cookbook/template_impo
> > rt.md). In my case, there is no argument for that function. I followed
> the
> > instruction but got the following error.
> >
> > checknow() takes no arguments (1 given)
> >
> >
> > #in my application.py:
> > def checknow():
> > ...
> > return TN_str
> >
> > render = web.template.render('templates/',globals={'stat':checknow})
> >
> > #in the template:
> > $def with(checknow)
> > ... ...
> > <h1><div>Test: $stat(checknow)</div></h1>
> >
> > By the way, how to refer multiple values of the function checknow()? The
> > function checknow() should return multiple values in the real case.
> Could
> > you please help me out? Thanks a lot.
> >
> > Best Regards,
> > -Yongzhi
>
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/d/optout.