Flaskr
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
* {% block body %}{% endblock %}
```
]]
.column_t1[.vmiddle[
###flaskr.py
####
.figstyle1[
![](images/flaskr-main.jpg)
]
]]
---
class: split-50 nopadding
.column_t2[.vmiddle[
```
import os
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
#---------------------------------------------
app = Flask(__name__)
#---------------------------------------------
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE=os.path.join(app.root_path, 'flaskr.db'),
DEBUG=True,
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
#---------------------------------------------
*def connect_db():
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
#---------------------------------------------
*def get_db():
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
#---------------------------------------------
*@app.teardown_appcontext
def close_db(error):
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
```
]]
.column_t1[.vmiddle[
###flaskr.py
####
]]
---
class: split-50 nopadding
.column_t1[.vmiddle[
###flaskr.py
```
@app.route('/')
def show_entries():
db = get_db()
cur = db.execute('select title, text from entries order by id desc')
entries = cur.fetchall()
* return render_template('show_entries.html', entries=entries)
#---------------------------------------------
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('insert into entries (title, text) values (?, ?)',
[request.form['title'], request.form['text']])
db.commit()
flash('New entry was successfully posted')
* return redirect(url_for('show_entries'))
```
]]
.column_t2[.vmiddle[
### show_entries.html
```html
{% extends "layout.html" %}
*{% block body %}
* {% if session.logged_in %}