├── .gitignore ├── app ├── __init__.py ├── forms.py ├── models.py ├── templates │ ├── base.html │ └── index.html └── views.py ├── main.png ├── readme.md ├── requirements.txt ├── rethink-admin.png ├── run.py ├── tasks.png └── template ├── app ├── __init__.py ├── forms.py ├── models.py ├── templates │ ├── base.html │ └── index.html └── views.py └── run.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | bin/ 10 | build/ 11 | develop-eggs/ 12 | dist/ 13 | eggs/ 14 | lib/ 15 | lib64/ 16 | parts/ 17 | sdist/ 18 | var/ 19 | *.egg-info/ 20 | .installed.cfg 21 | *.egg 22 | .DS_Store 23 | 24 | # Installer logs 25 | pip-log.txt 26 | pip-delete-this-directory.txt 27 | 28 | # Unit test / coverage reports 29 | .tox/ 30 | .coverage 31 | .cache 32 | nosetests.xml 33 | coverage.xml 34 | 35 | # Translations 36 | *.mo 37 | 38 | # Mr Developer 39 | .mr.developer.cfg 40 | .project 41 | .pydevproject 42 | 43 | # Rope 44 | .ropeproject 45 | 46 | # Django stuff: 47 | *.log 48 | *.pot 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | 54 | env 55 | -------------------------------------------------------------------------------- /app/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | app.config["SECRET_KEY"] = "my_precious" 5 | 6 | from app import views 7 | -------------------------------------------------------------------------------- /app/forms.py: -------------------------------------------------------------------------------- 1 | from flask.ext.wtf import Form 2 | from wtforms.fields import TextField 3 | from wtforms.validators import Required 4 | 5 | class TaskForm(Form): 6 | label = TextField('label', validators = [Required()]) 7 | -------------------------------------------------------------------------------- /app/models.py: -------------------------------------------------------------------------------- 1 | # models.py 2 | 3 | -------------------------------------------------------------------------------- /app/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Todo List - Featuring Flask and RethinkDB 5 | 6 | 7 | 13 | 14 | 15 |
16 | {% block content %}{% endblock %} 17 |
18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 | 4 |

Todo List

5 |

With Flask and RethinkDB

6 | 7 |
8 | {{form.hidden_tag()}} 9 |

10 |

Enter a task

11 | {{form.label}} 12 | 13 | {% for error in form.errors.label %} 14 | {{ error }} 15 | {% endfor %} 16 |

17 |
18 | 19 | 20 | {% for task in tasks %} 21 |
  • {{ task.name }}
  • 22 | {% endfor %} 23 | 24 | {% endblock %} 25 | -------------------------------------------------------------------------------- /app/views.py: -------------------------------------------------------------------------------- 1 | from flask import render_template, url_for, redirect, g, request ## 2 | from app import app 3 | from forms import TaskForm 4 | import json ## 5 | 6 | # rethink imports 7 | import rethinkdb as r 8 | from rethinkdb.errors import RqlRuntimeError, RqlDriverError 9 | 10 | # rethink config 11 | RDB_HOST = 'localhost' 12 | RDB_PORT = 28015 13 | TODO_DB = 'todo' 14 | 15 | # db setup; only run once 16 | def dbSetup(): 17 | connection = r.connect(host=RDB_HOST, port=RDB_PORT) 18 | try: 19 | r.db_create(TODO_DB).run(connection) 20 | r.db(TODO_DB).table_create('todos').run(connection) 21 | print 'Database setup completed' 22 | except RqlRuntimeError: 23 | print 'Database already exists.' 24 | finally: 25 | connection.close() 26 | dbSetup() 27 | 28 | # open connection before each request 29 | @app.before_request 30 | def before_request(): 31 | try: 32 | g.rdb_conn = r.connect(host=RDB_HOST, port=RDB_PORT, db=TODO_DB) 33 | except RqlDriverError: 34 | abort(503, "Database connection could be established.") 35 | 36 | # close the connection after each request 37 | @app.teardown_request 38 | def teardown_request(exception): 39 | try: 40 | g.rdb_conn.close() 41 | except AttributeError: 42 | pass 43 | 44 | @app.route('/', methods = ['GET', 'POST']) 45 | def index(): 46 | form = TaskForm() 47 | if form.validate_on_submit(): 48 | r.table('todos').insert({"name":form.label.data}).run(g.rdb_conn) 49 | return redirect(url_for('index')) 50 | selection = list(r.table('todos').run(g.rdb_conn)) 51 | return render_template('index.html', form = form, tasks = selection) 52 | -------------------------------------------------------------------------------- /main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/flask-rethink/0bffddec55b220449e5be9834c45cc1e6db4eb79/main.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | check out the blog post here - [http://www.realpython.com/blog/python/rethink-flask-a-simple-todo-list-powered-by-flask-and-rethinkdb/](http://www.realpython.com/blog/python/rethink-flask-a-simple-todo-list-powered-by-flask-and-rethinkdb/#.UqyteWRDtTc) 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.10.1 2 | Flask-WTF==0.9.3 3 | Jinja2==2.7.1 4 | MarkupSafe==0.18 5 | WTForms==1.0.5 6 | Werkzeug==0.9.4 7 | itsdangerous==0.23 8 | protobuf==2.5.0 9 | rethinkdb==1.11.0-1 10 | wsgiref==0.1.2 11 | -------------------------------------------------------------------------------- /rethink-admin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/flask-rethink/0bffddec55b220449e5be9834c45cc1e6db4eb79/rethink-admin.png -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from app import app 2 | 3 | if __name__ == '__main__': 4 | app.run(debug = True) 5 | -------------------------------------------------------------------------------- /tasks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjhea0/flask-rethink/0bffddec55b220449e5be9834c45cc1e6db4eb79/tasks.png -------------------------------------------------------------------------------- /template/app/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | app.config["SECRET_KEY"] = "my_precious" 5 | 6 | from app import views 7 | -------------------------------------------------------------------------------- /template/app/forms.py: -------------------------------------------------------------------------------- 1 | from flask.ext.wtf import Form 2 | from wtforms.fields import TextField 3 | from wtforms.validators import Required 4 | 5 | class TaskForm(Form): 6 | label = TextField('label', validators = [Required()]) 7 | -------------------------------------------------------------------------------- /template/app/models.py: -------------------------------------------------------------------------------- 1 | # models.py 2 | 3 | -------------------------------------------------------------------------------- /template/app/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Todo List - Featuring Flask and RethinkDB 5 | 6 | 7 | 13 | 14 | 15 |
    16 | {% block content %}{% endblock %} 17 |
    18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /template/app/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 | 4 |

    Todo List

    5 |

    With Flask and RethinkDB

    6 | 7 |
    8 | {{form.hidden_tag()}} 9 |

    10 |

    Enter a task

    11 | {{form.label}} 12 | 13 | {% for error in form.errors.label %} 14 | {{ error }} 15 | {% endfor %} 16 |

    17 |
    18 | 19 | {% for task in tasks %} 20 |
  • {{ task.name }}
  • 21 | {% endfor %} 22 | 23 | {% endblock %} 24 | -------------------------------------------------------------------------------- /template/app/views.py: -------------------------------------------------------------------------------- 1 | from flask import render_template, url_for, redirect, g, abort 2 | from app import app 3 | from forms import TaskForm 4 | 5 | @app.route("/") 6 | def index(): 7 | form = TaskForm() 8 | return render_template('index.html', form=form) 9 | -------------------------------------------------------------------------------- /template/run.py: -------------------------------------------------------------------------------- 1 | from app import app 2 | 3 | if __name__ == '__main__': 4 | app.run(debug = True) 5 | --------------------------------------------------------------------------------