【问题标题】:Conditional DateFormatter in Bokeh tablesBokeh 表中的条件 DateFormatter
【发布时间】:2020-08-12 10:53:45
【问题描述】:

我可以在散景表中为我的日期格式化程序做一个静态选择

TableColumn(field=xdata, title=xdata, formatter=DateFormatter(format="%a, %e %b %y")),

但我想做一些更动态的事情,如果距离当前时间不到几个小时,则显示一天中的时间,否则显示完整日期(天+时间)

其他问题似乎集中在单元格背景和类似的东西上,具体取决于条件,所以我找不到真正适合此的先前提出的问题。

【问题讨论】:

  • HTMLTemplateFormatter 可以使用任意 JavaScript 格式化值。

标签: bokeh date-formatting dateformatter


【解决方案1】:

完成这项工作的我丑陋的 JS 如下

    template="""
 <%= (
         function nameless()
         { 
            function shortenDateTime(inputDate /* Date() */
                                     ,threshold /*[seconds]*/) {
              function farAway(present /* integer unit epoch */, past /* integer unit epoch */) {
                if ( present - past > threshold*1000){
                  return true;
                }   else {
                  return false;
                }
              }

              // get a new date (locale machine date time)
              var now = new Date();
              var now_epoch = now.getTime();


              inputDate_epoch = inputDate.getTime();
              date = inputDate.toDateString();
              time = inputDate.toLocaleTimeString();

              if (farAway(now_epoch,inputDate_epoch)) {
                return date + ' ' + time;
              } else {
                return time;
              }
            };
            var _date = new Date(value);
            return shortenDateTime(_date)
         }()
     ) %>;
"""

然后必须从HTMLTemplateFormatter定义一个格式化程序

formater =  HTMLTemplateFormatter(template=template)

并在列的定义中使用它

TableColumn(field=xdata, width=22, title=xdata, formatter=formater),

我剩下的唯一问题与:

  • 在输出12:09:33; 的末尾删除;
  • 真的有必要使用我定义nameless()的技巧吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2015-10-09
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多