Skip to content

Instantly share code, notes, and snippets.

@shimo164
Last active December 4, 2018 03:10
Show Gist options
  • Select an option

  • Save shimo164/0caa23bfd1fb2b089839bb950b0516ab to your computer and use it in GitHub Desktop.

Select an option

Save shimo164/0caa23bfd1fb2b089839bb950b0516ab to your computer and use it in GitHub Desktop.
Simple web app
import os
import sqlite3
from flask import Blueprint, Flask, current_app, g, render_template, request
# manually set instance dir
app = Flask(__name__, instance_path='/home/../simple/instance')
app.config.from_mapping(
DATABASE=os.path.join(app.instance_path, 'simple.sqlite')
)
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
@app.route('/', methods=('GET', 'POST'))
def hello():
if request.method == 'POST':
enter_value = request.form['value']
if RepresentsInt(enter_value):
db = get_db()
error = None
if error is None:
db.execute(
'INSERT INTO post (value) VALUES (?)', (enter_value,)
)
db.commit()
return render_template('index.html')
else:
return 'enter int'
flash(error)
return render_template('index.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment