[PYTHON] Tableau des horaires

Environnement d'exécution

Ubuntu Stdio 18.04LTS Python 3.6.9 Firefox 71.0 (64 bits)

Site référencé

Créez votre propre application TODO avec Python https://news.mynavi.jp/article/zeropython-57/

Essayez d'utiliser JavaScript Exemple 11: Création d'un calendrier http://home.e02.itscom.net/shouji/pc/js/p-11.html

Autre

schedule.png

Je cherche un moyen de redimensionner le bouton d'ajout, mais je ne le trouve pas.

L'opportunité de faire

Je lisais l'article "Faisons ma propre application TODO avec Python" et j'ai commencé à l'utiliser en pensant qu'elle pourrait être utilisée telle quelle pour le planning. Net, c'est une combinaison d'exemples de code sur le net par des amateurs. Même si j'étudie en vieillissant, cela s'effacera de ma mémoire en moins d'une semaine, je suis donc un débutant.

Ce que j'ai fait, c'est la fonction de tri des amateurs dans l'exemple de programme de "Faisons votre propre application TODO avec Python" (S'il y a des espaces demi-largeur et pleine largeur, les lignes seront disjointes, vous devez donc vous connecter avec un trait de soulignement. ) Je l'ai ajouté car il est plus pratique d'avoir une horloge et un calendrier. La plupart des échantillons sont tels qu'ils sont sur le net. (Je vous remercie)

Il n'y a pas de code sur le site "Faisons votre propre application TODO avec Python", mais il y a un lien pour que vous puissiez le télécharger, alors téléchargez "Le 57ème programme ici" à partir de là. Je vais.

Cela ne fonctionnait pas tel quel, alors j'avais besoin de le réparer un peu. 1.data.txt n'est pas inclus. Si vous le connaissez, vous pouvez facilement le trouver en regardant le code, mais je ne sais pas, j'ai donc créé un fichier data.txt avec une liste de caractères appropriée, mais cela a bien fonctionné.

  1. Je pense que cela dépend du système d'exploitation, mais changez-le en #! / Usr / bin / python3 sur la première ligne de todo.py.

    1. Cela ne fonctionnait pas sur python2 (sans probablement la bibliothèque), alors changez la deuxième ligne de start.sh de python ... à python3 ... Pour Windows, je pense que c'est start.bat.
  2. Donnez l'autorisation d'exécution à start.sh. Donnez l'autorisation d'exécution à todo.py.

Désormais, lorsque vous exécutez start.sh, le serveur Web simple est démarré. Ensuite, lorsque vous accédez à "http: // localhost: 8000 / cgi-bin / todo.py" avec un navigateur, l'exemple de programme démarre et "Compliment TODO" s'affiche.

J'ai appris pour la première fois que HP peut être affiché avec un script (programme) python. (Mais c'est gênant)

J'ai ajouté un tri, une horloge et un calendrier à cela.

python


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

FILE_DATA = "./data.txt"

with open(FILE_DATA) as f:
    s = f.read()
    s1=s.split() #Convertir str en liste
    s1.sort() #Trier
    s = '\n'.join(s1) #Convertir la liste en str

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

#Paramètres requis pour gérer le japonais
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()

#Vérifiez les paramètres et branchez le processus
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("Mauvais paramètre")

#Afficher une liste de TODO
def mode_show():
    items = load_items()
    t = "<h1>Mon agenda</h1>"

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

    t += '<h2 id="time"></h2>' #d'ici
    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>"  #Horloge jusqu'à présent

    t += "<form><p class='frm'>des plans: "  #d'ici
    t += input_tag('text', 'todo', '')
    t += input_tag('hidden', 'm', 'add')
    t += input_tag('submit', '', 'ajouter à')
    t += "</p></form></div>"  #Pièce supplémentaire prévue jusqu'à présent

    for no, it in enumerate(items):  #programme
        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">'   #calendrier
    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("journée", "Mois", "Feu", "eau", "bois", "Argent", "sol");'
    t += '''document.write("<table class='calen'>\\n<tr class='bg1'><th colspan=7>" + year + "Année" + month + "Mois</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)

#Ajouter un nouveau TODO
def mode_add():
    todo = get_form('todo', '')
    if todo == '':
        out_html('Aucun texte à ajouter')
        return
    items = load_items()
    items.append(todo)
    save_items(items)
    s = "<div id='items'><p>Écrit.<br/>"
    s += "Génial! Je fais toujours de mon mieux!</p>"
    s += "<a href='todo.py'>Revenir</a></div>"
    out_html(s)

#Effacer l'article
def mode_remove():
    no = int(get_form('no', '-1'))
    if no < 0:
        out_html('Mauvais numéro')
        return
    items = load_items()
    del items[no]
    save_items(items)
    out_html('Il a été supprimé.<a href="todo.py">Revenir</a>')

#Charger l'élément
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

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

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

# <input>Renvoie une balise
def input_tag(type, name, value):
    s = '<input type="{}" name="{}" value="{}">'
    return s.format(type, name, value)

#Obtient et retourne la valeur du formulaire
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;             /*Spécification de la marge*/
    display: flex;              /*Faites-en une boîte flexible*/
}
 
.div_2 {
    padding: 10px;
    margin:  10px;              /*Marge extérieure*/
    border-radius:  5px;        /*Coins arrondis*/
    font-size: 30px;            /*Spécifiez la taille de la police*/

}
 
.div_2:nth-child(1) {
    flex-basis:  1100px;         /*Spécification de la largeur*/

}
 
.div_2:nth-child(2) {
    flex-basis:  200px;         /*Spécification de la largeur*/
    text-align:  center;        /*Centrage des caractères*/
}

/*calendrier*/
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

Tableau des horaires