Cheetah #for / #if to output data in lines instead of columns.

43 views
Skip to first unread message

wysiwyg

unread,
Apr 5, 2018, 6:23:55 AM4/5/18
to weewx-user
Hi there !!

I'm playing around with some js charts libraries and I use cheetah to fill up my scripts with the data.

Here is my code:

columns: [
          ['x'
          #for $record in $span($hour_delta=32).records
                , ${record.dateTime.raw}000
          #end for
          ],
          ['outTemp'
          #for $record in $span($hour_delta=32).records
            #if $record.outTemp.has_data
              , #echo '%.2f' % $record.outTemp.raw#
            #else
              , null
            #end if
          #end for
          ],



it works well, but output is like this:

 columns: [
          ['x'
                , 1522804500000
                , 1522804800000
                , 1522805100000
                , 1522805400000
                , 1522805700000
// Here I remove many lines, for easyer reading...        
                , 1522918200000
                , 1522918500000
                , 1522918800000
                , 1522919100000
                , 1522919400000
          ],
          ['outTemp'
              , 10.75
              , 10.74
              , 10.69
              , 10.68
              , 10.61
// Here I remove many lines, for easyer reading...        
              , 10.57
              , 14.22
              , 14.04
              , 14.19
              , 14.79
              , 15.04
          ],



this make the output file very loooong.

It would be more readable and comfortable to have it like this:

 columns: [
          ['x'      , 1522804500000, 1522804800000, 1522805100000, 1522805400000, 1522805700000  .......          ],
          ['outTemp', 10.75        , 10.74        , 10.69        , 10.68         , 10.61         .......          ],



It looks stupide, but whatever I tried, I was not able to find the suitable cheetah syntax to output this way :-(

Help is welcome !
















mwall

unread,
Apr 5, 2018, 6:30:59 AM4/5/18
to weewx-user
try slurping, like this:

#for $record in $span($hour_delta=32).records
    , ${record.dateTime.raw}000 #slurp
#end for

Frederic Stuyk

unread,
Apr 5, 2018, 7:15:47 AM4/5/18
to weewx...@googlegroups.com
Ah ! good idea!! :-)

It tried this:
columns: [
          ['x'

#for $record in $span($hour_delta=32).records
, ${record.dateTime.raw}000 #slurp
#end for
          ],

and it output this:

  columns: [
          ['x'
, 1522811100000 , 1522811400000 , 1522811700000 , 1522812000000 , 1522812300000 , 1522812600000



But if I try to move the #for one line upper (next to 'x'):
columns: [
          ['x' #for $record in $span($hour_delta=32).records

, ${record.dateTime.raw}000 #slurp
#end for
          ],

it goes back to "column" output.










--
You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/DV_L9WA1YsE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gjr80

unread,
Apr 5, 2018, 8:29:02 AM4/5/18
to weewx-user
So put a #slurp after ['x' in your first attempt/example. #slurp eats EOLs.

Gary
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.

wysiwyg

unread,
Apr 5, 2018, 8:45:42 AM4/5/18
to weewx-user
Hey ! that's perfect !!!!

I was not aware this #slurp could be used anywhere (outside #for etc..)
good to know !!
Good image"slurp eats EOLs", the words sounds like eating something :D

Many thanks, Gary and Matthew !

gjr80

unread,
Apr 5, 2018, 8:49:40 AM4/5/18
to weewx-user
Actually, I got it slightly wrong, #slurp eats newlines.

Gary

wysiwyg

unread,
Apr 5, 2018, 8:57:32 AM4/5/18
to weewx-user
Just to share, if someone needs something similar:

The template:
 columns: [
          ['x'                     #slurp

#for $record in $span($hour_delta=32).records
, ${record.dateTime.raw}000 #slurp
#end for
          ],
          ['$obs.label.outTemp'      #slurp

#for $record in $span($hour_delta=32).records
#if $record.outTemp.has_data
, #echo '%.1f' % $record.outTemp.raw#           #slurp
#else
, null           #slurp

#end if
#end for
          ],
          ['$obs.label.dewpoint'        #slurp
#for $record in $span($hour_delta=32).records
#if $record.dewpoint.has_data
, #echo '%.1f' % $record.dewpoint.raw#           #slurp
#else
, null          #slurp

#end if
#end for
          ],
          ['$obs.label.windchill' #slurp
#for $record in $span($hour_delta=32).records
#if $record.windchill.has_data
, #echo '%.1f' % $record.windchill.raw#            #slurp
#else
, null          #slurp

#end if
#end for
          ],


Output:

         columns: [
          ['x'                     , 1522817700000 , 1522818000000 , 1522818300000 , 1522818600000 , 1522818900000 , 1522819200000 , 152281$
          ['Température ext.'      , 8.6           , 8.5           , 8.5           , 8.5           , 8.3           , 8.3           , 8.3   $
          ['Point de rosée'        , 8.6           , 8.5           , 8.4           , 8.5           , 8.3           , 8.3           , 8.3   $
          ['Température ressentie' , null          , null          , null          , null          , null          , null          , null  $
        ],   /* end columns*/

it needs a bit of tuning of the spaces, but output is very clean and comfortable to read :-)


Reply all
Reply to author
Forward
0 new messages