【问题标题】:Python -- Turn Tree into HTML calendarPython——把树变成 HTML 日历
【发布时间】:2014-04-03 23:32:16
【问题描述】:

使用这个:

def Tree(): return defaultdict(Tree)

我为日历创建了一个可用日期树:

calendar = Tree()
# print calendar

calendar['2013']['10']=['1','2','3','4','6']
calendar['2013']['12']=['5','7','12']
calendar['2013']['11']=['8','10','19']

calendar['2014']['1']=['1','2','3','4','6']
calendar['2014']['2']=['5','7','12']
calendar['2014']['3']=['8','10','19']

print calendar

    for year in calendar:
        print "year:",year
        for month in calendar[year]:
            print "\tMonth:",month
            for day in calendar[year][month]:

打印 "\t\tday:",day

如何使这些日期成为 HTML <input type="date"> 中唯一可用的日期?

我正在使用瓶子,并且可以将树作为变量传递:

@bottle.route('/test')
def run_test_page():

    calendar = Tree()
    calendar['2013']['10']=['1','2','3','4','6']
    calendar['2013']['12']=['5','7','12']
    calendar['2013']['11']=['8','10','19']

    calendar['2014']['1']=['1','2','3','4','6']
    calendar['2014']['2']=['5','7','12']
    calendar['2014']['3']=['8','10','19']

    return bottle.template('main/test.tpl', {'date_list': calendar})

到目前为止我的 main/test.tpl:

{{date_list}}
<br><br>        
<input type="date">

编辑:刚刚意识到 input type='date' 在 IE 和 chrome 中不起作用......所以我可能不得不使用 datepicker 或一些 js?

【问题讨论】:

    标签: python html tree bottle


    【解决方案1】:

    我找到了一个解决方案,但它可能是绕圈子。可以更pythonic吗?

    首先,我将树更改为具有两位数的日期:

    @bottle.route('/test')
    def run_test_page():
    
        calendar = Tree()
        calendar['2013']['10']=['01','02','03','04','06']
        calendar['2013']['12']=['05','07','12']
        calendar['2013']['11']=['08','10','19']
    
        calendar['2014']['01']=['01','02','03','04','06']
        calendar['2014']['02']=['05','07','12']
        calendar['2014']['03']=['08','10','19']
        calendar['2014']['04']=['01','02','03','04']
        return bottle.template('main/test.tpl', {'date_list': calendar})
    

    其次,这里是test.tpl文件(我知道我应该有一个单独的js文件):

    <head>
        <script src="jquery-2.1.0.js"></script>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
        <script src="jquery.ui.core.js"></script>
        <script src="jquery.ui.datepicker.js"></script>
    
    
    </head>
    
    <p>{{date_list}}</p>
    
    
    <p>available dates for 2013: {{date_list['2013']}}</p>
    <p>available dates for 2014: {{date_list['2014']}}</p>
    %available_dates = ""
    <p>available days:
        %for year in date_list:
        {{year}}
        %for month in date_list[year]:
        %for day in date_list[year][month]:
        %available_dates+= month+"-"+day+"-"+year+","
        %end
        %end
        %end
        %available_dates=available_dates[:-1] # get rid of last comma
    </p>
    <input id='availableDates' type="hidden" value="{{available_dates}}">
    <br><br>
    Select Start Date:
    <input name="start_date" class="datepicker" id="start_date" type="text">
    
    
    <script>
        var availableDates = ($('#availableDates').val()).split(",");
    
        function available(date) {
            dmy =  ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "-" + date.getFullYear();
            console.log(dmy)
            if ($.inArray(dmy, availableDates) !== -1) {
                return [true, "", "Available"];
            } else {
                return [false, "", "unAvailable"];
            }
        }
    
        $(function(){
        $("#start_date").datepicker({
            dateFormat: "mm-dd-yy",
            beforeShowDay: available
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多