[PYTHON] Schedule table

Execution environment

Ubuntu Stdio 18.04LTS Python 3.6.9 Firefox 71.0 (64-bit)

Referenced site

Create your own TODO app with Python https://news.mynavi.jp/article/zeropython-57/

Try using JavaScript Example 11: Creating a calendar http://home.e02.itscom.net/shouji/pc/js/p-11.html

Other

schedule.png

I'm looking for a way to resize the add button, but I can't find it.

The opportunity to make

I was reading the article "Let's make my own TODO application with Python" and started using it thinking that it could be used as it is for the schedule. Net, it is a combination of sample code on the net by amateurs. Even if I study as I get older, it will be erased from my memory in less than a week, so I am a beginner for all ages.

What I did is the sort function of amateurs in the sample program "Let's make your own TODO application with Python" (If there are half-width and full-width spaces, the lines will be disjointed, so you need to connect with underscores. ) I added it because it is more convenient to have a clock and a calendar. Most of the samples are as they are on the net. (Thank you)

There is no code on the "Let's make your own TODO app with Python" site, but there is a link so that you can download it, so download "The 57th program here" from there. I will.

It didn't work as it was, so I needed to fix it a little. 1.data.txt is not included. If you are familiar with it, you can easily find it by looking at the code, but I don't know, so I created a data.txt file with a suitable list of characters, but it worked fine.

  1. I think it depends on the OS, but change it to #! / Usr / bin / python3 on the first line of todo.py.

    1. It didn't work in python2 (probably lacking library), so change the second line of start.sh from python ... to python3 ... For Windows, I think it's start.bat.
  2. Give execute permission to start.sh. Give execute permission to todo.py.

Now, when you run start.sh, the simple web server starts. Then, when you access "http: // localhost: 8000 / cgi-bin / todo.py" with a browser, the sample program starts and "Compliment TODO" is displayed.

I learned for the first time that HP can be displayed with a python script (program). (But it's troublesome)

I added sort, clock, and calendar to this.

python


#!/usr/bin/python3
# coding: UTF-8

FILE_DATA = "./data.txt"

with open(FILE_DATA) as f:
    s = f.read()
    s1=s.split() #Convert str to List
    s1.sort() #sort
    s = '\n'.join(s1) #Convert List to str

with open(FILE_DATA, mode='w') as f: #writing
    f.write(s)

#Settings required to handle Japanese
import os, sys, io, cgi, re
sys.stdin, sys.stdout, sys.etderr =  [
    open(sys.stdin.fileno(),  'r', encoding='UTF-8'),
    open(sys.stdout.fileno(), 'w', encoding='UTF-8'),
    open(sys.stderr.fileno(), 'w', encoding='UTF-8')]
out = lambda s: print(s, end="\n")

form = cgi.FieldStorage()

#Check the parameters and branch the process
def check_param():
    m = get_form('m', 'show')
    if m == "show":
        mode_show()
    elif m == "add":
        mode_add()
    elif m == "rm":
        mode_remove()
    else:
        out_html("Parameter error")

#Display a list of TODO
def mode_show():
    items = load_items()
    t = "<h1>My calendar</h1>"

    t += '<div class="div_1">'
    t += '<div class="div_2">'
    t += "<div id='items'>"

    t += '<h2 id="time"></h2>' #from here
    t += "<script>"
    t += "time();"
    t += "function time(){"
    t += "    var now = new Date();"
    t += '    document.getElementById("time").innerHTML = now.toLocaleString();'
    t += "}"
    t += "setInterval('time()',1000);"
    t += "</script>"  #Clock so far

    t += "<form><p class='frm'>plans: "  #from here
    t += input_tag('text', 'todo', '')
    t += input_tag('hidden', 'm', 'add')
    t += input_tag('submit', '', 'add to')
    t += "</p></form></div>"  #Scheduled additional part so far

    for no, it in enumerate(items):  #schedule
        k = "todo.py?m=rm&no="+str(no)
        a = "<a href='" + k + "'>❎️</a> "
        t += "<p>" + a + it + "</p>"

    t += "</div>"
    t += '<div class="div_2">'   #calendar
    t += ' <script type="text/javascript"><!--\n'
    t += 'var now = new Date();'
    t += 'var year = now.getFullYear();'
    t += 'var month = now.getMonth() + 1;'
    t += 'var today = now.getDate();'
    t += 'now.setDate(1);'
    t += 'var startDay = now.getDay();'
    t += 'var monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);'
    t += 'var dateMax = monthdays[month - 1];'
    t += 'if (month == 2 && ((year%4 == 0 && year%100 != 0) || year%400 == 0)) dateMax = 29;'
    t += 'var holidays = new Array();'
    t += 'for (var i = 0; i <= dateMax; i++) holidays[i] = 0;'
    t += 'var Holidays1 = new Array(1,1, 2,11, 2,23, 3,21, 4,29, 5,3, 5,4, 5,5, 9,23, 11,3, 11,23);'
    t += 'var firstSunday = (startDay == 0) ? 1: 8 - startDay;'
    t += 'for (i = 0; i < Holidays1.length; i += 2) {'
    t += 'if (Holidays1[i] == month) {'
    t += 'holidays[Holidays1[i+1]] = 1;'
    t += 'for (var j = firstSunday; j < dateMax; j += 7)'
    t += 'if (Holidays1[i+1] == j ) { holidays[j+1] = 1; break; } '
    t += '}'
    t += '}'
    t += 'var Holidays2 = new Array(1,2, 7,3, 9,3, 10,2);'
    t += 'var firstMonday = (startDay < 2) ? 2 - startDay: 9 - startDay;'
    t += 'for (i = 0; i < Holidays2.length; i += 2)'
    t += 'if (Holidays2[i] == month) holidays[(Holidays2[i+1] - 1) * 7 + firstMonday] = 1;'
    t += 'var days = new Array("Day", "Month", "fire", "water", "wood", "Money", "soil");'
    t += '''document.write("<table class='calen'>\\n<tr class='bg1'><th colspan=7>" + year + "Year" + month + "Month</th></tr>\\n");'''
    t += '''document.write("<tr class='bg2'><th class='sun'>" + days[0] + "</th>");'''
    t += 'for (i = 1; i < 6; i++) document.write("<th>" + days[i] + "</th>");'
    t += '''document.write("<th class='sat'>" + days[6] + "</th></tr>\\n");'''
    t += 'var col = 0, s1;'
    t += 'if (startDay > 0) {'
    t += 'document.write("<tr>");'
    t += 'for ( ; col < startDay; col++) document.write("<td>&nbsp;</td>");'
    t += '}'
    t += 'for (i = 1; i <= dateMax; i++) {'
    t += 'if (col == 0) document.write("<tr>");'
    t += 'if (i == today) {'
    t += '''if (holidays[i] == 1 || col == 0) s1 = "<td class='today sun'>";'''
    t += '''else if (col == 6) s1 = "<td class='today sat'>";'''
    t += '''else s1 = "<td class='today'>";'''
    t += '}'
    t += '''else if (holidays[i] == 1 || col == 0) s1 = "<td class='sun'>";'''
    t += '''else if (col == 6) s1 = "<td class='sat'>";'''
    t += 'else s1 = "<td>";'
    t += 'document.write(s1 + i + "</td>");'
    t += 'if (col == 6) { document.write("</tr>\\n"); col = 0; } else col++;'
    t += '}'
    t += 'if (col != 0) {'
    t += 'for ( ; col < 7; col++) document.write("<td>&nbsp;</td>");'
    t += 'document.write("</tr>");'
    t += '}'
    t += 'document.write("</table>");'
    t += '//-->'
    t += '</script>'
    t += '</div>'
    t += '</div>'

    out_html(t)

#Add a new TODO
def mode_add():
    todo = get_form('todo', '')
    if todo == '':
        out_html('No text to add')
        return
    items = load_items()
    items.append(todo)
    save_items(items)
    s = "<div id='items'><p>Written.<br/>"
    s += "Great! I'm always doing my best!</p>"
    s += "<a href='todo.py'>Return</a></div>"
    out_html(s)

#Delete item
def mode_remove():
    no = int(get_form('no', '-1'))
    if no < 0:
        out_html('Wrong number')
        return
    items = load_items()
    del items[no]
    save_items(items)
    out_html('It has been deleted.<a href="todo.py">Return</a>')

#Load item
def load_items():
    text = "test1\ntest2\ntest3\n"
    if os.path.exists(FILE_DATA):
        with open(FILE_DATA, 'rt', encoding="utf-8") as f:
            text = f.read()
    items = text.strip().split("\n")
    return items

#Save item
def save_items(items):
    text = "\n".join(items)
    with open(FILE_DATA, 'wt', encoding="utf-8") as f:
        f.write(text)

#Output HTML
def out_html(html):
    out("Content-type: text/html; charset=utf-8")
    out("")
    out("<html><head>")
    out("<title>My calendar</title>")
    out('<link href="/style.css" rel="stylesheet">')
    out("</head><body>" + html)
    out("</body></html>")

# <input>Returns a tag
def input_tag(type, name, value):
    s = '<input type="{}" name="{}" value="{}">'
    return s.format(type, name, value)

#Gets and returns the value of the form
def get_form(name, defvalue):
    if name in form:
        return form[name].value
    return defvalue

if __name__ == '__main__':
    check_param()

style.css

h1 {
    font-size: 24px;
    background-color: #f05090;
    padding: 8px;
    color: white;
}

#items {
	margin: 12px;
}

#items p {
	font-size: 16px;
	border-bottom: 4px solid #f09090;
	border-right: 6px solid #f09090;
	padding: 12px;
	margin: 12px;
	background-color: #fff0f0;
}
#items a {
	padding: 8px;
	text-decoration: none;
}
#items input[type=text] {
	width: 300px;
	font-size: 16px;
	padding: 8px;
}
#items input[type=submit] {
	font-size: 24px;
	padding: 8px;
	width: 60px;
	margin: 8px;
}

#items p.frm {
	background-color: #fff0ff;
}

.div_1 {
    padding:  10px;             /*Margin specification*/
    display: flex;              /*Make it flexbox*/
}
 
.div_2 {
    padding: 10px;
    margin:  10px;              /*Outer margin*/
    border-radius:  5px;        /*Rounded corners*/
    font-size: 30px;            /*Character size specification*/

}
 
.div_2:nth-child(1) {
    flex-basis:  1100px;         /*Width specification*/

}
 
.div_2:nth-child(2) {
    flex-basis:  200px;         /*Width specification*/
    text-align:  center;        /*Character centering*/
}

/*calendar*/
table.calen { border-collapse:collapse; font-size:11pt; background-color:#fffafa; color:black; }
table.calen th { border:1px solid #a57d6b; height:30px; }
table.calen td { border:1px solid #a57d6b; text-align:center; height:30px; width:30px; }
table.calen .sat { color:blue; }
table.calen .sun { color:red; }
table.calen .today { background-color:#ffe4ce; font-weight:bold; color:black; }
table.calen tr.bg1 { background-color:#f5f5dc; color:black; }
table.calen tr.bg2 { background-color:#eeeeee; color:black; }

Recommended Posts

Schedule table