LoginSignup
11
12

More than 5 years have passed since last update.

pythonでbottleを使ってtodoアプリを作ってみた

Posted at

利用しているpythonバージョンは3.6です
自分の備忘録的なものですがアドバイス等もらえるとうれしいです。

ソースコード

todo.py
#coding:utf-8
from bottle import run,route,template,redirect,request,post
import sqlite3

@route("/")
def index():
    todo_list = get_todo()
    return template("index",todo_list=todo_list)

@route("/enter",method=["POST"])
def enter():
    todo=request.POST.getunicode("todo_list")
    #データベースにtodo_listを書き込む
    save_todo(todo)
    #return todo
    return redirect("/")

@route("/delete",method=["POST"])
def delete():

    conn=sqlite3.connect("todo.db")
    c=conn.cursor()

    delete="delete from todo_list where todo='{0}'".format(request.POST.getunicode("finished"))
    c.execute(delete)
    conn.commit()
    return redirect("/")

#データベースにtodo_listを保存
def save_todo(todo):
    conn= sqlite3.connect('todo.db')
    c= conn.cursor()
    insert="insert into todo_list(todo) values('{0}')".format(todo)
    c.execute(insert)
    conn.commit()

#データベースのtodo_listを読み込む
def get_todo():
    conn= sqlite3.connect('todo.db')
    c= conn.cursor()
    select="select * from todo_list"
    c.execute(select)
    row = c.fetchall()
    return row
run(host="localhost",port=8000,debug=True,reloader=True)

index.html
<!DOCTYPE html>
<html lang="jp">
<head>
    <meta charset="UTF-8">
    <title>To Do App</title>
</head>
<body>
    <h1>Welcome to ToDo list</h1>

    <form name="todo" method="POST" action="/enter">
        <input type="text" name="todo_list"  required /><br/>
        <input type="submit" value="追加" />
    </form>


    <form method="POST" action="/delete">
      % for todo in todo_list:
      <input type="checkbox" name="finished"value="{{todo[0]}}">{{todo[0]}}<br>
      % end
      <input type="submit" value="チェックしたリストを消去">
    </form>


</body>
</html>

感想

初めてのwebアプリ制作だったのでかなり苦労しました。
また、まだまだ完成度は低いので改良を重ねていこうと思っています。
特に、デザインをなにもやっていないので見た目だけでもカッコよくできたらいいと思っています。

11
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12